JavaFX开启了一个新场景

时间:2015-03-16 15:31:49

标签: javafx scene

我希望我的代码可以工作,当我点击一个按钮时,会打开一个新场景,但它不起作用,我不知道为什么。

public void start(Stage primaryStage) throws Exception {
    window = primaryStage;

    Parent root = FXMLLoader.load(getClass().getResource("FXML/LoginScene.fxml"));
    scene = new Scene(root,400,400);
    openScene = new OpenScene(writer);
    window.setScene(scene);
    window.show();


}
public static void main(String[] args){
    launch(args);
}
@FXML protected void btnConnect(ActionEvent event) {
    System.out.println("hallo");
    try {
        openScene.start(window);
    } catch (Exception e) {

        e.printStackTrace();
    }
}

GUI成功显示,但是当我按下按钮时,它会抛出异常。

public class OpenScene extends Application{
    PrintWriter writer;

    @Override
    public void start(Stage window) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXML/OpenScene.fxml"));
        Scene scene =  new Scene(root, 200 ,200);
        window.setScene(scene);
        window.show();

    }
    public OpenScene(PrintWriter writer){
        this.writer = writer;
    }
}

更新

我尝试将应用程序类与控制器类分开,如下面的James_D&#39答案中所述,但我得到以下异常:

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.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.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.Node.fireEvent(Node.java:8216)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
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.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)
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.NullPointerException
at MainController.btnConnect(MainController.java:22)
... 53 more

1 个答案:

答案 0 :(得分:8)

看起来您正在尝试将主应用程序类用作控制器类。这会令人困惑,你应该避免它。以下是发生的事情:

启动应用程序时,它会调用launch(...)。继承自launch的{​​{1}}方法将执行一系列重要的"内务管理",例如启动JavaFX工具包和JavaFX应用程序线程。然后它创建一个应用程序子类的实例,创建一个初始Application并在该实例中调用Stage

start(...)方法中,您初始化了几个实例变量(start(...)window),并加载了一个FXML文件,显示其内容。 openScene方法执行以下操作:

  • 根据文件
  • 中的XML元素创建UI元素
  • 创建FXML文件中FXMLLoader.load(...)属性指定的类的实例
  • 将任何fx:controller - 带注释的字段注入控制器实例
  • 注册FXML文件中指定的所有事件处理程序

请注意,如果为应用程序和控制器指定相同的类,则将创建该类的两个实例。一个由@FXML方法创建,一个由launch创建。请注意,只有FXMLLoader方法创建的实例已调用launch方法。由于您在start(...)方法中初始化实例变量,因此这些变量不会在start(...)创建的实例中初始化。因此,在FXMLLoader("控制器实例",如果您愿意)创建的实例中,FXMLLoaderwindow未初始化。因此行

openScene

将抛出 openScene.start(window);

由于应用程序和控制器确实具有完全不同的角色,因此您应将它们分成不同的类。这将使事情变得更加混乱。请注意,您始终可以通过调用

找到显示节点的窗口
NullPointerException

因此无需缓存anyNode.getScene().getWindow(); 实例。

所以:

Stage

并为控制器使用不同的类:

public class MainApp extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        // change LoginScene.fxml so it now has fx:controller="LoginController"
        Parent root = FXMLLoader.load(getClass().getResource("FXML/LoginScene.fxml"));

        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

另请注意,您的public class LoginController { private OpenScene openScene ; @FXML private Button connectButton ; // needs fx:id in fxml file... public void initialize() throws Exception { PrintWriter writer = ... ; openScene = new OpenScene(writer); } @FXML // handler for connect button: private void btnConnect() throws Exception { Stage stage = (Stage) connectButton.getScene().getWindow(); openScene.start(stage); } } 类不需要是OpenScene子类:每个应用程序只需要一个这样的类:

Application