【问题标题】:Javafx8 Cannot set owner once stage has been set visible一旦阶段设置为可见,Javafx8无法设置所有者
【发布时间】:2017-12-11 21:39:20
【问题描述】:

我有两个类,第一个用于控制第二个类的模态。
它有 2 个按钮:btn1 将模态设置为无,btn2 将模态设置为 APPLICATION_MODAL。
我只能按一个按钮(它会创建具有所需模式的新阶段的窗口)但是当我关闭此窗口并尝试按另一个按钮时,我总是得到“一旦阶段设置为可见就无法设置所有者”

//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 在展位课程中(我遇到了更多错误)

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

【问题讨论】:

  • 为什么你的应用程序中有两个应用程序类?

标签: javafx-8


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    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);
    
            }
    

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

    【讨论】:

    • 解决方案肯定是首先正确地构建您的应用程序。
    猜你喜欢
    • 2017-03-20
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    相关资源
    最近更新 更多