一、介绍
java8中新增了javafx.css开放了css相关api。
选择器分类:
Type选择器:通过Node的getTypeSelector可以获取
id选择器:通过设定id=属性(注意这里的id不是fx:id)
styleClass属性: styleClass可以使用class选择器
选择器命名规范:如ToggleButton需要写成:toggle-button
与w3c css语法的一些区别:
1.不支持 @font‑face, @-keyword 等
2.不支持层次伪类选择器
3.伪类:active,:focus等,在Nodes中变为:pressed, :focused
4.超链接伪类:link,:visited变为:visited
5.javafx中不支持逗号序列的值,如不能再-fx-font-family以逗号分隔同时指导fontsize等
6.javafx css使用HSB颜色模型代替HSL
7、javafx css不会对uri进行编码
关于Inheritance继承
举例:
Scene scene = new Scene(new Group());
scene.getStylesheets().add(“test.css”);
Rectangle rect = new Rectangle(100,100);
rect.setLayoutX(50);
rect.setLayoutY(50);
rect.getStyleClass().add(“my-rect”);
((Group)scene.getRoot()).getChildren().add(rect);
.my-rect { -fx-fill: red; }
.my-rect {
-fx-fill: yellow;
-fx-stroke: green;
-fx-stroke-width: 5;
-fx-stroke-dash-array: 12 2 4 2;
-fx-stroke-dash-offset: 6;
-fx-stroke-line-cap: butt;
}
语法排错:
WARNING: com.sun.javafx.css.parser.CSSParser declaration Expected ‘’ while parsing ‘-fx-background-color’ at ?[1,49]
[line, position]