'Morning. 我只是在java FX方面遇到了一个小问题。 问题是这样的:我想写一个代码,并在使用时弹出一个弹出窗口,通过默认的关闭按钮关闭应用程序。
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
Stage window;
Button button;
boolean answer=false;
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
window=primaryStage;
window.setTitle("Homepage");
window.setOnCloseRequest(e-> {
e.consume();
close();
});
StackPane layout = new StackPane();
layout.getChildren().add(button);
Scene scene = new Scene(layout, 300,300);
window.setScene(scene);
window.show();
}
private void close(){ answer = ConfirmationBox.display("close app", "'sure about that?");
if(answer) {
System.out.println(answer);
window.close();
}
}
public static void main(String[] args) {
launch(args);
}
}
每次我按下默认的 kill 应用程序按钮时,都会弹出一条消息,询问我是否要关闭应用程序
public static boolean display(String titolo,String messaggio){
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle(titolo);
Label label=new Label();
label.setText(messaggio);
Button yButton = new Button("yes");
Button nButton = new Button("no");
yButton.setOnAction(e-> {
flag=true;
window.close();
});
nButton.setOnAction(e-> {
flag=false;
window.close();
});
VBox layout = new VBox(20);
layout.minHeight(250);
layout.getChildren().addAll(label,yButton,nButton);
layout.setAlignment(Pos.CENTER);
Scene scene = new Scene(layout,300,300);
window.setScene(scene);
window.show();
return flag;
问题是,当我第一次运行该方法时,布尔变量"answer"并没有正确保存结果,因此 close(( 无法在 if 的情况下重新输入。 如何防止这种情况?我是否错过了一些JavaFX功能?我对javaFX和Java本身很陌生。
尝试这样的事情。
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Exit Application");
alert.setContentText("Do you want to exit?");
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent() && result.get() != ButtonType.OK) {
event.consume();
}
}
});
}
Window.show()
方法显示窗口并立即返回。因此您的代码
public static boolean display(String titolo,String messaggio){
// ...
window.show();
return flag;
}
将显示窗口并立即返回当前值flag
(最初false
(。
如果用户按下yButton
,则标志设置为true
,下次他们尝试关闭主窗口时,该方法立即返回true
并且主窗口关闭。
您可以改用方法window.showAndWait()
"显示此阶段并等待其隐藏(关闭(后再返回调用方"。
public static boolean display(String titolo,String messaggio){
// ...
// window.show();
window.showAndWait();
return flag;
}
正如其他答案所指出的那样,还有一个Dialog
API,包括一个方便Alert
类。Dialog
提供普通Stage
没有的主要功能是从showAndWait()
方法返回值的功能。在您的情况下,您希望返回一个boolean
,因此基于Dialog
的方法版本如下所示:
public static boolean display(String titolo,String messaggio){
Dialog<Boolean> dialog = new Dialog<>();
dialog.setTitle(titolo);
dialog.getDialogPane().setContentText(messaggio);
dialog.getDialogPane().getButtonTypes().addAll(
ButtonType.YES, ButtonType.NO);
// Result of dialog is true if button is YES button,
// false otherwise
dialog.setResultConverter(button -> button == ButtonType.YES);
// return result of dialog, or false if there is no result
// (i.e. if user closes dialog without pressing a button)
return dialog.showAndWait().orElse(false);
}
我自己是Java的新手,所以我完全理解你现在的情况。
我猜的是,您的主要方法在确认对话框出现后继续运行,因此第一次答案仍然是错误的。也许您必须在用户仍在考虑是否关闭程序时保持主方法等待。
如果 ConfirmationBox 类扩展了 DialogPane 类,则可以使用方法window.showAndWait()
而不是在display()
中使用window.show()
。我认为这应该行得通。
希望对您有所帮助!
编辑:实际上,我刚刚意识到此解决方案与ConfirmationBox是否扩展DialogPane无关,因为window
是一个Stage对象并且已经包含showAndWait()
方法。