JavaFX 禁用元素样式
JavaFX disabled element styling
我有一个包含一些数据的组合框。
public class Test extends Application {
public static final String[] items = "One Two Three".split(" ");
@Override
public void start(Stage primaryStage) throws Exception {
final ComboBox<String> box = new ComboBox<>(FXCollections.observableArrayList(items));
box.getSelectionModel().selectFirst();
primaryStage.setScene(new Scene(box));
primaryStage.show();
}
}
如果我将组合框设置为禁用,它会变灰,但我需要将文本设置为黑色。 Google 说明我需要将不透明度设置为 1.0。
box.setDisable(true);
box.setStyle("-fx-opacity: 1.0;");
然后什么也没发生。它也变灰了。
即使我把text-fill
属性设置成黑色它也变灰了
box.setDisable(true);
box.setStyle("-fx-opacity: 1.0; -fx-text-fill: black;");
会发生什么?如何更改禁用组合框的文本颜色?
disabled
属性从场景图节点级联到它的子节点,因此组合框的所有子节点有效地拾取它们的:disabled
CSS样式。因此,例如,显示所选项目的 Label
使用其 disabled
样式,其不透明度设置为 0.4。
要实现你想要的,就去做
.combo-box:disabled, .combo-box:disabled > * {
-fx-opacity: 1.0 ;
}
在外部 CSS 文件中。
我有一个包含一些数据的组合框。
public class Test extends Application {
public static final String[] items = "One Two Three".split(" ");
@Override
public void start(Stage primaryStage) throws Exception {
final ComboBox<String> box = new ComboBox<>(FXCollections.observableArrayList(items));
box.getSelectionModel().selectFirst();
primaryStage.setScene(new Scene(box));
primaryStage.show();
}
}
如果我将组合框设置为禁用,它会变灰,但我需要将文本设置为黑色。 Google 说明我需要将不透明度设置为 1.0。
box.setDisable(true);
box.setStyle("-fx-opacity: 1.0;");
然后什么也没发生。它也变灰了。
即使我把text-fill
属性设置成黑色它也变灰了
box.setDisable(true);
box.setStyle("-fx-opacity: 1.0; -fx-text-fill: black;");
会发生什么?如何更改禁用组合框的文本颜色?
disabled
属性从场景图节点级联到它的子节点,因此组合框的所有子节点有效地拾取它们的:disabled
CSS样式。因此,例如,显示所选项目的 Label
使用其 disabled
样式,其不透明度设置为 0.4。
要实现你想要的,就去做
.combo-box:disabled, .combo-box:disabled > * {
-fx-opacity: 1.0 ;
}
在外部 CSS 文件中。