JavaFX - 为什么我的 FileChooser 允许我访问原始舞台?
JavaFX - Why is my FileChooser giving me access to the origin Stage?
当我单击该按钮时,将打开一个 FileChooser。但是,例如,我可以在 FileChooser 仍处于打开状态时关闭 Original Stage,或者我仍然可以单击并切换实际 window。检查下面的代码
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.
但是,您将所有者 window 设置为不在屏幕上的 window,所以我认为在这种情况下没有 "owner chain",并且文件选择器实际上不是模态的。
为什么不直接做
button.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
FileChooser fileChooser = new FileChooser();
fileChooser.showOpenDialog(stage);
}
});
以便您将文件选择器的所有者 window 设为实际的 window?
当我单击该按钮时,将打开一个 FileChooser。但是,例如,我可以在 FileChooser 仍处于打开状态时关闭 Original Stage,或者我仍然可以单击并切换实际 window。检查下面的代码
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.
但是,您将所有者 window 设置为不在屏幕上的 window,所以我认为在这种情况下没有 "owner chain",并且文件选择器实际上不是模态的。
为什么不直接做
button.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
FileChooser fileChooser = new FileChooser();
fileChooser.showOpenDialog(stage);
}
});
以便您将文件选择器的所有者 window 设为实际的 window?