Javafx 简明教程
JavaFX - CheckBox
Checkbox 是一个允许用户选择或取消选择选项的用户界面组件。它最常用语创建多项选择题、偏好设置、过滤器等。下图显示了具有多个选项的筛选器功能,允许用户根据偏好选择类别和品牌。
Checkbox is a user interface component that allows the user to select or deselect an option. It is most commonly used to create multiple-choice questions, preferences, filters, and many more. The figure below shows a filter feature with multiple options, allowing users to choose a category and brand according to their preferences.
Creating CheckBox in JavaFX
在 JavaFX 中,复选框由名为 CheckBox 的类表示。此类属于 javafx.scene.control 包。通过实例化此类,我们可以在 JavaFX 中创建一个复选框。CheckBox 类的构造函数如下:
In JavaFX, the checkbox is represented by a class named CheckBox. This class belongs to the javafx.scene.control package. By instantiating this class, we can create a checkbox in JavaFX. Constructors of the CheckBox class are listed below −
-
CheckBox() − It is the default constructor that constructs a CheckBox without any option name.
-
CheckBox(String str) − It constructs a new CheckBox with the specified option.
CheckBox 类中使用最广泛的构造函数是其参数化构造函数。它接受一个代表 CheckBox 选项名称的文本。创建复选框后,通过将 CheckBox 对象传递给其构造函数来定义 layout pane ,例如 Vbox 或 Hbox。然后,创建一个 Scene 并将其布局面板对象作为参数值传递给其构造函数。接下来,设置 JavaFX 应用程序的 stage 和标题。最后,调用 main() 方法来启动应用程序。
The most commonly used constructor of the CheckBox class is its parameterized constructor. It accepts a text representing the option name of the CheckBox. Once the checkbox is created, define a layout pane, such as Vbox or Hbox by passing the CheckBox object to its constructor. Then, create a Scene and pass the object of layout pane as a parameter value to its constructor. Next, set the stage and title of the JavaFX application. Finally, call the main() method to launch the application.
Example
以下是使用 JavaFX 创建 CheckBox 的程序。将此代码保存在名为 CheckBoxDemo.java 的文件中。
Following is the program that will create the CheckBox using JavaFX. Save this code in a file with the name CheckBoxDemo.java.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
public class CheckBoxDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
// Creating a Label
Label label = new Label("Click the box to select: ");
// Creating three CheckBoxes
CheckBox checkBx1 = new CheckBox("Item1");
checkBx1.setTextFill(Color.GREEN);
checkBx1.setSelected(false);
CheckBox checkBx2 = new CheckBox("Item2");
checkBx2.setTextFill(Color.BLUE);
checkBx2.setSelected(false);
CheckBox checkBx3 = new CheckBox("Item3");
checkBx3.setTextFill(Color.SKYBLUE);
checkBx3.setSelected(false);
// Create a Label to display the selection
Label selectLabel = new Label();
selectLabel.setTextFill(Color.RED);
// Adding listeners to the CheckBoxes
checkBx1.setOnAction(e -> selectLabel.setText("You selected: " +
(checkBx1.isSelected() ? "Item1" : "")
));
checkBx2.setOnAction(e -> selectLabel.setText("You selected: " +
(checkBx2.isSelected() ? "Item2" : "")
));
checkBx3.setOnAction(e -> selectLabel.setText("You selected: " +
(checkBx2.isSelected() ? "Item3" : "")
));
// Create a VBox and add the CheckBoxes and Label
VBox vbox = new VBox();
vbox.setAlignment(Pos.CENTER);
vbox.setPadding(new Insets(10));
vbox.setSpacing(10);
vbox.getChildren().addAll(label, checkBx1, checkBx2, checkBx3, selectLabel);
// Create a scene and add the VBox
Scene scene = new Scene(vbox, 400, 300);
// Set the scene and show the stage
stage.setScene(scene);
stage.setTitle("CheckBox in JavaFX");
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 CheckBoxDemo.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls CheckBoxDemo
在执行后,上述程序生成一个 JavaFX 窗口,如下所示,其中显示三个复选框:
On executing, the above program generates a JavaFX window displaying three checkboxes as shown below −
Creating CheckBox using default constructor in JavaFX
如前所述,我们可以使用 JavaFX 中的默认构造函数或其参数化构造函数创建复选框。在以下示例中,我们将使用默认构造函数并借助名为 setText() 的内置方法传递选项文本。使用名称 JavafxCheckbox.java 将此代码保存在文件中。
As we discussed earlier, we can create CheckBox in JavaFX either by using its default constructor or its parameterized constructor. In the next example, we will use the default constructor and pass the option text with the help of a built-in method named setText(). Save this code in a file with the name JavafxCheckbox.java.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
public class JavafxCheckbox extends Application {
@Override
public void start(Stage stage) throws Exception {
// Creating a Label
Label label = new Label("Click the box to select: ");
// Creating three CheckBoxes without label text
CheckBox checkBx1 = new CheckBox();
checkBx1.setTextFill(Color.GREEN);
checkBx1.setSelected(true);
// adding lable to the check box1
checkBx1.setText("Item1");
CheckBox checkBx2 = new CheckBox();
// adding lable to the check box2
checkBx2.setText("Item2");
checkBx2.setTextFill(Color.BLUE);
checkBx2.setSelected(false);
// Create a Label to display the selection
Label selectLabel = new Label();
selectLabel.setTextFill(Color.RED);
// Adding listeners to the CheckBoxes
checkBx1.setOnAction(e -> selectLabel.setText("You selected: " +
(checkBx1.isSelected() ? "Item1" : "")
));
checkBx2.setOnAction(e -> selectLabel.setText("You selected: " +
(checkBx2.isSelected() ? "Item2" : "")
));
// Create a HBox and add the CheckBoxes and Label
HBox box = new HBox();
box.setAlignment(Pos.CENTER);
box.setPadding(new Insets(10));
box.setSpacing(10);
box.getChildren().addAll(label, checkBx1, checkBx2, selectLabel);
// Create a scene and add the HBox
Scene scene = new Scene(box, 400, 300);
// Set the scene and show the stage
stage.setScene(scene);
stage.setTitle("CheckBox in JavaFX");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
要从命令提示符编译并执行已保存的 Java 文件,请使用以下命令−
To compile and execute the saved Java file from the command prompt, use the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxCheckbox.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxCheckbox
执行以上程序时,它将生成一个 JavaFX 窗口,该窗口显示两个复选框,如下所示 −
On executing the above program, it will generate a JavaFX window displaying two checkboxes as shown below −