JavaFX: `stage.setOnCloseRequest()`不起作用

huangapple go评论96阅读模式
英文:

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 -&gt; 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(&quot;errorDialog.fxml&quot;));
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 -&gt; overlayOff());
errorStage.showAndWait();
}
} catch (IOException exceptionHandleButtonAction) {
System.out.println(&quot;Error on handleButtonAction.&quot;);
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提供的解决方案。

JavaFX: `stage.setOnCloseRequest()`不起作用

英文:

Solution provided by @James_D and @anko.

JavaFX: `stage.setOnCloseRequest()`不起作用

huangapple
  • 本文由 发表于 2020年5月29日 19:21:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/62084781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定