如何在 JavaFX 中创建一个 ChoiceDialog?
ChoiceDialog 是一个对话框,用于显示你可以从中选择一个的选项列表。你可以通过实例化javafx.scene.control.ChoiceDialog 类来创建一个选择对话框。
示例
以下示例演示了如何创建 **ChoiceDialog**。
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class ChoiceDialogExample extends Application {
public void start(Stage stage) {
//Creating a choice box
ChoiceDialog<String> choiceDialog = new ChoiceDialog<String>("English");
//Retrieving the observable list
ObservableList<String> list = choiceDialog.getItems();
//Adding items to the list
list.add("English");
list.add("Hindi");
list.add("Telugu");
list.add("Tamil");
//Setting the display text
Text txt = new Text("Click the button to show the choice dialog");
Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
txt.setFont(font);
//Creating a button
Button button = new Button("Show Dialog");
//Showing the choice dialog on clicking the button
button.setOnAction(e -> {
choiceDialog.showAndWait();
});
//Adding the choice box to the group
HBox layout = new HBox(25);
layout.getChildren().addAll(txt, button);
layout.setPadding(new Insets(15, 50, 50, 50));
layout.setStyle("-fx-background-color: BEIGE");
//Setting the stage
Scene scene = new Scene(layout, 595, 300);
stage.setTitle("Choice Dialog");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出

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