JavaFX css 类样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37499885/
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
JavaFX css class style
提问by user5562650
How can I set a CSS style for a class which extends a JavaFX object?
如何为扩展 JavaFX 对象的类设置 CSS 样式?
public class DiagramPane extends ScrollPane implements IDiagramEditor {
// .... Methods go here
}
I've tried the following ways in the main method:
我在 main 方法中尝试了以下方法:
public class DiagramPane extends ScrollPane implements IDiagramEditor {
DiagramPane() {
this.setStyle("-fx-background-color: #f8ecc2;-fx-font-size: 8pt;");
setStyle("-fx-background-color: #f8ecc2;-fx-font-size: 8pt;");
}
}
采纳答案by Jan Lochman
Add these lines to your css file
将这些行添加到您的 css 文件中
.diagram-pane {
-fx-background-color: #f8ecc2;
-fx-font-size: 8pt;
}
and set DiagramPane
instance to use diagram-pane
style class
并设置DiagramPane
实例以使用diagram-pane
样式类
diagramPane.getStyleClass().clear();
diagramPane.getStyleClass().add("diagram-pane");
回答by DVarga
One of the possibilities is what you have mentioned to use setStylemethod of Node.
一种可能性是您提到使用Node.js 的 setStyle方法。
public class MyScrollPane extends ScrollPane {
public MyScrollPane(){
setStyle("-fx-background-color: blue;");
}
}
Another possibility to use a CSS stylesheet
使用 CSS 样式表的另一种可能性
This one is the suggested approach, as it totally separates the CSS styling from the Java code.
这是建议的方法,因为它将 CSS 样式与 Java 代码完全分开。
Note: MyScrollPane.css
is placed in the same directory as the class itself.
注意:MyScrollPane.css
放置在与类本身相同的目录中。
MyScrollPane.java
我的滚动窗格
public class MyScrollPane extends ScrollPane {
public MyScrollPane(){
getStylesheets().add(getClass().getResource("MyScrollPane.css").toExternalForm());
}
}
In this stylesheet you can overwrite the existing CSS classes of the ScrollPane like:
在此样式表中,您可以覆盖 ScrollPane 的现有 CSS 类,例如:
MyScrollPane.css
我的滚动窗格.css
.scroll-pane {
-fx-background-color: red, white;
-fx-background-insets: 0, 2;
-fx-padding: 2.0;
}
To check which classes exist for scroll pane in JavaFX you can read the caspian.css. The base class for ScrollPane
is .scroll-pane
.
要检查 JavaFX 中滚动窗格存在哪些类,您可以阅读caspian.css。的基类ScrollPane
是.scroll-pane
。
Also you can define new CSS classes and add them to your ScrollPane
:
您也可以定义新的 CSS 类并将它们添加到您的ScrollPane
:
public class MyScrollPane extends ScrollPane {
public MyScrollPane(){
getStylesheets().add(getClass().getResource("MyScrollPane.css").toExternalForm());
getStyleClass().add("red-border");
}
}
And in CSS
在 CSS 中
.red-border {
-fx-background-color: red, white;
-fx-background-insets: 0, 2;
-fx-padding: 2.0;
}
To learn about CSS styling in JavaFX: http://docs.oracle.com/javafx/2/css_tutorial/jfxpub-css_tutorial.htm
要了解 JavaFX 中的 CSS 样式:http: //docs.oracle.com/javafx/2/css_tutorial/jfxpub-css_tutorial.htm
Also you can check the CSS Reference Guide for JavaFX: https://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html
您也可以查看 JavaFX 的 CSS 参考指南:https: //docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html