如何显示JFXDialog以确认用户是否要关闭窗口?

时间:2016-10-02 20:30:01

标签: java javafx

我在javafx应用程序中使用http://jfoenix.com/进行材质设计,我想在用户想要关闭窗口时显示确认对话框。我自己尝试这个,但是当我点击X按钮时JFXDialog没有出现。怎么了?

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    Parent loginView = FXMLLoader.load(getClass().getResource("login.fxml"));
    javafx.geometry.Rectangle2D r = Screen.getPrimary().getBounds();
    primaryStage.setScene(new Scene(loginView, r.getWidth(), r.getHeight()));
    primaryStage.setOnCloseRequest(event -> {
        JFXDialogLayout layout = new JFXDialogLayout();
        layout.setHeading(new Text("Confirmar"));
        layout.setBody(new Text("¿Está seguro que desea salir?"));
        StackPane stackpane = new StackPane();
        JFXDialog dialog = new JFXDialog(stackpane, layout, JFXDialog.DialogTransition.CENTER);
        JFXButton yeap = new JFXButton("OK");
        yeap.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                dialog.close();
                primaryStage.hide();
            }
        });
        JFXButton nope = new JFXButton("Cancelar");
        nope.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                dialog.close();
            }
        });
        layout.setActions(nope, yeap);
        dialog.show();
    });
    primaryStage.show();
    primaryStage.setResizable(false);

}
public static void main(String[] args) {
    launch(args);
}

}

0 个答案:

没有答案