java.lang.IllegalStateException:舞台设置可见后无法设置模态

java.lang.IllegalStateException: Cannot set modality once stage has been set visible

我有一个问题:当我点击一个 menuItem 时它显示 window 谁说 "are you sure you want to logout" ,我点击否(这样 window 就关闭了),但是当我再次单击它显示的 menuItem java.lang.IllegalStateException:舞台设置可见后无法设置模态。

我该怎么办?

代码如下:

MainWindowController.java

public class MainWindowController {

static Stage logoutStage = new Stage();

@FXML
private void initialize()
{

}

@FXML
public void logoutWindow() throws IOException
{
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("LogoutFXML.fxml"));
    BorderPane border = loader.load();
    Scene scene = new Scene(border);
    logoutStage.setScene(scene);
    logoutStage.setTitle("Pit Stop - Logout");
    logoutStage.initModality(Modality.WINDOW_MODAL);
    logoutStage.initOwner(MainFXMLController.mainFXMLStage);
    logoutStage.showAndWait();
  }

如果用户单击 "NO" 我不想注销

,这是关闭 window 的方法
@FXML
private void noBtn()
{
    MainWindowController.logoutStage.close();
}

输出:

 Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1762)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1364)
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren4(ContextMenuContent.java:1317)
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$$Lambda1/7286030.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3724)
    at javafx.scene.Scene$MouseHandler.access00(Scene.java:3452)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
    at com.sun.glass.ui.View.notifyMouse(View.java:925)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null1(WinApplication.java:102)
    at com.sun.glass.ui.win.WinApplication$$Lambda/29531133.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1759)
    ... 43 more
Caused by: java.lang.IllegalStateException: Cannot set modality once stage has been set visible
    at javafx.stage.Stage.initModality(Stage.java:513)
    at main.MainWindowController.logoutWindow(MainWindowController.java:43)
    ... 53 more

MainWindowController.java.43 行是这一行:

 logoutStage.initModality(Modality.WINDOW_MODAL);

您应该拆分设置 (initialize) 和控制 (logoutWindow)。

@FXML
private void initialize() {
    FXMLLoader loader = new FXMLLoader();
    loader.setLocation(getClass().getResource("LogoutFXML.fxml"));
    BorderPane border = loader.load();
    Scene scene = new Scene(border);
    logoutStage.setScene(scene);
    logoutStage.setTitle("Pit Stop - Logout");
    logoutStage.initModality(Modality.WINDOW_MODAL);
    logoutStage.initOwner(MainFXMLController.mainFXMLStage);
}

@FXML
public void logoutWindow() throws IOException {
    logoutStage.showAndWait();
}