2016-11-06 74 views
1

即時消息有一個問題:當我點擊一個菜單項時,它顯示一個窗口,說「你確定要註銷」,並且我點擊沒有所以窗口是關閉的),但是當我再次點擊時,我menuItem它顯示我java.lang.IllegalStateException:一旦舞臺設置可見,無法設置模態。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(); 
    } 

這裏的,如果用戶點擊 「否」 我不想退出,關閉該窗口的方法

@FXML 
private void noBtn() 
{ 
    MainWindowController.logoutStage.close(); 
} 

OUTPUT:

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$createChildren$324(ContextMenuContent.java:1317) 
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$$Lambda$231/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.access$1500(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$null$141(WinApplication.java:102) 
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/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); 

回答

1

你應該分離的設置(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(); 
} 
+0

非常感謝你:)現在工作正常 –