JavaFX - 警報框



警報框指的是出現在螢幕上用於告知使用者錯誤或任何事件的彈出視窗或對話方塊。警報框的資訊不僅僅限於錯誤訊息,它可以是任何訊息,包括簡單的“你好”。例如,下圖顯示了關於資料夾刪除的通知 -

JavaFX Alert

JavaFX 中的警報框

在 JavaFX 中,警報框由名為 Alert 的類表示。此類屬於包 javafx.scene.control。透過例項化此類,我們可以在 JavaFX 中建立警報框。此外,我們需要將 Alert.AlertType 列舉值傳遞給建構函式。此值決定對話方塊的預設屬性,例如標題、標題欄、圖形和按鈕。Alert 類的建構函式如下所示:

  • Alert(Alert.AlertType typeOfalert) − 用於使用指定的警報型別構造警報框。

  • Alert(Alert.AlertType typeOfalert, String str, ButtonType buttons) − 使用指定的警報型別、預定義文字和按鈕型別構造警報框。

如何在 JavaFX 中建立警報框?

請按照以下步驟在 JavaFX 中建立警報框。

步驟 1:例項化 Alert 類

要建立警報框,請例項化 Alert 類並將 Alert.AlertType 列舉值作為引數值傳遞給其建構函式,如下面的程式碼所示:

// Creating an Alert
Alert alert = new Alert(AlertType.INFORMATION);

步驟 2:設定警報框標題

使用 setTitle() 方法為警報框設定合適的標題,如下面的程式碼塊所示:

// setting the title of alert box
alert.setTitle("Alert Box");

步驟 3:設定警報框標題欄文字

setHeaderText() 方法用於設定警報框的標題欄文字。我們使用下面的程式碼塊將標題欄文字設定為“null”:

// setting header text 
alert.setHeaderText(null);

步驟 4:設定警報框內容文字

要設定警報框的內容文字,請使用 setContentText() 方法。它接受 String 型別的文字,如下面的程式碼所示:

// setting content text
alert.setContentText("Showing an Alert in JavaFX!");

步驟 5:啟動應用程式

建立警報框並設定其屬性後,建立一個按鈕,單擊該按鈕將顯示警報框。接下來,透過將 Button 物件傳遞給其建構函式來定義佈局面板,例如 VBox 和 HBox。然後,設定 SceneStage。最後,使用 launch() 方法啟動應用程式。

示例

下面的 JavaFX 程式演示瞭如何在 JavaFX 應用程式中生成警報框。將此程式碼儲存在名為 ShowAlert.java 的檔案中。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.geometry.Pos;
public class ShowAlert extends Application {
   @Override
   public void start(Stage stage) {
      // Creating a Label
      Label label = new Label("On clicking the below button, it will display an alert....");
      // Creating a Button
      Button button = new Button("Show Alert");
      // Creating an Alert
      Alert alert = new Alert(AlertType.INFORMATION);
      alert.setTitle("Alert Box");
      alert.setHeaderText(null);
      alert.setContentText("Showing an Alert in JavaFX!");
      // Setting the Button's action
      button.setOnAction(e -> alert.showAndWait());
      // Create a VBox to hold the Label and Button
      VBox vbox = new VBox(label, button);
      vbox.setAlignment(Pos.CENTER); 
      // Create a Scene with the VBox as its root node
      Scene scene = new Scene(vbox, 400, 300);
      // Set the Title of the Stage
      stage.setTitle("Alert in JavaFX");
      // Set the Scene of the Stage
      stage.setScene(scene);
      // Display the Stage
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

要從命令提示符編譯和執行儲存的 Java 檔案,請使用以下命令:

javac --module-path %PATH_TO_FX% --add-modules javafx.controls ShowAlert.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls ShowAlert

輸出

執行上述程式碼時,將生成以下輸出。

Alert Output

JavaFX 中的警報框型別

Alert 類是 Dialog 類的子類,它支援許多預構建的對話方塊(或警報框)型別,即確認、警告、資訊、錯誤和無。透過更改 Alert.AlertType 列舉的值,我們可以使用這些不同的警報框型別。

示例

在下面的示例中,我們將演示 JavaFX 中的警報框型別。將此程式碼儲存在名為 JavafxAlert.java 的檔案中。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
public class JavafxAlert extends Application {
   @Override
   public void start(Stage stage) {
      // Creating a Label
      Label label = new Label("Types of alert in JavaFX...");
      // Creating Buttons
      Button cnfrmButtn = new Button("Confirm");
      Button infrmButtn = new Button("Inform");
      Button warngButtn = new Button("Warning");
      Button errorButtn = new Button("Error");
      // Creating information Alert and setting Button's action
      Alert infrmAlert = new Alert(AlertType.INFORMATION);
      infrmAlert.setContentText("It is an Information Alert!");
      infrmButtn.setOnAction(e -> infrmAlert.showAndWait());
      // Creating confirmation Alert and setting Button's action
      Alert cnfrmAlert = new Alert(AlertType.CONFIRMATION);
      cnfrmAlert.setContentText("It is a Confirmation Alert!");
      cnfrmButtn.setOnAction(e -> cnfrmAlert.showAndWait());
      // Creating warning Alert and setting Button's action
      Alert warngAlert = new Alert(AlertType.WARNING);
      warngAlert.setContentText("It is a Warning Alert!");
      warngButtn.setOnAction(e -> warngAlert.showAndWait());
      // Creating error Alert and setting Button's action
      Alert errorAlert = new Alert(AlertType.ERROR);
      errorAlert.setContentText("It is an Error Alert!");
      errorButtn.setOnAction(e -> errorAlert.showAndWait());
      // Create a HBox to hold the Buttons
      HBox box = new HBox(cnfrmButtn, infrmButtn, warngButtn, errorButtn);
      box.setAlignment(Pos.CENTER); 
      box.setPadding(new Insets(15));
      box.setSpacing(10);
      // Create a VBox to hold the Label and Button
      VBox vbox = new VBox(label, box);
      vbox.setAlignment(Pos.CENTER); 
      // Create a Scene with the VBox as its root node
      Scene scene = new Scene(vbox, 400, 300);
      // Set the Title of the Stage
      stage.setTitle("Alert in JavaFX");
      // Set the Scene of the Stage
      stage.setScene(scene);
      // Display the Stage
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

使用以下命令從命令提示符編譯和執行儲存的 Java 檔案:

javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxAlert.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxAlert

輸出

執行上述程式碼後,它將生成一個顯示四個按鈕的視窗。每個按鈕都與不同的警報框相關聯。

Alert Output2
廣告
© . All rights reserved.