gpt4 book ai didi

java - 如何在另一个已激活时加载 FXML?

转载 作者:行者123 更新时间:2023-11-29 05:31:29 25 4
gpt4 key购买 nike

我为我的 Java 项目使用了 Oracle FXML 示例。问题是我有 2 个 FXML 文件(load.fxml、main.fxml)。我首先加载 load.fxml,然后想按下一个按钮触发一个方法 (handleLoadButton()) 来加载 main.fxml。但这不起作用。我收到错误“JavaFX 应用程序线程”。所以也许我是个白痴,但我现在有点沮丧,因为我找不到任何解决方案。我将如何加载 main.fxml?

到目前为止我的代码:

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

private Stage myStage;

public static void main(String[] args) {
Application.launch(Main.class, args);
}

@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("load.fxml"));

stage.setTitle("FXML Welcome");
stage.setScene(new Scene(root, 300, 275));
stage.show();

myStage = stage;
}

@FXML
private void handleLoadButton() throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));

myStage.setTitle("FXML Main");
myStage.setScene(new Scene(root, 300, 275));
myStage.show();
}
}

我得到的错误:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1449)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Node.fireEvent(Node.java:6867)
at javafx.scene.control.Button.fire(Button.java:179)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)
at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3311)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3151)
at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3106)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2248)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
at com.sun.glass.ui.View.handleMouseEvent(View.java:530)
at com.sun.glass.ui.View.notifyMouse(View.java:924)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:89)
at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:75)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:279)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1446)
... 43 more
Caused by: java.lang.IllegalStateException: Application launch must not be called more than once
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:94)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:75)
at javafx.application.Application.launch(Application.java:148)
at program.Main.handleLoadButton(Main.java:35)
... 53 more

嘿,谢谢大家帮助我!

最佳答案

您的问题是,您将 Application 类用作 Controller 。这意味着 FXMLLoader 尝试创建另一个 Main 实例。您必须为 Controller 创建一个不同的类。

修改后的主类:

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

private Stage myStage;

public static void main(String[] args) {
Application.launch(Main.class, args);
}


@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader();

// this creates a instance of Controller now
// and not a instance of Main
Parent root = (Parent) loader.load(getClass().getResourceAsStream("load.fxml"));

// get the controller corresponding to root
Controller controller = loader.getController();

// tell the controller that this is it's Main class
controller.setMain(this);

stage.setTitle("FXML Welcome");
stage.setScene(new Scene(root, 300, 275));
stage.show();

myStage = stage;
}

public void handleLoadButton() throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));

myStage.setTitle("FXML Main");
myStage.setScene(new Scene(root, 300, 275));
myStage.show();
}
}

Controller 类:

import java.io.IOException;
import javafx.fxml.FXML;

public class Controller {

@FXML
private void handleLoadButton() throws IOException {
myMain.handleLoadButton();
}

private Main myMain;

public void setMain(Main myMain) {
this.myMain = myMain;
}

}

当然要替换load.fxml中的controller类:

文件根节点中的fx:controller属性应该是:

fx:controller="Controller"

如果您不使用默认包,则可能需要添加包名称。

关于java - 如何在另一个已激活时加载 FXML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20949899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com