📜  JavaFX |用例子提醒

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

JavaFX |用例子提醒

Alert 是 JavaFX 的一部分,它是 Dialog 类的子类。警报是一些预定义的对话框,用于向用户显示一些信息。警报基本上是特定的警报类型:

  • CONFIRMATION 警报: CONFIRMATION 警报类型将警报对话框配置为以暗示对话框内容正在寻求用户确认的方式出现。
  • WARNING 警报: WARNING 警报类型将警报对话框配置为以暗示对话框内容警告用户某些事实或操作的方式出现。
  • NONE 警报: NONE 警报类型的效果是不在警报中设置任何默认属性。
  • INFORMATION 警报: INFORMATION 警报类型将警报对话框配置为以暗示对话框内容正在通知用户一条信息的方式出现。
  • 错误警报:错误警报类型将警报对话框配置为以暗示出现问题的方式出现。

警报包含 3 个部分:

  • 标题
  • 内容文本
  • 确认按钮

该类的构造函数是:

  1. Alert(Alert.AlertType a) :创建具有指定警报类型的新警报。
  2. Alert(Alert.AlertType a, String c, ButtonType... b) :创建具有指定警报类型、内容和按钮类型的新警报。

常用方法:

methodexplanation
getAlertType()set a specified alert type
setAlertType(Alert.AlertType a)set a specified alert type for the alert
getButtonTypes()Returns an ObservableList of all ButtonType instances that are currently set inside this Alert instance.
setContentText(String s)sets the context text for the alert
getContentText()returns the content text for the alert.

下面的程序说明了 Alert 类:

  1. 创建不同类型的警报并显示它们的程序:该程序创建一个默认类型的警报。需要时,警报将更改为不同的警报类型。该程序创建一个由名称 b、b1、b2、b3 指示的按钮。按钮将在场景内创建,而场景又将托管在舞台内。我们将创建一个标签来显示按钮是否被按下。函数setTitle() 用于为舞台提供标题。然后创建一个平铺窗格,调用 addChildren() 方法将按钮和标签附加到场景中。最后,调用 show() 方法来显示最终结果。我们将创建一个事件处理程序来处理按钮事件。事件处理程序将使用 setOnAction()函数添加到按钮。按下按钮时,它们将显示与其关联的相应警报,并将使用函数setAlertType() 设置相应的警报类型。
Java
// Java Program to create alert of different
// types and display them
 
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 Alert_1 extends Application {
 
    // launch the application
    public void start(Stage s)
    {
        // set title for the stage
        s.setTitle("creating alerts");
 
        // create a button
        Button b = new Button("Confirmation alert");
        Button b1 = new Button("error alert");
        Button b2 = new Button("Information alert");
        Button b3 = new Button("Warning alert");
 
        // create a tile pane
        TilePane r = new TilePane();
 
        // create a alert
        Alert a = new Alert(AlertType.NONE);
 
        // action event
        EventHandler event = new
                         EventHandler() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.CONFIRMATION);
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler event1 = new
                         EventHandler() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.ERROR);
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler event2 = new
                         EventHandler() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.INFORMATION);
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler event3 = new
                          EventHandler() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.WARNING);
 
                // show the dialog
                a.show();
            }
        };
 
        // when button is pressed
        b.setOnAction(event);
        b1.setOnAction(event1);
        b2.setOnAction(event2);
        b3.setOnAction(event3);
 
        // add button
        r.getChildren().add(b);
        r.getChildren().add(b1);
        r.getChildren().add(b2);
        r.getChildren().add(b3);
 
        // 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 alert and set
// different alert types and button type
// and also set different content text
 
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 Alert_2 extends Application {
 
    // launch the application
    public void start(Stage s)
    {
        // set title for the stage
        s.setTitle("creating alerts");
 
        // create a button
        Button b = new Button("Confirmation alert");
        Button b1 = new Button("error alert");
        Button b2 = new Button("Information alert");
        Button b3 = new Button("Warning alert");
        Button b4 = new Button("none alert");
 
        // create a tile pane
        TilePane r = new TilePane();
 
        // create a alert
        Alert a = new Alert(AlertType.NONE);
 
        // action event
        EventHandler event = new
                          EventHandler() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.CONFIRMATION);
 
                // set content text
                a.setContentText("ConfirmationDialog");
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler event1 = new
                          EventHandler() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.ERROR);
 
                // set content text
                a.setContentText("error Dialog");
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler event2 = new
                          EventHandler() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.INFORMATION);
 
                // set content text
                a.setContentText("Information Dialog");
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler event3 = new
                            EventHandler() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.WARNING);
 
                // set content text
                a.setContentText("Warning Dialog");
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler event4 = new
                               EventHandler() {
            public void handle(ActionEvent e)
            {
                Alert a1 = new Alert(AlertType.NONE,
                              "default Dialog",ButtonType.APPLY);
 
                // show the dialog
                a1.show();
            }
        };
 
        // when button is pressed
        b.setOnAction(event);
        b1.setOnAction(event1);
        b2.setOnAction(event2);
        b3.setOnAction(event3);
        b4.setOnAction(event4);
 
        // add button
        r.getChildren().add(b);
        r.getChildren().add(b1);
        r.getChildren().add(b2);
        r.getChildren().add(b3);
        r.getChildren().add(b4);
 
        // 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. 创建警报并设置不同警报类型和按钮类型以及设置不同内容文本的程序:该程序创建一个默认类型的警报。需要时,警报将更改为不同的警报类型。该程序创建了一个按钮,名称为 b、b1、b2、b3、b4。按钮将在场景内创建,而场景又将托管在舞台内。我们将创建一个标签来显示按钮是否被按下。函数setTitle() 用于为舞台提供标题。然后创建一个平铺窗格,调用 addChildren() 方法将按钮和标签附加到场景中。最后,调用 show() 方法来显示最终结果。我们将创建一个事件处理程序来处理按钮事件。事件处理程序将使用 setOnAction()函数添加到按钮。按下按钮时,它们将显示与其关联的相应警报,并将使用函数setAlertType() 设置相应的警报类型。内容文本也将使用 setContentText() 方法进行更改。我们将为默认类型的第 4 个按钮创建一个新警报,并使用警报的构造函数设置按钮类型。

Java

// Java Program to create alert and set
// different alert types and button type
// and also set different content text
 
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 Alert_2 extends Application {
 
    // launch the application
    public void start(Stage s)
    {
        // set title for the stage
        s.setTitle("creating alerts");
 
        // create a button
        Button b = new Button("Confirmation alert");
        Button b1 = new Button("error alert");
        Button b2 = new Button("Information alert");
        Button b3 = new Button("Warning alert");
        Button b4 = new Button("none alert");
 
        // create a tile pane
        TilePane r = new TilePane();
 
        // create a alert
        Alert a = new Alert(AlertType.NONE);
 
        // action event
        EventHandler event = new
                          EventHandler() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.CONFIRMATION);
 
                // set content text
                a.setContentText("ConfirmationDialog");
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler event1 = new
                          EventHandler() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.ERROR);
 
                // set content text
                a.setContentText("error Dialog");
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler event2 = new
                          EventHandler() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.INFORMATION);
 
                // set content text
                a.setContentText("Information Dialog");
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler event3 = new
                            EventHandler() {
            public void handle(ActionEvent e)
            {
                // set alert type
                a.setAlertType(AlertType.WARNING);
 
                // set content text
                a.setContentText("Warning Dialog");
 
                // show the dialog
                a.show();
            }
        };
 
        // action event
        EventHandler event4 = new
                               EventHandler() {
            public void handle(ActionEvent e)
            {
                Alert a1 = new Alert(AlertType.NONE,
                              "default Dialog",ButtonType.APPLY);
 
                // show the dialog
                a1.show();
            }
        };
 
        // when button is pressed
        b.setOnAction(event);
        b1.setOnAction(event1);
        b2.setOnAction(event2);
        b3.setOnAction(event3);
        b4.setOnAction(event4);
 
        // add button
        r.getChildren().add(b);
        r.getChildren().add(b1);
        r.getChildren().add(b2);
        r.getChildren().add(b3);
        r.getChildren().add(b4);
 
        // 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. 输出:

  • https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Alert.html
  • https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Alert.AlertType.html