我已经浏览了很多页面,尝试了解如何切换场景,但我一直不成功。如何在JavaFX中切换场景
我有一个计算器,我的目标是选择一个菜单选项来改变计算器(即:基本和科学)。现在我只是测试所以这里是我的代码有关这个问题迄今(我使用场景生成器):
@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