使用JavaFx的Textfield CSS样式

时间:2017-10-18 14:28:13

标签: css javafx textfield

我正在尝试使用JavaFX设置一些文本字段,但我没有得到理想的结果。我的目标是让文本字段用单个下划线表示。这是我的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;
}

我已经研究过这个问题,但我找到的类似问题的答案并没有真正包含足够的信息来重现并适用于我的程序。我对CSS样式知之甚少并没有帮助。我试图使用具有最小结果的插图。感谢您提供任何答案!

2 个答案:

答案 0 :(得分:6)

以下适用于我:

/* 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 ;
}

以下

的测试工具
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);
    }
}

产生

enter image description here

答案 1 :(得分:3)

由于您只需要下划线,因此您需要的最少内容是

.text-field {
  -fx-border-color: grey;
  -fx-border width: 0 0 1 0; // top, right, bottom, left
  -fx-background-color: transparent;
}

这会将边框颜色更改为灰色,除了底部边框以外的所有内容都将边框宽度设置为0,并将文本字段的背景设置为透明,使其不是白色。