简体   繁体   English

单击按钮打开现有屏幕

[英]open existing screen on button click

I am newbie on JavaFX. 我是JavaFX的新手。 I need to open another screen on button click which already created. 我需要在已创建的按钮单击上打开另一个屏幕。
For this, i try to search but didn't get anything helpful because every tutorial which i find are redirecting to create new screen. 为此,我尝试搜索,但没有得到任何帮助,因为我发现的每个教程都将重定向到新屏幕。 But i want to open screen which already created. 但是我想打开已经创建的屏幕。

For this, i write following code in controller.java 为此,我在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 code fxml代码

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

But i am getting following exception 但我正在追随异常

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

I don't understand how to open one screen on another screen. 我不明白如何在另一个屏幕上打开一个屏幕。

Please give me reference or hint. 请给我参考或提示。

Use following code in replacement of your Fxml loader, this is a correct way to load fxml file. 使用以下代码替换您的Fxml加载程序,这是加载fxml文件的正确方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM