javafx的css语法,使用JavaFx的Textfield CSS样式

I'm trying to style some textfields using JavaFX, but I'm not getting desired results. My goal is to have the textfield be represented by a singular underline. Here's my CSS:

.text-field{

-fx-font-family: "Quicksand";

-fx-font-size: 18;

-fx-padding: 1,1,1,1;

-fx-border-color: grey;

-fx-border-width: 2;

-fx-border-radius: 1;

-fx-border: gone;

-fx-background-color: transparent;

-fx-text-fill: grey;

}

I have researched this question, but the answers I find to similar ones don't really contain enough information to reproduce and apply properly to my program. It doesn't help that I'm not very knowledgeable about CSS styling. I've tried to use insets with minimal results. Thank you for any answers provided!

解决方案

The following works for me:

/* File style.css */

.text-field {

-fx-background-color: -fx-text-box-border, -fx-background ;

-fx-background-insets: 0, 0 0 1 0 ;

-fx-background-radius: 0 ;

}

.text-field:focused {

-fx-background-color: -fx-focus-color, -fx-background ;

}

The following test harness for this

import javafx.application.Application;

import javafx.geometry.Insets;

import javafx.scene.Scene;

import javafx.scene.control.TextField;

import javafx.scene.layout.GridPane;

import javafx.stage.Stage;

public class TextFieldStyleTest extends Application {

@Override

public void start(Stage primaryStage) {

GridPane root = new GridPane();

root.setHgap(10);

root.setVgap(5);

for (int row = 0 ; row < 4; row++) {

for (int col = 0 ; col < 2; col++) {

root.add(new TextField(), col, row);

}

}

root.setPadding(new Insets(5));

Scene scene = new Scene(root);

scene.getStylesheets().add("style.css");

primaryStage.setScene(scene);

primaryStage.show();

}

public static void main(String[] args) {

launch(args);

}

}

produces

76dd84523fcaf9812278cffa4aa277e1.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值