Javafx 简明教程
JavaFX - CSS
CSS ,也称为 Cascading Style Sheet ,是一种简单的设计语言,旨在简化制作网页并使其易于用户查看的过程。它允许我们定义网页用户界面元素的外观。使用 CSS,我们可以控制文本颜色、字体样式、段落间距、列宽和布局。除此之外,我们还可以控制所使用的背景图片或颜色、布局设计、不同设备和屏幕尺寸的视口变化,以及各种其他效果。
CSS, also referred to as Cascading Style Sheet, is a simple design language intended to simplify the process of making web pages presentable and user friendly. It allows us to define the appearance of user interface elements of a web page. Using CSS, we can control the color of the text, style of fonts, spacing between paragraphs, size of columns and layout. Apart from these, we can also control the background images or colors that are used, layout designs, variations in viewport for different devices and screen sizes as well as a variety of other effects.
通常,CSS 广泛用于 Web 开发,但它也可以应用于 JavaFX 应用程序。在本教程中,我们将学习如何在 JavaFX 应用程序中使用 CSS。
Generally, CSS is widely used in web development, but it can also be applied to JavaFX application. In this tutorial, we are going to learn how to use the CSS in JavaFX application.
CSS in JavaFX
JavaFX 为我们提供了使用 CSS 增强应用程序外观的便利性。包 javafx.css 包含用于为 JavaFX 应用程序应用 CSS 的类。
JavaFX provides us the facility of using CSS to enhance the look and feel of the application. The package javafx.css contains the classes that are used to apply CSS for JavaFX applications.
CSS 包含浏览器解释然后应用于文档中相应元素的样式规则。
A CSS comprises of style rules that are interpreted by the browser and then applied to the corresponding elements in our document.
样式规则由三部分组成,如下所示 −
A style rule is made of three parts, which are as follows −
-
Selector − A selector is an HTML tag at which a style will be applied. This could be any tag like <h1> or <table>, etc.
-
Property − A property is a type of attribute of the HTML tag. In simpler terms, all the HTML attributes are converted into CSS properties. They could be color, border, etc.
-
Value − Values are assigned to properties. For example, a color property can have value either red or #F1F1F1, etc.
我们可以将 CSS 样式规则语法设为:
we can put CSS Style Rule Syntax as follows −
selector { property: value }
JavaFX 使用的默认样式表是 modena.css 。它位于 JavaFX 运行时 jar 中。
The default style sheet used by JavaFX is modena.css. It is found in the JavaFX runtime jar.
Adding Inline Style Sheets in JavaFX
我们还可以使用 setStyle() 方法添加内联样式。这些样式仅由键值对组成,并且适用于设置它们的节点。以下是将内联样式表设置到按钮的示例代码。
we can also add in-line styles using the setStyle() method. These styles consist of only key-value pairs and they are applicable to the nodes on which they are set. Following is a sample code of setting an inline style sheet to a button.
.button {
-fx-background-color: red;
-fx-text-fill: white;
}
Example
假设我们开发了一个显示带有文本字段、密码字段、两个按钮的表单的 JavaFX 应用程序。默认情况下,此表单的外观如下图所示 −
Assume that we have developed an JavaFX application which displays a form with a Text Field, Password Field, Two Buttons. By default, this form looks as shown in the following screenshot −
以下 JavaFX 代码演示了如何使用 CSS 向上述应用程序添加样式。将此代码保存在名为 CssExample.java 的文件中。
The following JavaFX code demonstrates how to add styles to the above application using CSS. Save this code in a file with the name CssExample.java.
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, 400, 300);
// Setting title to the Stage
stage.setTitle("CSS Example in JavaFX");
// 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 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls CssExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls CssExample
执行后,上述程序会生成如下所示的 JavaFX 窗口。
On executing, the above program generates a JavaFX window as shown below.