【发布时间】:2015-06-13 16:45:50
【问题描述】:
当我单击按钮时,会打开 FileChooser。但是,例如,我可以在 FileChooser 仍然打开时关闭 Original Stage,或者我仍然可以单击并切换实际窗口。检查下面的代码
我的问题是:
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);
}
}
【问题讨论】:
标签: events button javafx javafx-8 filechooser