JavaFX2:从内部关闭一个阶段(子阶段) [英] JavaFX2 : Closing a stage (substage) from within itself

查看:179
本文介绍了JavaFX2:从内部关闭一个阶段(子阶段)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaFx的新手,我正在创建一个应用程序,并且需要类似于使用swing组件时提供的JDialog。我通过创建新舞台解决了这个问题,但现在我需要一种方法通过单击按钮从内部关闭新舞台。 (是的,x按钮也能正常工作,但也希望它也能按下按钮)。描述情况:
我有一个主类,我用它创建一个带有场景的主舞台。我使用FXML。

I am new to JavaFx and I am creating an application and was in need of something similar to JDialog that was offered while using swing components. I solved that by creating new stage, but now I need a way to close the new stage from within itself by clicking a button. (yes, the x button works too, but wanted it on button as well). To describe the situation: I have a main class from which I create the main stage with a scene. I use FXML for that.

public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("Builder.fxml"));
    stage.setTitle("Ring of Power - Builder");
    stage.setScene(new Scene(root));
    stage.setMinHeight(600.0);
    stage.setMinWidth(800.0);
    stage.setHeight(600);
    stage.setWidth(800);
    stage.centerOnScreen();
    stage.show();
}

现在,在出现的主窗口中,我有所有控制项目和菜单,通过FXML和适当的控制类制作的东西。这是我决定在帮助菜单中包含关于信息的部分。因此,当菜单帮助 - 关于激活时,我发生了一个事件,如下所示:

Now in the main window that appears I have all the control items and menus and stuff, made through FXML and appropriate control class. That's the part where I decided to include the About info in the Help menu. So I have an event going on when the menu Help - About is activated, like this:

@FXML
private void menuHelpAbout(ActionEvent event) throws IOException{
    Parent root2 = FXMLLoader.load(getClass().getResource("AboutBox.fxml"));
    Stage aboutBox=new Stage();
    aboutBox.setScene(new Scene(root2));
    aboutBox.centerOnScreen();
    aboutBox.setTitle("About Box");
    aboutBox.setResizable(false);
    aboutBox.initModality(Modality.APPLICATION_MODAL); 
    aboutBox.show();
}

如图所示,关于Box的窗口是通过FXML再次创建的控件类。有一个图片,一个文本区域和一个按钮,我希望该按钮可以关闭AboutBox.java类中的aboutBox新阶段。

As seen the About Box window is created via FXML with control class again. There is a picture, a text area and a button, and I want that button to close the new stage that is the aboutBox from within the AboutBox.java class so to speak.

我发现自己能够做到这一点的唯一方法是定义
公共静态舞台aboutBox; Builder.java类中的
,以及处理关闭按钮上的动作事件的方法中的AboutBox.java中的那个引用。但不知何故,它感觉并不完全干净正确。还有更好的办法吗?

The only way I found myself to be able to do this, was to define a public static Stage aboutBox; inside the Builder.java class and reference to that one from within the AboutBox.java in method that handles the action event on the closing button. But somehow it doesn't feel exactly clean and right. Is there any better way?

提前致谢,谢谢。

推荐答案

您可以从传递给事件处理程序的事件派生要关闭的阶段。

You can derive the stage to be closed from the event passed to the event handler.

new EventHandler<ActionEvent>() {
  @Override public void handle(ActionEvent actionEvent) {
    // take some action
    ...
    // close the dialog.
    Node  source = (Node)  actionEvent.getSource(); 
    Stage stage  = (Stage) source.getScene().getWindow();
    stage.close();
  }
}

这篇关于JavaFX2:从内部关闭一个阶段(子阶段)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆