我有一个名为GUI的类来管理我的应用程序.当用户想要在我的程序中删除他的帐户时,我想要弹出警告框并要求他确认或取消他的操作,因此他不会意外删除他的帐户. package application; impo
          package application;
 import javafx.geometry.Pos;
 import javafx.scene.Scene;
 import javafx.scene.control.Button;
 import javafx.scene.control.Label;
 import javafx.scene.layout.HBox;
 import javafx.scene.layout.VBox;
 import javafx.stage.*;
 /**
  * An Alert Box for the GUI
  * to display errors to the user.
  *
  */
public class AlertBox
{   
Stage window;
public boolean display(String title, String message)
{       
    boolean cancel = false;
    window = new Stage();                                                               // Create the stage.        
    window.initModality(Modality.APPLICATION_MODAL);                                    // If window is up, make user handle it.
    window.setTitle(title);
    window.setMinHeight(350);
    window.setMinWidth(250);
    VBox root = new VBox();                 // Root layout.
    Label warning = new Label(message);     // Message explaining what is wrong.
    HBox buttonLayout = new HBox();         // The yes and cancel button.
    Button yesButton = new Button("Yes");   // Yes button for the user.
    Button noButton = new Button("No");     // No button for the user.
    yesButton.setOnAction(e ->
    {
        cancel = false;
    });
    noButton.setOnAction(e ->
    {
        cancel = true;
    });
    buttonLayout.getChildren().addAll(yesButton, noButton);
    root.getChildren().addAll(warning, buttonLayout);
    root.setAlignment(Pos.CENTER);
    Scene scene = new Scene(root);
    window.setScene(scene);
    window.show();
}
/**
 * Closes the window and returns whether the user said yes or no.
 * @param variable
 * @return
 */
private boolean close(boolean variable)
{
    window.close();
    return variable;
} 
 }
我希望我的GUI类能够准确地知道用户在AlertBox类中发生了什么.用户是否单击是或否?所以我考虑将display方法设为boolean.这就是问题所在,我的事件监听器表达式不能返回任何值,因为它位于void类型的回调中.然后我想,“哦,我只是让close方法返回一个布尔值”.但后来我记得原来我调用的函数是:
AlertBox warning = new AlertBox;
boolean userWantsToDelete = warning.display("Warning!", "You are about to delete your account. Are you sure you would like to do this?"); 
 它希望display方法返回一个变量,而不是close方法.我不能只是打电话给关闭,因为这不起作用.我该怎么做才能解决这个问题?非常感谢你.
您可以使用JavaFX警报轻松执行任务:Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
    alert.setTitle("Confirmation Dialog");
    alert.setHeaderText("Warning !");
    alert.setContentText("Are you sure you want to perform this action ?");
    Optional<ButtonType> result = alert.showAndWait();
    if (result.get() == ButtonType.OK) {
     // delete user
 } 
 result.get()返回一个布尔值.因此,您可以通过在if条件中进行比较来检查用户是否单击了ButtonType.OK或ButtonType.CANCEL.
以下是您可以了解的示例:
使用默认警报,您将获得两个按钮,一个是OK按钮,另一个是取消.但是,如果您希望自定义警报,您可以将自己的按钮类型添加到其中,如下所示:
ButtonType buttonTypeOne = new ButtonType("One");
ButtonType buttonTypeTwo = new ButtonType("Two");
ButtonType buttonTypeThree = new ButtonType("Three"); 
 并将它们添加到警报中:
alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo, buttonTypeThree);
因此,您可以检查用户按下了哪个按钮
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == buttonTypeOne){
    // ... user chose "One"
} else if (result.get() == buttonTypeTwo) {
    // ... user chose "Two"
} else{
    // ... user chose "Three"
}
        
             