我在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);
}
}