当我点击按钮时,会打开一个FileChooser。但是,例如,我可以在FileChooser仍然打开时关闭原始阶段,或者仍然可以单击并切换实际的窗口。检查下面的代码
我的问题是: 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);
}
}发布于 2015-04-08 17:07:06
根据JavaDocs
如果设置了文件对话框的所有者窗口,则在显示文件对话框时,对话框所有者链中所有窗口的输入都会被阻塞。
但是,您将所有者窗口设置为屏幕上没有的窗口,因此我认为在这种情况下不存在“所有者链”,而且文件选择器实际上不是模式。
为什么不直接做
button.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
FileChooser fileChooser = new FileChooser();
fileChooser.showOpenDialog(stage);
}
});以便使文件的所有者窗口选择实际的窗口?
https://stackoverflow.com/questions/29519518
复制相似问题