JavaFX-为什么我的FileChooser可以访问原始舞台? [英] JavaFX - Why is my FileChooser giving me access to the origin Stage?

查看:81
本文介绍了JavaFX-为什么我的FileChooser可以访问原始舞台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击按钮时,将打开一个FileChooser.但是,例如,当FileChooser仍然打开时,我可以关闭原始舞台,或者我仍然可以单击并切换实际窗口.检查下面的代码

When i click on the button, a FileChooser is opened. However, i can for example close the Original Stage while the FileChooser is still opened, or i still can click and switch the actual window. Check the code below

我的问题是:
1-关闭主窗口时如何关闭FileChooser?
2-打开FileChooser时如何使主窗口不可单击?

package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;

public class Main extends Application {

    @Override public void start(Stage stage) {

        stage.setTitle("Main Stage");
        stage.setWidth(500);
        stage.setHeight(500);
        stage.show();
        Button button = new Button();
        AnchorPane ap = new AnchorPane();
        Scene scene = new Scene(ap);
        ap.getChildren().addAll(button);
        stage.setScene(scene);

        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent e) {
                FileChooser fileChooser = new FileChooser();
                Stage stage2=new Stage();
                stage2.initOwner(stage);
                stage2.initModality(Modality.WINDOW_MODAL);
                fileChooser.showOpenDialog(stage2);  
           }
       });  
    }

    public static void main(String[] args) {
        launch(args);
    }
}

推荐答案

根据

如果设置了文件对话框的所有者窗口,请输入所有窗口 文件对话框正在执行时,对话框所有者链中的文件被阻止 显示.

If the owner window for the file dialog is set, input to all windows in the dialog's owner chain is blocked while the file dialog is being shown.

但是,您将所有者窗口设置为不在屏幕上的窗口,因此我认为在这种情况下没有所有者链",并且文件选择器实际上不是模态的.

However, you are setting the owner window to a window that is not on the screen, so I think there is no "owner chain" in that case, and the file chooser is effectively not modal.

为什么不做

    button.setOnAction(new EventHandler<ActionEvent>() {
        @Override public void handle(ActionEvent e) {
            FileChooser fileChooser = new FileChooser();
            fileChooser.showOpenDialog(stage); 
       }
   });

以便将文件选择器的所有者窗口设置为实际窗口?

so that you make the owner window of the file chooser the actual window?

这篇关于JavaFX-为什么我的FileChooser可以访问原始舞台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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