为什么我无法使用CSS调整javafx中的按钮大小

时间:2019-06-27 15:07:41

标签: javafx

我正在尝试调整按钮的大小,但是它不起作用。

css文件已正确链接到文件。 (按钮的颜色更改)

public void start(Stage primaryStage) throws Exception {
    VBox root = new VBox();
    Button btn = new Button("s");
    root.getChildren().addAll(btn);
    Scene scene = new Scene(root, 400, 400);
    scene.getStylesheets().add("Style.css");
    primaryStage.setScene(scene);
    primaryStage.show();
}

css文件

.button {
  -fx-width: 250px;
  -fx-background-color: red;
}

我也不能为vbox设置间距。(它可以正常工作)

1 个答案:

答案 0 :(得分:2)

尝试以下操作来设置所有按钮的宽度:

.button {
    -fx-min-width: 20px;
    -fx-max-width: 20px;
    -fx-pref-width: 20px;
}

选择要设置的属性( max min pref 或全部)。
height 相同,只需将 width 替换为 height
要根据this Oracle post设置VBox的间距,您可以使用:

.vbox {
    -fx-spacing: 10;
}