java JavaFX CSS 属性和选择器的最佳参考

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7880410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:48:44  来源:igfitidea点击:

Best reference for JavaFX CSS properties & selectors

javajavafx-2

提问by Bryan Young

I'm trying to learn JavaFX 2, but I've been stumbling a lot trying to style my application. I've found this documentwhich tries to document controls and the css properties that apply to them. I can't tell if it's incomplete, if I should be using some unknown selectors or JavaFX's CSS support just isn't powerful enough for my needs.

我正在尝试学习 JavaFX 2,但是我在尝试设计我的应用程序样式时遇到了很多困难。我发现这个文档试图记录控件和适用于它们的 css 属性。我不知道它是否不完整,是否应该使用一些未知的选择器,或者 JavaFX 的 CSS 支持不足以满足我的需求。

Here are a couple of examples:

下面是几个例子:

  • How would I change the background color for the area behind a TabPane without coloring every other child component (is there a selector for that, or perhaps a property?)
  • How would I change the color of non-selected tabs?
  • 我将如何更改 TabPane 后面区域的背景颜色而不为每个其他子组件着色(是否有一个选择器,或者一个属性?)
  • 如何更改非选定标签的颜色?

回答by JimClarke

Have you tried something like this?

你有没有尝试过这样的事情?

This uses an ID selector as shown in the "Skinning JavaFX Applications with CSS" document. You could also leave off the "#MyTabPane" selector and have it apply to all TabPane's. (It looks like the .tab and .tab-content-area selectors are not discussed in the reference guide. I went to the "caspian.css" file contained in jfxrt.jar file to find them.)

这使用了一个 ID 选择器,如“Skinning JavaFX Applications with CSS”文档中所示。您也可以不使用“#MyTabPane”选择器并将其应用于所有 TabPane。(看起来参考指南中没有讨论 .tab 和 .tab-content-area 选择器。我去了 jfxrt.jar 文件中包含的“caspian.css”文件来找到它们。)

TabExample.css

TabExample.css

#MyTabPane .tab {
    -fx-background-color: blue;
}
#MyTabPane .tab:selected {
    -fx-background-color: red;
}

#MyTabPane .tab-content-area {
    -fx-background-color: cyan;
}

#MyTabPane .tab *.tab-label {
    -fx-text-fill: white;
}

TabPaneEx.java

TabPaneEx.java

@Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World");
        StackPane root = new StackPane();
        TabPane pane = new TabPane();
        pane.setId(("MyTabPane"));
        Tab tab1 = new Tab("ONE");
        Tab tab2 = new Tab("TWO");
        Tab tab3 = new Tab("THREE");
        pane.getTabs().addAll(tab1,tab2,tab3);
        Scene scene = new Scene(root, 300, 250);
        root.getChildren().add(pane);
        scene.getStylesheets().add(
                this.getClass().getClassLoader().getResource("tabpaneex/TabExample.css").toString());
        primaryStage.setScene(scene);
        primaryStage.show();
    }