本文介绍了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
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
但是,您将所有者窗口设置为不在屏幕上的窗口,因此我认为在这种情况下没有所有者链",并且文件选择器实际上不是模态的.
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可以访问原始舞台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!