提问者:小点点

如何在JavaFX中正确显示第二个fxml文件视图?


我是JavaFX和SceneBuilder的新手。我已经为如何直接从我的控制器正确显示我的应用程序的第二个视图困扰了一段时间。(如果用户单击NEXT按钮,则显示这个fxml文件)这是我到目前为止尝试的内容:

package application.controller;
import java.io.IOException;

import application.Main;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class controller1 implements EventHandler<ActionEvent> {

    private Stage window;
    private AnchorPane anchorPane;
    FXMLLoader loader = new FXMLLoader();
    
    @FXML
    private Button whoami;
    @FXML
    private Button next;
    @FXML
    private Label where;
    
    @Override
    public void handle(ActionEvent event) {
        // TODO Auto-generated method stub
            
            where.setText("You're in view #1");    // When running my program, this does work
    }
    
    public void event2(ActionEvent event) throws IOException {
        // TODO Auto-generated method stub
        
            
            where.setText("NEXT was selected.");   // This second button also print to the console
            
            /* Here is where I am a little lost, 
            // after this part, my program gets an error starting with
            // Exception in thread "JavaFX Application Thread"
            // I did this approach for my start method in Main.java, and view 1 gets displayed
            // I am happy to learn from any feedback  */

            window.setTitle("View #2!!!!");
            
            loader.setLocation(Main.class.getResource("view/view2.fxml"));
            anchorPane = loader.load();
            
            Scene scene2 = new Scene(anchorPane);
            window.setScene(scene2);
            window.show();  
    }
}

这是我的Main.java:

package application;

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
 
public class Main extends Application {
    private Stage window;
    private AnchorPane anchorPane;
    FXMLLoader loader = new FXMLLoader();
    
    // Sets Stage for application:
    @Override      
    public void start(Stage window) throws IOException {

        // window is what I called my primaryStage
        this.window = window;
        this.window.setTitle("View #1");
        this.window.setResizable(false);
        
        loader.setLocation(Main.class.getResource("view/view1.fxml"));
        anchorPane =loader.load();
        
        Scene scene1 = new Scene(anchorPane);
        window.setScene(scene1);
        window.show();  
    }
    
    // Main Method:
    public static void main(String[] args) {
        launch(args);
    }
    
    
}

这是错误跟踪:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: 

    java.lang.reflect.InvocationTargetException
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
        at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
        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:8411)
        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:3757)
        at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
        at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
        at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
        at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
        at com.sun.glass.ui.View.notifyMouse(View.java:937)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
        at java.lang.Thread.run(Thread.java:748)
    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:498)
        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:498)
        at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
        ... 48 more
    Caused by: java.lang.NullPointerException
        at application.controller.controller1.event2(controller1.java:48)
        ... 58 more

共1个答案

匿名用户

控制器中的窗口变量抛出< code > NullPointerException ,因为它没有引用Main.java中的实际窗口。

一个简单的解决方法是创建一个可以从控制器访问的 Stage 变量。下面是一个示例:

主要.java:

public class Main extends Application {
    private Stage window;
    private AnchorPane anchorPane;
    FXMLLoader loader = new FXMLLoader();
    protected static Stage window;

    // Sets Stage for application:
    @Override      
    public void start(Stage window1) throws IOException {
        window = window1;
        // window is what I called my primaryStage
        this.window = window;
        this.window.setTitle("View #1");
        this.window.setResizable(false);
        
        loader.setLocation(Main.class.getResource("view/view1.fxml"));
        anchorPane =loader.load();
        
        Scene scene1 = new Scene(anchorPane);
        window.setScene(scene1);
        window.show();  
    }
    
    // Main Method:
    public static void main(String[] args) {
        launch(args);
    }
}

在上面,我创建了一个window变量,该变量引用windows1,该变量是start()方法的参数。

现在要对窗口进行修改,我只需在主类中引用窗口变量就可以了。首先移除控制器中的< code>window声明。然后以< code>Main.window的形式访问Main.java中的< code>window变量。

public void event2(ActionEvent event) throws IOException {
        // TODO Auto-generated method stub
        
            
            where.setText("NEXT was selected."); 
             happy to learn from any feedback  */

            Main.window.setTitle("View #2!!!!");
            
            loader.setLocation(Main.class.getResource("view/view2.fxml"));
            anchorPane = loader.load();
            
            Scene scene2 = new Scene(anchorPane);
            Main.window.setScene(scene2);
            Main.window.show();  
    }