java 按钮关闭窗口,如何通过单击按钮关闭Java窗口 - JavaFX项目

博主在JavaFX项目中创建了GUI登录界面,并使用JavaSceneBuilder设计。成功登录后,需要关闭登录窗口并显示主程序窗口。尽管能够显示主程序窗口,但无法关闭登录窗口。尝试过dispose()等方法无效。问题在于不知道如何在控制器类中引用登录窗口以关闭它。解决方案是在控制器类中给登录按钮命名,然后添加一个处理方法来关闭窗口。
摘要由CSDN通过智能技术生成

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:

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.

Any help will be much appreciated!

解决方案

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:

This would close the stage that this button is on.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值