Javafx8一旦阶段设置可见,就无法设置所有者

时间:2017-12-11 21:39:20

标签: javafx-8

我有两个班,第一个是控制第二班的形式 它有2个按钮:btn1将模态设置为无,btn2将模态设置为APPLICATION_MODAL。
我只能按下一个按钮(它会创建一个带有所需模态的新舞台的窗口)但是当我关闭这个窗口并且我尝试按下另一个按钮时我总是得到"一旦舞台被设置为可见就无法设置所有者& #34;

//My control class:
public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    @Override
    public void start( Stage stage) {
        stage.setTitle("Blank Stage 1");
        Button okBtn = new Button("OK");
        Button okBtn2 = new Button("OK");
        VBox root = new VBox();
        root.getChildren().add(okBtn);
        root.getChildren().add(okBtn2);
        Scene scene = new Scene(root, 200, 100);
        stage.setScene(scene);
        stage.setTitle("A Dialog Box");
        stage.show();

    okBtn.setOnAction(e -> {
        try {
            showDialog();
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    });
    okBtn2.setOnAction(e -> {
        try {
            showDialog(stage, APPLICATION_MODAL);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    });

}
    private void showDialog(Window owner, Modality modality) throws Exception {
        BlankStage MyC = new BlankStage();
        MyC.start(BlankStage.classStage, owner, modality);
    }
    private void showDialog() throws Exception {
        BlankStage MyC = new BlankStage();
        MyC.start(BlankStage.classStage);

    }
}
//This is my2nd class:
public class BlankStage extends Application {
    public static void main(String[] args) {
        Application.launch(args); 
    }
    static Stage classStage = new Stage();

    public void start(Stage primaryStage, Window owner, Modality modality) {
        classStage = primaryStage;
        primaryStage.initOwner(owner);
        primaryStage.initModality(modality);
        primaryStage.setTitle("Blank Stage");
        primaryStage.setHeight(100);
        primaryStage.setWidth(300);
        primaryStage.show();
    }
    @Override
    public void start(Stage primaryStage) throws Exception {
        classStage = primaryStage;
        primaryStage.setTitle("No Modality Stage");
        primaryStage.setHeight(100);
        primaryStage.setWidth(300);
        primaryStage.show();
    }
}
我尝试使用
- 关();并隐藏(); (实际上它们是一样的)。没有效果。
- platform.exit(),当然它关闭了我的整个应用程序:) - Showandwait在展位课程中(我得到更多错误)

不知道如何永久关闭第二个窗口(或重置模态值...... :()

2 个答案:

答案 0 :(得分:0)

您的问题本身就有答案:一旦阶段设置可见,就无法设置所有者。

Stage#initOwner方法的javadoc:

 Specifies the owner Window for this stage, or null for a top-level,
 unowned stage. This must be done prior to making the stage visible.

 @param owner the owner for this stage.

 @throws IllegalStateException if this property is set after the stage 
 has ever been made visible.

 @throws IllegalStateException if this stage is the primary stage.

 @defaultValue null

答案 1 :(得分:-1)

如果是这样的话......就这样吧。:) 与此同时,我找到了解决方法 我在Control Class中添加了一些简单的行:

private void showDialog(Window owner, Modality modality) throws Exception {
            MyCalenderModal MyC = new MyCalenderModal();
            System.out.println(MyCalenderModal.classStage.getModality());
            if (MyCalenderModal.classStage.getModality()==NONE) {
                MyC.start(MyCalenderModal.classStage, owner, modality);
            }
            else MyC.start(MyCalenderModal.classStage);

        }

它检查第二个窗口是否已有模态。如果是,它只显示舞台 我必须记住,如果我打开一个窗口,我不能再关闭它,直到我的应用程序运行。