如何通过单击按钮关闭 Java 窗口 - JavaFX 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25037724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to close a java window with a button click - JavaFX Project
提问by Aristomenis
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 Scene Builder 中为第一个登录框架创建了 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!
任何帮助都感激不尽!
回答by Moh-Aw
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.
这将关闭此按钮所在的阶段。
回答by fabian
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):
如果从控制器执行此操作,则可以从舞台Node
场景内的任何地方获取舞台(如有必要,让 FXML 加载器使用id
fxml 中 fxml 命名空间中的属性将一个分配给控制器的字段):
Window stage = node.getScene().getWindow();
回答by Forager
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()
:
on aNode
将使您从控制器进入舞台,重要的是要注意调用close()
orhide()
是等效的,并且只会使登录窗口不可见。至于使用dispose()
:
回答by Aristomenis
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)
(我也导入了正确的东西以使那行代码工作)
回答by Sedrick
Similar to the other answers but more precise.
与其他答案类似,但更精确。
@FXML
public void handleCloseButtonAction(ActionEvent event) {
((Stage)(((Button)event.getSource()).getScene().getWindow())).close();
}
回答by Jean Castro
Hide doesn't close the window, just put in visible mode. The best solution was:
隐藏不会关闭窗口,只是将其置于可见模式。最好的解决办法是:
@FXML
private void exitButtonOnAction(ActionEvent event){
((Stage)(((Button)event.getSource()).getScene().getWindow())).close();
}