用户尝试在JavaFx中使用setOnCloseRequest关闭应用程序时的警报框 [英] Alert Box For When User Attempts to close application using setOnCloseRequest in JavaFx
问题描述
我试图提示用户确认他们想在退出前关闭程序。如果任务仍在执行,我想确认仍然希望退出或让他们有机会在退出之前完成任务。我使用了setOnCloseRequest,但它没有用。我使用了event.consume,似乎只是禁用了[x]按钮。任何建议表示赞赏。我发现这里有一个类似的问题对我不起作用 - >
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control。*;
import javafx.scene.layout.StackPane;
import javafx.stage。*;
import javafx.stage.WindowEvent;
import java.util.Optional;
公共类CloseConfirm扩展Application {
private Stage mainStage;
@Override
public void start(阶段阶段)抛出异常{
this.mainStage = stage;
stage.setOnCloseRequest(confirmCloseEventHandler);
按钮closeButton = new按钮(关闭应用程序);
closeButton.setOnAction(event - >
stage.fireEvent(
new WindowEvent(
stage,
WindowEvent.WINDOW_CLOSE_REQUEST
)
)
);
StackPane layout = new StackPane(closeButton);
layout.setPadding(new Insets(10));
stage.setScene(新场景(布局));
stage.show();
}
private EventHandler< WindowEvent> confirmCloseEventHandler = event - > {
警报closeConfirmation =新警报(
Alert.AlertType.CONFIRMATION,
你确定要退出吗?
);
Button exitButton =(Button)closeConfirmation.getDialogPane()。lookupButton(
ButtonType.OK
);
exitButton.setText(退出);
closeConfirmation.setHeaderText(确认退出);
closeConfirmation.initModality(Modality.APPLICATION_MODAL);
closeConfirmation.initOwner(mainStage);
//通常情况下,您只需使用默认的警报定位,
//但是对于这个简单的示例,主阶段很小,
//所以明确定位警报所以主窗口仍然可以看到。
closeConfirmation.setX(mainStage.getX());
closeConfirmation.setY(mainStage.getY()+ mainStage.getHeight());
可选< ButtonType> closeResponse = closeConfirmation.showAndWait();
if(!ButtonType.OK.equals(closeResponse.get())){
event.consume();
}
};
public static void main(String [] args){
launch(args);
}
}
答案确实使用 event.consume()
,你说你试过并且没有为你工作(虽然我不知道为什么不能,因为这对我来说这个示例代码很合适)。
I am trying to prompt the user to confirm they want to close a program before exiting. In the event a task is still being executed, I wanted to confirm that the still wish to exit or give them a chance to allow the task to finish before exiting. I used setOnCloseRequest, but it did not work. Ive used event.consume which just seemed to disable the [x] button. Any suggestions are appreciated. I found a similar question here that did not work for me -> JavaFX stage.setOnCloseRequest without function?
Public class Sample extends Application {
@Override
public void start(Stage stage) throws Exception {
stage.setOnCloseRequest(event -> {
NewScanView checkScan = new NewScanView();
boolean isScanning = checkScan.isScanning();
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Close Confirmation");
alert.setHeaderText("Cancel Creation");
alert.setContentText("Are you sure you want to cancel creation?");
if (isWorking == true){
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
stage.close();
}
if(result.get()==ButtonType.CANCEL){
alert.close();
}
}
});
stage.setHeight(600);
stage.setWidth(800);
stage.setTitle("Title");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This answer is based on the answer to Javafx internal close request. That question is different from this question, but the answer is very similar.
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.stage.*;
import javafx.stage.WindowEvent;
import java.util.Optional;
public class CloseConfirm extends Application {
private Stage mainStage;
@Override
public void start(Stage stage) throws Exception {
this.mainStage = stage;
stage.setOnCloseRequest(confirmCloseEventHandler);
Button closeButton = new Button("Close Application");
closeButton.setOnAction(event ->
stage.fireEvent(
new WindowEvent(
stage,
WindowEvent.WINDOW_CLOSE_REQUEST
)
)
);
StackPane layout = new StackPane(closeButton);
layout.setPadding(new Insets(10));
stage.setScene(new Scene(layout));
stage.show();
}
private EventHandler<WindowEvent> confirmCloseEventHandler = event -> {
Alert closeConfirmation = new Alert(
Alert.AlertType.CONFIRMATION,
"Are you sure you want to exit?"
);
Button exitButton = (Button) closeConfirmation.getDialogPane().lookupButton(
ButtonType.OK
);
exitButton.setText("Exit");
closeConfirmation.setHeaderText("Confirm Exit");
closeConfirmation.initModality(Modality.APPLICATION_MODAL);
closeConfirmation.initOwner(mainStage);
// normally, you would just use the default alert positioning,
// but for this simple sample the main stage is small,
// so explicitly position the alert so that the main window can still be seen.
closeConfirmation.setX(mainStage.getX());
closeConfirmation.setY(mainStage.getY() + mainStage.getHeight());
Optional<ButtonType> closeResponse = closeConfirmation.showAndWait();
if (!ButtonType.OK.equals(closeResponse.get())) {
event.consume();
}
};
public static void main(String[] args) {
launch(args);
}
}
The answer does use event.consume()
, which you said you tried and did not work for you (though I'm not sure why not as it works fine for me with this sample code).
这篇关于用户尝试在JavaFx中使用setOnCloseRequest关闭应用程序时的警报框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!