簡體   English   中英

如何在 JavaFX 中正確顯示第二個 fxml 文件視圖?

[英]How to properly display a second fxml file view in JavaFX?

我是 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

控制器中的窗口變量拋出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變量,它引用了start()方法的參數window1變量。

現在要對窗口進行更改,我可以通過引用 Main 類中的 window 變量來簡單地進行更改。 首先刪除控制器中的window聲明。 然后將 Main.java 中的window變量作為Main.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();  
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM