gpt4 book ai didi

events - JavaFX - 为什么我的 FileChooser 允许我访问原始舞台?

转载 作者:行者123 更新时间:2023-12-04 06:21:07 28 4
gpt4 key购买 nike

当我单击该按钮时,将打开一个 FileChooser。但是,例如,我可以在 FileChooser 仍处于打开状态时关闭 Original Stage,或者我仍然可以单击并切换实际窗口。检查下面的代码

My questions are :
1- How can i make the FileChooser Closes when i close the main window ?
2- How can i make the main window not clickable when the FileChooser is opened ?

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

最佳答案

根据JavaDocs

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.

但是,您将所有者窗口设置为不在屏幕上的窗口,所以我认为在这种情况下不存在“所有者链”,并且文件选择器实际上不是模态的。

为什么不做

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

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

关于events - JavaFX - 为什么我的 FileChooser 允许我访问原始舞台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29519518/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com