我有一个名为 GUI 的类来管理我的应用程序。当用户想在我的程序中删除他的帐户时,我希望弹出一个警告框,要求他确认或取消他的操作,以免他不小心删除他的帐户。
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 类中时发生了什么。用户点击是还是否?所以我想到了将显示方法设为 boolean 值。这就是问题所在,我的事件监听器表达式无法返回任何值,因为它在类型为 void 的回调中。然后我想,“哦,我会让 close 方法返回一个 boolean 值”。但是后来我想起来我原来调用的函数是:
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 方法。我也不能只调用 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() 返回一个 boolean 值。因此,您可以通过在 if 条件中进行比较来检查用户是否单击了 ButtonType.OK
或 ButtonType.CANCEL
。
使用默认的警报,您会得到两个按钮,一个是确定按钮,另一个是取消按钮。但是如果你想自定义警报,你可以像下面这样添加你自己的按钮类型:
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"
}
关于java - 获取显示方法以返回 boolean 值? - JavaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33889551/