如何实现场景切换的java_如何在JavaFX中切换场景

我看了很多页面试图找出如何切换场景,但我没有成功.

我有一个计算器,我的目标是选择一个菜单选项来改变计算器(即:基本和科学).现在我只是测试所以这里是我的代码到目前为止与这个问题相关(我使用的是Scene Builder):

@FXML private MenuItem basic;

@FXML private MenuItem testSwitch;

public static void main(String[] args)

{

Application.launch( args );

}

@Override

public void start(Stage primaryStage) throws Exception

{

Parent pane = FXMLLoader.load(

getClass().getResource( "calculator.fxml" ) );

Scene scene = new Scene( pane );

primaryStage.setScene(scene);

primaryStage.setTitle( "Calculator" );

primaryStage.show();

}

@FXML

public void handleMenuOption(ActionEvent e)

{

if(e.getSource()==basic)

{

changeScene("calculator.fxml");

}

else if(e.getSource()==testSwitch)

{

changeScene("TestSwitch.fxml");

}

}

public void changeScene(String fxml)

{

//this prints out

System.out.println(fxml);

}

编辑

我已经尝试了很多东西.无论如何,我总是得到这个NullPointerException.我觉得它可能与在场景构建器中设置某些内容有关,但我一直无法找到答案

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

at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)

at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)

at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)

at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)

at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)

at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)

at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)

at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)

at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)

at javafx.event.Event.fireEvent(Unknown Source)

at javafx.scene.control.MenuItem.fire(Unknown Source)

at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(Unknown Source)

at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(Unknown Source)

at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)

at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)

at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)

at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)

at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)

at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)

at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)

at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)

at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)

at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)

at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)

at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)

at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)

at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)

at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)

at javafx.event.Event.fireEvent(Unknown Source)

at javafx.scene.Scene$MouseHandler.process(Unknown Source)

at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)

at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)

at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)

at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)

at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(Unknown Source)

at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)

at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)

at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)

at com.sun.glass.ui.View.notifyMouse(Unknown Source)

at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)

at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Caused by: java.lang.reflect.InvocationTargetException

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at sun.reflect.misc.Trampoline.invoke(Unknown Source)

at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at sun.reflect.misc.MethodUtil.invoke(Unknown Source)

... 44 more

Caused by: java.lang.NullPointerException

at CalculatorMain.changeScene(CalculatorMain.java:75)

at CalculatorMain.handleMenuOption(CalculatorMain.java:64)

... 53 more

at CalculatorMain.changeScene(CalculatorMain.java:75)

This is at:stage . getScene() . setRoot(pane);

at CalculatorMain.handleMenuOption(CalculatorMain.java:64)

This is at:changeScene ("TestSwitch.fxml");

工作代码:

我玩了下面使用下面的建议并使用此代码使其工作:

private Stage stage;

public static void main(String[] args)

{

Application.launch( args );

}

@Override

public void start(Stage primaryStage) throws Exception

{

this.stage = primaryStage;

FXMLLoader loader = new FXMLLoader(getClass()

.getResource("calculator.fxml"));

Parent root = (Parent)loader.load();

BasicCalculatorView controller = (BasicCalculatorView)loader.getController();

controller.setModel(new BasicCalculatorModelTest(controller));

controller.setLogic(this);

primaryStage.setTitle("Calculator");

primaryStage.setScene(new Scene(root));

primaryStage.show();

}

public void switchScene(String fxmlFile)

{

FXMLLoader loader = new FXMLLoader(getClass()

.getResource(fxmlFile));

Parent root;

try

{

root = (Parent)loader.load();

if(fxmlFile.equals("calculator.fxml"))

{

BasicCalculatorView controller = (BasicCalculatorView)loader.getController();

controller.setModel(new BasicCalculatorModelTest(controller));

controller.setLogic(this);

}

else if(fxmlFile.equals("TestSwitch.fxml"))

{

TestSwitch controller = (TestSwitch)loader.getController();

controller.setLogic(this);

}

this.stage.setScene(new Scene(root));

}

catch (IOException e)

{

e.printStackTrace();

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值