什麼是三態複選框?如何在 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