英文:
JavaFX: stage.setOnCloseRequest() not working
问题
我有一个主窗口,在其上打开一个错误对话框。此时,在主窗口和错误对话框(主窗口舞台的一部分)之间显示了一个覆盖层。<br/>
`mainWindowOverlay.setManaged(true)`
当错误对话框关闭时,覆盖层应该消失。<br/>
`errorStage.setOnCloseRequest(we -> overlayOff())`
我不知道是否注册了关闭事件,或者是否还有其他问题,但是错误对话框关闭后,覆盖层仍然保留。
有什么想法?
**controllerMainWindow.java**
```java
package packageA;
import [...];
public class controllerMainWindow extends AbstractController
{
@FXML
private AnchorPane mainWindowOverlay;
@FXML
private Button btnError;
//---------------------------------------------------------------------------------------------------
public void initialize()
{
mainWindowOverlay.setManaged(false);
}
//---------------------------------------------------------------------------------------------------
@FXML
public void handleButtonAction(ActionEvent event)
{
try {
if (event.getSource().equals(btnError)) {
FXMLLoader errorLoader = new FXMLLoader();
errorLoader.setLocation(getClass().getResource("errorDialog.fxml"));
controllerErrorDialog errorController = new controllerErrorDialog();
errorLoader.setController(errorController);
Parent layout;
layout = errorLoader.load();
Scene errorScene = new Scene(layout);
Stage errorStage = new Stage();
errorStage.initStyle(StageStyle.UNDECORATED);
errorController.setStage(errorStage);
if(this.main!=null) {
errorStage.initOwner(main.getPrimaryStage());
}
customerServiceOverlay.setManaged(true);
errorStage.initModality(Modality.APPLICATION_MODAL);
errorStage.setScene(errorScene);
errorStage.setX(750);
errorStage.setY(425);
errorStage.setOnCloseRequest(we -> overlayOff());
errorStage.showAndWait();
}
} catch (IOException exceptionHandleButtonAction) {
System.out.println("在 handleButtonAction 中出错。");
exceptionHandleButtonAction.printStackTrace();
return;
}
}
//---------------------------------------------------------------------------------------------------
public void overlayOff()
{
customerServiceOverlay.setManaged(false);
}
//---------------------------------------------------------------------------------------------------
}
controllerErrorDialog.java
package packageA;
import [...];
public class controllerErrorDialog extends AbstractController implements Initializable {
@FXML
private Button btnClose;
@FXML
private Stage stage = null;
//---------------------------------------------------------------------------------------------------
@Override
public void initialize(URL url, ResourceBundle rb)
{
}
//---------------------------------------------------------------------------------------------------
public void setStage(Stage stage)
{
this.stage = stage;
}
//---------------------------------------------------------------------------------------------------
@FXML
public void handleButtonAction(ActionEvent event)
{
stage.fireEvent(new javafx.stage.WindowEvent(stage,
javafx.stage.WindowEvent.WINDOW_CLOSE_REQUEST));
stage.close();
}
//---------------------------------------------------------------------------------------------------
}
英文:
I have a main window on top of which an error dialog opens. At that point an overlay is displayed between the main window and the error dialog (part of the main window stage).<br/>
mainWindowOverlay.setManaged(true)
When the error dialog is closed, the overlay should disappear.<br/>
errorStage.setOnCloseRequest(we -> overlayOff())
I don't know if the close event isn' t registered or if there's another problem but the overlay stays after the error dialog has been closed.
Any ideas?
controllerMainWindow.java
package packageA;
import [...];
public class controllerMainWindow extends AbstractController
{
@FXML
private AnchorPane mainWindowOverlay;
@FXML
private Button btnError;
//---------------------------------------------------------------------------------------------------
public void initialize()
{
mainWindowOverlay.setManaged(false);
}
//---------------------------------------------------------------------------------------------------
@FXML
public void handleButtonAction(ActionEvent event)
{
try {
if (event.getSource().equals(btnError)) {
FXMLLoader errorLoader = new FXMLLoader();
errorLoader.setLocation(getClass().getResource("errorDialog.fxml"));
controllerErrorDialog errorController = new controllerErrorDialog();
errorLoader.setController(errorController);
Parent layout;
layout = errorLoader.load();
Scene errorScene = new Scene(layout);
Stage errorStage = new Stage();
errorStage.initStyle(StageStyle.UNDECORATED);
errorController.setStage(errorStage);
if(this.main!=null) {
errorStage.initOwner(main.getPrimaryStage());
}
customerServiceOverlay.setManaged(true);
errorStage.initModality(Modality.APPLICATION_MODAL);
errorStage.setScene(errorScene);
errorStage.setX(750);
errorStage.setY(425);
errorStage.setOnCloseRequest(we -> overlayOff());
errorStage.showAndWait();
}
} catch (IOException exceptionHandleButtonAction) {
System.out.println("Error on handleButtonAction.");
exceptionHandleButtonAction.printStackTrace();
return;
}
}
//---------------------------------------------------------------------------------------------------
public void overlayOff()
{
customerServiceOverlay.setManaged(false);
}
//---------------------------------------------------------------------------------------------------
}
controllerErrorDialog.java
package packageA;
import [...];
public class controllerErrorDialog extends AbstractController implements Initializable {
@FXML
private Button btnClose;
@FXML
private Stage stage = null;
//---------------------------------------------------------------------------------------------------
@Override
public void initialize(URL url, ResourceBundle rb)
{
}
//---------------------------------------------------------------------------------------------------
public void setStage(Stage stage)
{
this.stage = stage;
}
//---------------------------------------------------------------------------------------------------
@FXML
public void handleButtonAction(ActionEvent event)
{
stage.fireEvent(new javafx.stage.WindowEvent(stage,
javafx.stage.WindowEvent.WINDOW_CLOSE_REQUEST));
stage.close();
}
//---------------------------------------------------------------------------------------------------
}
答案1
得分: 0
由@James_D和@anko提供的解决方案。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论