如何通过单击- JavaFX项目关闭java窗口

时间:2022-08-04 17:03:20

I made a JavaFX project and created the GUI for the first-login frame in the java Scene Builder. When the login is successful, the login frame must be closed and the next frame must be visible (main program frame). I can make the new frame appear, but I can't make the login frame closed. I tried stuff like dispose() but nothing works. Below is the code for the main class:

我做了一个JavaFX项目,并为java场景构建器中的第一个登录框架创建了GUI。当登录成功时,登录帧必须关闭,下一个帧必须是可见的(主程序帧)。我可以使新的框架出现,但是我不能使登录框架关闭。我尝试过dispose()之类的东西,但没有什么起作用。下面是主要类的代码:

public class KuberComm extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);
        stage.setResizable(false);
        stage.setTitle("Login to KuberComm");
        stage.setScene(scene);

        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

The handler for the login button is located in another class (controller class made by the NetBeans IDE). I can't figure out what the frame's name is in order to use it in the controller class.

登录按钮的处理程序位于另一个类(由NetBeans IDE创建的控制器类)中。为了在控制器类中使用它,我不知道框架的名字是什么。

Any help will be much appreciated!

如有任何帮助,我们将不胜感激!

5 个解决方案

#1


21  

give your button a name in the controller class:

给你的按钮一个控制器类的名字:

@FXML
public Button closeButton;

and add this method:

并添加这个方法:

@FXML
public void handleCloseButtonAction(ActionEvent event) {
    Stage stage = (Stage) closeButton.getScene().getWindow();
    stage.close();
}

In your FXML you need a reference to the button name and the method to call onAction:

在您的FXML中,需要引用按钮名称和调用onAction的方法:

<Button fx:id="closeButton" cancelButton="true" layoutX="350.0" layoutY="767.0" mnemonicParsing="false" onAction="#handleCloseButtonAction" prefWidth="100.0" text="Close" />

This would close the stage that this button is on.

这将关闭这个按钮的舞台。

#2


6  

Use

使用

stage.hide()

If you do this from a controller, you can get the stage from any Node inside the scene of the stage (if necessary let the FXML loader assign one to a field of the controller using the id attribute from the fxml namespace in the fxml):

如果您从控制器进行此操作,您可以从舞台场景中的任何节点获得舞台(如果需要,让FXML加载程序使用FXML名称空间中的id属性将舞台分配给控制器的一个字段):

Window stage = node.getScene().getWindow();

#3


4  

Similar to the other answers but more precise.

与其他答案相似,但更精确。

@FXML
public void handleCloseButtonAction(ActionEvent event) {
    ((Stage)(((Button)event.getSource()).getScene().getWindow())).close();
}

#4


3  

Thanks for your time to reply,but in the end I found out how to fix it. I used

谢谢你的回复,但是最后我找到了解决的办法。我使用

((Node)(event.getSource())).getScene().getWindow().hide();

in the if that it's responsible for the successful login. I mean, after a dialog appears that informs the user about their successful login, that code goes there.

在if中,它负责成功登录。我的意思是,当一个对话框出现并告知用户登录成功后,代码就会出现。

(I imported the right stuff too in order to make that line of code work)

(为了使代码行正常工作,我也导入了正确的内容)

#5


0  

Although

虽然

    getScene().getWindow();

on a Node will get you the stage from the controller, it's important to note that calling close() or hide() are equivalent, and will simply make the login window invisible. As for using dispose():

在一个节点上,您将从控制器获得舞台,重要的是要注意调用close()或hide()是等价的,并且只会使登录窗口不可见。至于使用处理():

This link might help to clear up any confusion.

这个链接可能有助于消除任何混淆。

#1


21  

give your button a name in the controller class:

给你的按钮一个控制器类的名字:

@FXML
public Button closeButton;

and add this method:

并添加这个方法:

@FXML
public void handleCloseButtonAction(ActionEvent event) {
    Stage stage = (Stage) closeButton.getScene().getWindow();
    stage.close();
}

In your FXML you need a reference to the button name and the method to call onAction:

在您的FXML中,需要引用按钮名称和调用onAction的方法:

<Button fx:id="closeButton" cancelButton="true" layoutX="350.0" layoutY="767.0" mnemonicParsing="false" onAction="#handleCloseButtonAction" prefWidth="100.0" text="Close" />

This would close the stage that this button is on.

这将关闭这个按钮的舞台。

#2


6  

Use

使用

stage.hide()

If you do this from a controller, you can get the stage from any Node inside the scene of the stage (if necessary let the FXML loader assign one to a field of the controller using the id attribute from the fxml namespace in the fxml):

如果您从控制器进行此操作,您可以从舞台场景中的任何节点获得舞台(如果需要,让FXML加载程序使用FXML名称空间中的id属性将舞台分配给控制器的一个字段):

Window stage = node.getScene().getWindow();

#3


4  

Similar to the other answers but more precise.

与其他答案相似,但更精确。

@FXML
public void handleCloseButtonAction(ActionEvent event) {
    ((Stage)(((Button)event.getSource()).getScene().getWindow())).close();
}

#4


3  

Thanks for your time to reply,but in the end I found out how to fix it. I used

谢谢你的回复,但是最后我找到了解决的办法。我使用

((Node)(event.getSource())).getScene().getWindow().hide();

in the if that it's responsible for the successful login. I mean, after a dialog appears that informs the user about their successful login, that code goes there.

在if中,它负责成功登录。我的意思是,当一个对话框出现并告知用户登录成功后,代码就会出现。

(I imported the right stuff too in order to make that line of code work)

(为了使代码行正常工作,我也导入了正确的内容)

#5


0  

Although

虽然

    getScene().getWindow();

on a Node will get you the stage from the controller, it's important to note that calling close() or hide() are equivalent, and will simply make the login window invisible. As for using dispose():

在一个节点上,您将从控制器获得舞台,重要的是要注意调用close()或hide()是等价的,并且只会使登录窗口不可见。至于使用处理():

This link might help to clear up any confusion.

这个链接可能有助于消除任何混淆。