问题描述
我是JavaFX和SceneBuilder的新手。现在,我在如何直接从我的控制器正确显示应用程序的第二个视图上已经停留了一段时间。 (如果用户单击“下一步”按钮,则显示此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
解决方法
控制器中的window变量抛出NullPointerException
,因为它没有引用Main.java中的实际窗口。
一个简单的解决方法是创建一个Stage
变量,您可以从控制器中访问它。这是一个示例:
Main.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
变量,该变量引用window1
方法的参数中的start()
变量。
现在要对窗口进行更改,我只需在Main类中引用window变量即可。首先,在控制器中删除window
声明。然后,以window
的身份访问Main.java中的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();
}