📜  JavaFX |选择对话框

📅  最后修改于: 2022-05-13 01:54:47.295000             🧑  作者: Mango

JavaFX |选择对话框

ChoiceDialog 是 JavaFX 框架的一部分。 ChoiceDialog 是一个对话框,它为用户提供一组选项,用户最多可以从中选择一个选项。 Choice 类是基类 Dialog 的派生类。
该类的构造函数是

  1. ChoiceDialog() :创建一个没有项目的空选择对话框。
  2. ChoiceDialog(T d, Collection Choices) :创建一个选择对话框,其中包含来自提供的集合的一组项目和一个选定项目
  3. ChoiceDialog(T d, T…choices) :创建一个选择对话框,其中包含默认选定项和用户可用选项的数组。

常用方法

methodexplanation
getItems()returns all the available items of the choice box
getSelectedItem()returns the selected item of the dialog
setSelectedItem(T item)sets the selected item of the dialog
setContentText(String s)sets the content text of the dialog
setHeaderText(String s)sets the header textof the dialog

下面的程序说明了 ChoiceDialog 类:

  1. 创建一个选择对话框并显示它的程序:该程序创建一个选择对话框d和一个字符串数组days ,其中包含一周中各天的名称。该程序创建一个名称为b的 Button。该按钮将在场景内创建,而场景又将托管在舞台内。我们将创建一个标签来显示按钮是否被按下。函数setTitle() 用于为舞台提供标题。然后创建一个平铺窗格,调用 addChildren() 方法将按钮和标签附加到场景中。最后,调用 show() 方法来显示最终结果。我们将创建一个事件处理程序来处理按钮事件。事件处理程序将使用 setOnAction()函数添加到按钮。单击按钮时,将出现带有选定项目的选择对话框。允许用户从提供的选项中选择一个项目。
Java
// Java Program to create a choice dialog and display it
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.control.Alert.AlertType;
public class choice_1 extends Application {
 
    // launch the application
    public void start(Stage s)
    {
        // set title for the stage
        s.setTitle("creating choice dialog");
 
        // create a button
        Button b = new Button("click");
 
        // create a tile pane
        TilePane r = new TilePane();
 
        // items for the dialog
        String days[] = { "Monday", "Tuesday", "Wednesday",
                                       "Thursday", "Friday" };
 
        // create a choice dialog
        ChoiceDialog d = new ChoiceDialog(days[1], days);
 
        // action event
        EventHandler event = new EventHandler()
        {
            public void handle(ActionEvent e)
            {
 
                // show the dialog
                d.show();
            }
        };
 
        // when button is pressed
        b.setOnAction(event);
 
        // add button
        r.getChildren().add(b);
 
        // create a scene
        Scene sc = new Scene(r, 200, 200);
 
        // set the scene
        s.setScene(sc);
 
        s.show();
    }
 
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}


Java
// Java Program to create a choice dialog and add header
// and content text and also add a label to
// display the selected choice
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.control.Alert.AlertType;
public class choice_2 extends Application {
 
    // launch the application
    public void start(Stage s)
    {
        // set title for the stage
        s.setTitle("creating choice dialog");
 
        // create a button
        Button b = new Button("click");
 
        // items for the dialog
        String days[] = { "Monday", "Tuesday", "Wednesday",
                                       "Thursday", "Friday" };
 
        // create a label
        Label l = new Label(days[1] + " selected");
 
        // create a tile pane
        TilePane r = new TilePane();
 
        // create a choice dialog
        ChoiceDialog d = new ChoiceDialog(days[1], days);
 
        // action event
        EventHandler event = new EventHandler() {
            public void handle(ActionEvent e)
            {
                // setheader text
                d.setHeaderText("day of the week");
 
                // set content text
                d.setContentText("please select the day of the week");
 
                // show the dialog
                d.showAndWait();
 
                // get the selected item
                l.setText(d.getSelectedItem() + " selected");
            }
        };
 
        // when button is pressed
        b.setOnAction(event);
 
        // add button
        r.getChildren().add(b);
        r.getChildren().add(l);
 
        // create a scene
        Scene sc = new Scene(r, 200, 200);
 
        // set the scene
        s.setScene(sc);
 
        s.show();
    }
 
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}


  1. 输出:

  1. 创建一个选择对话框并添加标题和内容文本并添加一个标签以显示所选选项的程序:该程序创建一个选择对话框d和一个字符串数组,其中包含星期几的名称。该程序创建一个名称为b的 Button。该按钮将在场景内创建,而场景又将托管在舞台内。我们将创建一个标签来显示按钮是否被按下。函数setTitle() 用于为舞台提供标题。然后创建一个平铺窗格,调用 addChildren() 方法将按钮和标签附加到场景中。最后,调用 show() 方法来显示最终结果。我们将创建一个事件处理程序来处理按钮事件。事件处理程序将使用 setOnAction()函数添加到按钮。单击按钮时,将出现带有选定项目的选择对话框。允许用户从提供的选项中选择一个项目。我们还创建了一个标签 l 来显示当前选中的项目。我们还将使用 setHeaderText() 和 setContentText() 函数设置标题文本和内容文本。我们将使用 getSelectedItem()函数获取所选项目。

Java

// Java Program to create a choice dialog and add header
// and content text and also add a label to
// display the selected choice
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.control.Alert.AlertType;
public class choice_2 extends Application {
 
    // launch the application
    public void start(Stage s)
    {
        // set title for the stage
        s.setTitle("creating choice dialog");
 
        // create a button
        Button b = new Button("click");
 
        // items for the dialog
        String days[] = { "Monday", "Tuesday", "Wednesday",
                                       "Thursday", "Friday" };
 
        // create a label
        Label l = new Label(days[1] + " selected");
 
        // create a tile pane
        TilePane r = new TilePane();
 
        // create a choice dialog
        ChoiceDialog d = new ChoiceDialog(days[1], days);
 
        // action event
        EventHandler event = new EventHandler() {
            public void handle(ActionEvent e)
            {
                // setheader text
                d.setHeaderText("day of the week");
 
                // set content text
                d.setContentText("please select the day of the week");
 
                // show the dialog
                d.showAndWait();
 
                // get the selected item
                l.setText(d.getSelectedItem() + " selected");
            }
        };
 
        // when button is pressed
        b.setOnAction(event);
 
        // add button
        r.getChildren().add(b);
        r.getChildren().add(l);
 
        // create a scene
        Scene sc = new Scene(r, 200, 200);
 
        // set the scene
        s.setScene(sc);
 
        s.show();
    }
 
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}
  1. 输出: