JavaFX:带有 CSS 选择器的样式应用程序
JavaFX: Styling application with CSS Selectors
我有几个关于 使用 CSS 选择器 设置 JavaFX 应用程序样式的问题(例如:每个 TableView
的 .table-view
) .
我创建了一个主 CSS 文件,我想在其中为我的应用程序定义通用样式属性。例如:每个 TableView
在每个屏幕中都获得相同的颜色。我只是在与 .fxml 文件关联的每个 .css 中导入 Main.css
。
现在我想以相同的方式在 'sidebar' 中设置每个 HBox
的样式。我试过这样(如 Oracle 文档中所建议):
.sidebar > .hbox {
/* Just some styling */
}
这并不令我惊讶,但以下代码片段有效:
.sidebar > HBox {
/* Just some styling */
}
.sidebar HBox {
/* Just some styling */
}
可能与.sidebar
是自定义样式有关,但我不确定。
所以我的问题是:
1.为什么第一个不起作用?
2。这样做的方法应该是什么? (使用 .hbox
或 HBox
和 >
或什么都没有?)
正如您在 CSS 文档中所见,HBOX
class 没有定义样式 class。因此你不能简单地使用 .hbox
http://docs.oracle.com/javase/8/javafx/api/javafx/scene/doc-files/cssref.html#hbox
如果您只想查找工具栏的直接子项,可以使用 >
符号。在 CSS selector 中使用 >
标志将在性能问题上有一些好处,因为这样做不需要扫描工具栏控件下的完整子层次结构。只会在子级的第一个层次结构中搜索匹配节点。
因此,如果您想要 select 侧边栏的所有直接子按钮,您可以执行以下操作:
. sidebar > .button
但是如果你真的想为侧边栏中的所有按钮设置样式(即使它们被包裹在窗格等中)你需要使用以下 select 或:
.sidebar .button
回到您的 HBOX 问题:即使 HBOX 没有定义的样式 class (.hbox),它也有一个可用于类型 selector 的类型。如 CSS 文档中所述,所有节点都有一个类型:
Node's getTypeSelector method returns a String which is analogous to a
CSS Type Selector. By default, this method returns the simple name of
the class. Note that the simple name of an inner class or of an
anonymous class may not be usable as a type selector. In such a case,
this method should be overridden to return a meaningful value.
因为 HBOX select或正在工作。
我有几个关于 使用 CSS 选择器 设置 JavaFX 应用程序样式的问题(例如:每个 TableView
的 .table-view
) .
我创建了一个主 CSS 文件,我想在其中为我的应用程序定义通用样式属性。例如:每个 TableView
在每个屏幕中都获得相同的颜色。我只是在与 .fxml 文件关联的每个 .css 中导入 Main.css
。
现在我想以相同的方式在 'sidebar' 中设置每个 HBox
的样式。我试过这样(如 Oracle 文档中所建议):
.sidebar > .hbox {
/* Just some styling */
}
这并不令我惊讶,但以下代码片段有效:
.sidebar > HBox {
/* Just some styling */
}
.sidebar HBox {
/* Just some styling */
}
可能与.sidebar
是自定义样式有关,但我不确定。
所以我的问题是:
1.为什么第一个不起作用?
2。这样做的方法应该是什么? (使用 .hbox
或 HBox
和 >
或什么都没有?)
正如您在 CSS 文档中所见,HBOX
class 没有定义样式 class。因此你不能简单地使用 .hbox
http://docs.oracle.com/javase/8/javafx/api/javafx/scene/doc-files/cssref.html#hbox
如果您只想查找工具栏的直接子项,可以使用 >
符号。在 CSS selector 中使用 >
标志将在性能问题上有一些好处,因为这样做不需要扫描工具栏控件下的完整子层次结构。只会在子级的第一个层次结构中搜索匹配节点。
因此,如果您想要 select 侧边栏的所有直接子按钮,您可以执行以下操作:
. sidebar > .button
但是如果你真的想为侧边栏中的所有按钮设置样式(即使它们被包裹在窗格等中)你需要使用以下 select 或:
.sidebar .button
回到您的 HBOX 问题:即使 HBOX 没有定义的样式 class (.hbox),它也有一个可用于类型 selector 的类型。如 CSS 文档中所述,所有节点都有一个类型:
Node's getTypeSelector method returns a String which is analogous to a CSS Type Selector. By default, this method returns the simple name of the class. Note that the simple name of an inner class or of an anonymous class may not be usable as a type selector. In such a case, this method should be overridden to return a meaningful value.
因为 HBOX select或正在工作。