首先,請不要將此問題標記爲重複&而不會經歷整個問題。我已經搜索了類似於我的問題,但找不到任何問題。所以我請求你指導我到有類似問題的帖子&然後關閉它。按鈕操作不重複按下按鈕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 &我的搜索導致什麼,我找不到與我有關的單個問題。
感謝您的寶貴時間&輸入
不相關的問題,但東西你需要閱讀:HTTP://計算器。com/questions/513832/how-do-i-compare-strings-in-java總之,不要使用'=='來比較字符串值。 – VGR