使用JavaFX的警报框未显示在应用程序的中心

时间:2018-07-16 04:15:21

标签: javafx

当我调整应用程序大小或移动应用程序时,我试图弹出应用程序中心的框。 我尝试使用CSS对齐方式,也使用Java。如果我不使用窗格并直接在场景中添加框,是否可能?

这是我的代码:

public Boolean call(String question) {
  final Stage dialogStage = new Stage(StageStyle.UNDECORATED);
  dialogStage.initModality(Modality.WINDOW_MODAL);
  dialogStage.initOwner(owner);
  dialogStage.setTitle("ConfirmTitle"); // WIP, waiting for the strings&trans
  final Button ok = new Button(
      nmsGuiContainer.getI18nService().getMessage("com.mlnms.gui.fmwk.main.container.ok")); // WIP,
                                                                                            // waiting
                                                                                            // for
                                                                                            // the
  // strings&trans
  ok.getStyleClass().add(HTML_POPUP_BUTTON_STYLE);
  final Button cancel = new Button(
      nmsGuiContainer.getI18nService().getMessage("com.mlnms.gui.fmwk.main.container.cancel")); // WIP,
                                                                                                // waiting
  // for the
  // strings&trans
  cancel.getStyleClass().add(HTML_POPUP_BUTTON_STYLE);
  final Text text = new Text(question);
  text.getStyleClass().add(HTML_POPUP_STYLE);
  final Insets ins = new Insets(10);
  final VBox box = new VBox();
  box.setAlignment(Pos.BOTTOM_CENTER);
  box.setSpacing(10);
  box.setPadding(ins);
  final HBox buttons = new HBox(10);
  buttons.getChildren().addAll(ok, cancel);
  buttons.setAlignment(Pos.CENTER);
  buttons.setPadding(ins);
  box.getChildren().addAll(text, buttons);
  box.getStyleClass().add(HTML_POPUP_STYLE);
  StackPane pane = new StackPane();
  pane.setAlignment(box, Pos.CENTER);
  pane.getChildren().add(box);
  Scene scene = new Scene(pane);
  try {
    URL javafxCss = nmsGuiContainer.getBundleContext().getBundle()
        .getResource(NmsGuiContainer.JAVAFX_CSS_URL);
    scene.getStylesheets().add(javafxCss.toExternalForm());

  } catch (Exception e) {
    LOGGER.error("Cannot load the CSS file for JavaFX components ", e);
  }
  dialogStage.setScene(scene);
  ok.setCancelButton(false);

  final boolean[] res = new boolean[1];
  ok.setOnAction(new CloseDialogHandler(dialogStage, res));
  cancel.setCancelButton(true);
  cancel.setOnAction(new CloseDialogHandler(dialogStage, null));
  dialogStage.centerOnScreen();
  nmsGuiContainer.fadeContainer();
  dialogStage.showAndWait();
  nmsGuiContainer.unfadeContainer();
  return res[0];
}

以下是警报框的屏幕截图:

This is the screenshot of the alertbox

1 个答案:

答案 0 :(得分:1)

Stage.initOwner()方法完全可以满足您的需求。当您在示例代码中调用它时,我不知道您要传递给什么owner

这是一个演示如何执行此操作的示例。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) {

        VBox root = new VBox(10);
        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(10));

        Button btnShowAlert = new Button("Show Alert!");

        // Set the action to show the alert
        btnShowAlert.setOnAction(e -> {
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setHeaderText("This is centered over the main window!");
            alert.setContentText("Move the main window and show the alert again!");

            alert.initOwner(primaryStage);
            alert.showAndWait();

        });

        root.getChildren().add(btnShowAlert);

        primaryStage.setTitle("Centered Alerts");
        primaryStage.setScene(new Scene(root));
        primaryStage.setWidth(500);
        primaryStage.setHeight(300);

        primaryStage.show();
    }
}