如何在 Java 代码中选择 TitledPane 的标题,而不使用 CSS?
我制作了TitledPane,想让标题的字体粗细为粗体。
所以我尝试了这样的。
TitledPane Rootpane = new TitledPane();
Rootpane.setText("Options");
Rootpane.setStyle("-fx-background-color: #eeeeee; -fx-text-fill: #1b75bc; -fx-font-weight: bold;");
标题变为粗体,但位于 TitledPane 中的其他按钮也变为粗体...
我只是想让标题加粗,没有 CSS 文件。
那么我如何在Java代码中选择标题呢?
我尝试过这样
Rootpane.lookup(".titled-pane > .title").setStyle("-fx-font-weight: bold;");
但是结果是
Exception in thread "main" java.lang.NullPointerException
最佳答案
您应该在将节点添加到舞台之后以及 Stage.show()
之后调用 lookup()
。例如这样:
@Override
public void start(Stage primaryStage) {
TitledPane root = new TitledPane();
root.setText("Options");
primaryStage.setScene(new Scene(root, 200, 100));
primaryStage.show();
root.lookup(".titled-pane > .title > .text").setStyle("-fx-font-weight: bold;");
}
如您所见,您还应该使用 .titled-pane > .title > .text
来获取实际的文本标签 ( docs )。
关于java - 如何在 JavaFX 中选择 TitledPane 的标题而不使用 CSS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56137341/