什么是三态复选框?如何在 JavaFX 中创建三态复选框?
复选框是一种选择控件,形状为正方形,其中包含一个勾号。通常,复选框具有两种状态:选中和未选中。
根据 GUI(技术),我们还可以拥有第三种状态,名为 *未定义/不确定*,它表示当前复选框既未选中也未取消选中。
JavaFX 支持三态复选框,**javafx.scene.control.CheckBox** 类表示一个复选框,它包含三个布尔属性:
**allowIndeterminate** - 此属性指定复选框是否应具有所有三种状态。您可以使用 **setAllowIndeterminate()** 方法为此属性设置值。
**indeterminate** - 此属性指定复选框是否处于未定义状态。您可以使用 **setIndeterminate()** 方法为此属性设置值。
**selected** - 此属性指定当前复选框是否已选中。您可以使用 **setSelected()** 方法为此属性设置值。
要切换所有三种状态:
实例化 CheckBox 类。
通过传递布尔值 **true** 作为参数来调用 **setAllowIndeterminate()** 方法。
通过传递布尔值 **true** 作为参数来调用 **setIndeterminate()** 方法。
将创建的复选框添加到父节点。
示例
import javafx.application.Application;
import javafx.geometry.Insets;
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.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class CheckBox_Undefined_State extends Application {
public void start(Stage stage) {
//Creating the check boxes
CheckBox checkBox1 = new CheckBox("Salary");
CheckBox checkBox2 = new CheckBox("Over Time Allowence");
CheckBox checkBox3 = new CheckBox("Bonus");
CheckBox checkBox4 = new CheckBox("Night Shift Allowence");
Label label = new Label("Select Recieved Allowences:");
Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
label.setFont(font);
//Setting the indeterminate state true
checkBox2.setAllowIndeterminate(true);
checkBox2.setIndeterminate(true);
checkBox4.setAllowIndeterminate(true);
checkBox4.setIndeterminate(true);
//Adding the toggle button to the pane
VBox vBox = new VBox(5);
vBox.setPadding(new Insets(5, 5, 5, 50));
vBox.getChildren().addAll(label, checkBox1, checkBox2, checkBox3, checkBox4);
//Setting the stage
Scene scene = new Scene(vBox, 595, 150, Color.BEIGE);
stage.setTitle("Check Box Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出

广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP