我看了很多页面试图找出如何切换场景,但我没有成功.
我有一个计算器,我的目标是选择一个菜单选项来改变计算器(即:基本和科学).现在我只是测试所以这里是我的代码到目前为止与这个问题相关(我使用的是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$Contr