点击按钮打开现有屏幕

时间:2013-12-10 18:47:07

标签: button javafx screen

我是JavaFX的新手。我需要在已经创建的按钮点击上打开另一个屏幕 为此,我尝试搜索但没有得到任何帮助,因为我找到的每个教程都重定向到创建新的屏幕。但我想打开已经创建的屏幕。

为此,          我在controller.java中编写以下代码

@FXML
public void handleNewTestBedButtonAction(ActionEvent event) throws IOException {


    URL url = getClass().getResource("com/sobc/testbed/gui/testbedform.fxml");

    FXMLLoader fxmlloader = new FXMLLoader();
    fxmlloader.setLocation(url);
    fxmlloader.setBuilderFactory(new JavaFXBuilderFactory());
    AnchorPane pane = new AnchorPane();

    pane.getChildren().clear();
    pane.getChildren().add((Node) fxmlloader.load(url.openStream()));
    // here we go
    //((SOARiteController) fxmlloader.getController()).setContext();
}

fxml代码

<Button id="newtestbedbtn" fx:id="newTestBedButtonId" mnemonicParsing="false" onAction="#handleNewTestBedButtonAction" onMouseEntered="#NewTestBedButtonMouseEntered" onMouseExited="#NewTestBedButtonMouseExited" styleClass="imgbtn" text="" wrapText="false">

但我正在追踪异常

Caused by: java.lang.NullPointerException
at com.soab.SOARiteController.handleNewTestBedButtonAction(SOARiteController.java:52)  

我不明白如何在另一个屏幕上打开一个屏幕。

请给我参考或提示。

1 个答案:

答案 0 :(得分:1)

使用以下代码替换您的Fxml加载程序,这是加载fxml文件的正确方法。

URL url = getClass().getResource("com/sobc/testbed/gui/testbedform.fxml");
FXMLLoader loader = new FXMLLoader(url);
Node node = (Node) loader.load();
相关问题