JavaFX-为什么我的FileChooser可以访问原始舞台? [英] JavaFX - Why is my FileChooser giving me access to the origin Stage?
本文介绍了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);
}
}
推荐答案
查看全文