多读书多实践,勤思考善领悟

JavaFX CSS(层叠样式表)

本文于1660天之前发表,文中内容可能已经过时。

层叠样式表(也称为CSS)是一种简单的设计语言,旨在简化使网页呈现的过程。

CSS处理网页的外观和感觉部分。使用CSS,您可以控制文本的颜色,字体的样式,段落之间的间距,列的大小和布局。除此之外,您还可以控制所使用的背景图像或颜色,布局设计,不同设备和屏幕尺寸的显示变化以及各种其他效果。

JavaFX中的CSS

JavaFX为您提供了使用CSS来增强应用程序外观的功能。包javafx.css包含用于为JavaFX应用程序应用CSS的类。

CSS包含样式规则,这些样式规则由浏览器解释,然后应用于文档中的相应元素。

样式规则由三部分组成,它们是 -

  • Selector 选择器 - 选择器是将应用样式的HTML标记。这可以是<h1><table>等任何标签。
  • Property 属性 - 属性是HTML标记的一种属性。简单来说,所有HTML属性都转换为CSS属性。它们可以是颜色,边框等。
  • Value 值 - 将值分配给属性。例如,颜色属性可以具有红色#F1F1F1 等值

您可以按如下方式放置CSS样式规则语法 -

1
selector { property: value }

CSS风格

JavaFX使用的默认样式表是modena.css。它位于JavaFX运行时jar中。

添加自己的样式表

您可以将自己的样式表添加到JavaFX中的场景,如下所示 -

1
2
Scene scene = new Scene(new Group(), 500, 400); 
scene.getStylesheets().add("path/stylesheet.css");

添加内联样式表

您还可以使用setStyle()方法添加内嵌样式。这些样式仅由键值对组成,它们适用于设置它们的节点。以下是将内联样式表设置为按钮的示例代码。

1
2
3
4
.button { 
-fx-background-color: red;
-fx-text-fill: white;
}

假设我们已经开发了一个JavaFX应用程序,它显示一个带有文本字段,密码字段,两个按钮的表单。默认情况下,此表单如下面的屏幕截图所示 -

网格窗格

以下程序是演示如何在JavaFX中向上述应用程序添加样式的示例。

将此代码保存在名为CssExample.java的文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import javafx.application.Application; 
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class CssExample extends Application {
@Override
public void start(Stage stage) {
//creating label email
Text text1 = new Text("Email");

//creating label password
Text text2 = new Text("Password");

//Creating Text Filed for email
TextField textField1 = new TextField();

//Creating Text Filed for password
PasswordField textField2 = new PasswordField();

//Creating Buttons
Button button1 = new Button("Submit");
Button button2 = new Button("Clear");

//Creating a Grid Pane
GridPane gridPane = new GridPane();

//Setting size for the pane
gridPane.setMinSize(400, 200);

//Setting the padding
gridPane.setPadding(new Insets(10, 10, 10, 10));

//Setting the vertical and horizontal gaps between the columns
gridPane.setVgap(5);
gridPane.setHgap(5);

//Setting the Grid alignment
gridPane.setAlignment(Pos.CENTER);

//Arranging all the nodes in the grid
gridPane.add(text1, 0, 0);
gridPane.add(textField1, 1, 0);
gridPane.add(text2, 0, 1);
gridPane.add(textField2, 1, 1);
gridPane.add(button1, 0, 2);
gridPane.add(button2, 1, 2);

//Styling nodes
button1.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
button2.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");

text1.setStyle("-fx-font: normal bold 20px 'serif' ");
text2.setStyle("-fx-font: normal bold 20px 'serif' ");
gridPane.setStyle("-fx-background-color: BEIGE;");

// Creating a scene object
Scene scene = new Scene(gridPane);

// Setting title to the Stage
stage.setTitle("CSS Example");

// Adding scene to the stage
stage.setScene(scene);

//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}

使用以下命令从命令提示符编译并执行保存的java文件。

1
2
javac CssExample.java 
java CssExample

执行时,上面的程序生成一个JavaFX窗口,如下所示。

CSS示例