2016-01-18 91 views
0

首先,請不要將此問題標記爲重複&而不會經歷整個問題。我已經搜索了類似於我的問題,但找不到任何問題。所以我請求你指導我到有類似問題的帖子&然後關閉它。按鈕操作不重複按下按鈕JavaFX

現在的問題。

我在JavaFX應用程序中創建了一個彈出窗口。我在主窗口/舞臺上有兩個按鈕,按任何一個都會生成一個新窗口/舞臺。但是,當關閉新生成的彈出窗口並再次在原始窗口上按下這些按鈕時,將導致JavaFX應用程序線程異常。

下面是按鈕&與之相關的操作代碼&是在主窗口

public class FirstScene 
{ 
    //.....usual code.....// 
    //Creating the buttons 
    Button leftClick = new Button("",lftIcon); 
    Button rightClick = new Button("", rghtIcon); 

    //Adding ACTION to the buttons 
    add.setOnAction(ae->LoginFunc.loginHandler("add")); 
    remove.setOnAction(ae ->LoginFunc.loginHandler("remove")); 

下面是處理該事件&是在彈出的LoginFunc代碼窗口

public class LoginFunc 
{ 
    private static String userID, password, button; 
    private static Stage loginStage = new Stage(); 
    static Button login = new Button("Login"); 
    Scene addScene, removeScene; 

    public static void loginHandler(String bttn) 
    { 
     button = bttn; 
     loginStage.setTitle("Movie Database Login"); 

     loginStage.setMaxHeight(400); 
     loginStage.setMaxWidth(400); 

     GridPane loginLayout = new GridPane(); 
     loginLayout.getChildren().add(login); 

     Scene loginScene = new Scene(loginLayout,400,400); 

     login.setOnAction(eh -> ButtonClicked(eh)); 


     loginStage.setScene(loginScene); 
     loginStage.initStyle(StageStyle.UTILITY); 
     loginStage.initModality(Modality.APPLICATION_MODAL);   

     loginStage.show(); 

    } 

    private static void ButtonClicked (ActionEvent eh) 
    { 
     if(button == "add") 
     { 
      FirstScene.mainStage.setTitle("Add Window"); 
      loginStage.close(); 

     } 
     if(button == "remove") 
     { 
      FirstScene.mainStage.setTitle("Remove Window"); 
      loginStage.close(); 

     } 
    } 
} 

問題是,一旦我任何按鈕再次關閉新生成的彈出窗口&機,它會導致以下異常:

Exception in thread "JavaFX Application Thread" java.lang.IllegalStateException: Cannot set style once stage has been set visible 
    at javafx.stage.Stage.initStyle(Unknown Source) 
    at MovieDataBase.LoginFunc.loginHandler(LoginFunc.java:34) 
    at MovieDataBase.FirstScene.lambda$0(FirstScene.java:101) 
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source) 
    at javafx.event.Event.fireEvent(Unknown Source) 
    at javafx.scene.Node.fireEvent(Unknown Source) 
    at javafx.scene.control.Button.fire(Unknown Source) 
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source) 
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source) 
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source) 
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source) 
    at javafx.event.Event.fireEvent(Unknown Source) 
    at javafx.scene.Scene$MouseHandler.process(Unknown Source) 
    at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source) 
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source) 
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(Unknown Source) 
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source) 
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source) 
    at com.sun.glass.ui.View.notifyMouse(Unknown Source) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

我希望能夠在每次按下按鈕時,重複相同的窗口。

對不起,如果這是一個非常簡單的問題&解決方案已經存在,我是一個初學者在Java &我的搜索導致什麼,我找不到與我有關的單個問題。

感謝您的寶貴時間&輸入

+1

不相關的問題,但東西你需要閱讀:HTTP://計算器。com/questions/513832/how-do-i-compare-strings-in-java總之,不要使用'=='來比較字符串值。 – VGR

回答

0

你的方法調用LoginFunc是靜態的,它使用了類的靜態成員。這意味着,當您第一次運行該方法時,它們將被初始化並進行設置。第二次調用該方法時,它們已經初始化。有一些方法(initStyle就是其中之一),只能在對象的生命週期中調用一次。

但是有一個簡單的解決方案:從LoginFunc中刪除靜態修飾符,然後在調用方法loginHandler之前在事件處理函數中創建實例,但保留對它的引用,以便您可以將其傳遞給remove方法。 或者甚至更好地分離用於添加和移出LoginFunc的邏輯,因爲在輸入參數上觸發行爲不是一個好的決定。

您可以有一個工廠方法,它將爲add和remove提供事件處理程序,並使用布爾值來決定工廠方法應該返回哪個事件處理程序。

public EventHandler<MouseEvent> createButtonClickEventHandler(boolean add) { 
    ... 
} 

然後你就可以實現這樣的按鈕:

EventHandler<MouseEvent> addEventHandler = creatButtonClickEventHandler(true); 
add.setOnAction(ae->new LoginFunc().loginHandler(addEventHandler)); 

EventHandler<MouseEvent> removeEventHandler = creatButtonClickEventHandler(false); 
remove.setOnAction(ae->new LoginFunc().loginHandler(removeEventHandler)); 
+0

當我嘗試實現'public EventHandler createButtonClickEventHandler(booloean add)'時,它顯示我必須返回一個EventHandler類型的變量。但我不希望它返回任何東西。我只是希望它執行一些計算,但它一直給出這個警告。當我嘗試返回'EventHandler evt'時,它顯示'evt'必須被初始化。 – Auro