就像标题一样,我的应用程序有问题。该应用程序应该在全屏模式下运行(无意切换回窗口模式),因此我设计了一个页脚栏,其中包含一些图像(带有标签,在 VBox 中),以便用户可以导航或退出程序。
因此,启动应用程序后,所有按钮都可以正常触摸。甚至我的页脚栏中的退出按钮也通过打开我的自定义对话框正确响应。但这开始了我的问题。对话框通过 showAndWait() 方法调用显示,但不响应触摸事件。相反,鼠标事件仍然被处理(我仍然可以使用鼠标单击对话框中的按钮并且对话框正确响应)。
我希望有人知道我做错了什么。
MyDialog.java:
public static boolean showExitDialog(Window owner, ResourceBundle resources) {
LOGGER.info("Showing exit dialog...");
final Dialog<ButtonType> dialog = new Dialog<ButtonType>();
dialog.getDialogPane().getStylesheets().add(MyDialog.getInstace().getCssPath());
dialog.setContentText(resources.getString("label.exitdialog.text"));
dialog.setHeaderText(resources.getString("label.exitdialog.header"));
dialog.initOwner(owner);
dialog.initStyle(StageStyle.TRANSPARENT);
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.getDialogPane().getButtonTypes().add(new ButtonType(resources.getString("btn.Exitdialog.exit"), ButtonData.OK_DONE););
dialog.getDialogPane().getButtonTypes().add(new ButtonType(resources.getString("btn.Exitdialog.cancel"), ButtonData.FINISH));
Optional<ButtonType> result = dialog.showAndWait();
LOGGER.debug("Result: {}", result.get());
if(result.isPresent() && result.get().getButtonData() == ButtonData.OK_DONE) {
LOGGER.info("Closing exit dialog returning true...");
return true;
} else {
LOGGER.info("Closing exit dialog returning false...");
return false;
}
}
在MainApp.java中:
private EventHandler<WindowEvent> confirmCloseEventHandler = event -> {
// close event handling logic.
// consume the event if you wish to cancel the close operation.
if(MyDialog.showExitDialog(primaryStage, rb)) {
event.consume();
System.exit(0);
}
};
...
primaryStage.setOnCloseRequest(confirmCloseEventHandler);
在FooterBar.java中:
@FXML
private void exitProgramPressedTouch(TouchEvent event) {
event.consume();
controller.getWindow().fireEvent(new WindowEvent(controller.getWindow(), WindowEvent.WINDOW_CLOSE_REQUEST));
}
*编辑*哦完全忘记了:没有抛出异常或其他任何东西。
最佳答案
我不知道所描述行为的原因 - 也许是一个错误。但是,您可以尝试监听 ActionEvent
而不是 TouchEvent
。它处理触摸和鼠标事件:
@FXML
private void exitProgramPressedTouch(ActionEvent event) {
event.consume();
controller.getWindow().fireEvent(new WindowEvent(controller.getWindow(), WindowEvent.WINDOW_CLOSE_REQUEST));
}
也许您还需要更改 FXML 文件中绑定(bind)事件监听器的属性(从 onTouch
到 onAction
)。
最后,我认为,如果您仅在单击取消按钮时使用关闭事件,则可以避免 System.exit(0);
:
if(!MyDialog.showExitDialog(primaryStage)) {
event.consume();
}
关于java - 显示自定义对话框后应用程序不响应触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35501941/