首先,请不要将此问题标记为重复&关闭它而不经历整个问题。我搜索过与我类似的问题,但无法找到。所以我要求你指导我发现有类似问题的帖子。然后关闭它。
现在问题。
我在JavaFX应用程序中创建了一个弹出窗口。我在主窗口/舞台上有两个按钮,按下其中一个按钮会生成一个新的窗口/舞台。但是,当我关闭新生成的弹出窗口并再次按原始窗口上的那些按钮时,会导致JavaFX应用程序线程异常。
以下是按钮和代码的代码。与他们相关的行动&它位于 MAIN WINDOW :
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& S的初学者。我的搜索结果是什么,我无法找到与我相关的单个问题。
感谢您宝贵的时间和时间。输入
答案 0 :(得分:0)
您对LoginFunc
的方法调用是静态的,它使用类的静态成员。这意味着当您第一次运行该方法时,它们会被初始化和设置。第二次调用方法时,它们已经初始化。有些方法(initStyle
就是其中之一)只能在对象的生命周期中调用一次。
但是有一个简单的解决方案:从LoginFunc
中删除静态修饰符并在调用方法loginHandler
之前在事件处理程序中创建一个实例,但是保留对它的引用,所以你可以在remove方法上传递它。
或者甚至更好地分离添加和删除LoginFunc
的逻辑,因为在输入参数上触发行为不是一个好的决定。
您可以使用一个工厂方法来提供添加和删除事件处理程序,并使用布尔值来决定工厂方法应该返回哪个事件处理程序。
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));