【问题标题】:Alert box is not showing in the center of the application using JavaFX [duplicate]使用 JavaFX 的应用程序中心未显示警报框 [重复]
【发布时间】:2018-12-23 13:39:59
【问题描述】:

我试图在调整应用程序大小或移动应用程序时弹出应用程序中心的框。 我尝试使用 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];
}

这里是alertbox的截图:

【问题讨论】:

  • 试图阅读这段代码让我头疼。但是您不需要 CSS 将 Alert 居中,您只需要调用警报的 initOwner() 方法即可。您确实在这里调用它,并将owner 传递给它,但您发布的代码并未表明owner 是什么。

标签: javafx


【解决方案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();
    }
}

【讨论】:

  • 感谢 Zephyr 的帮助。它有效。
  • @KaushalPatel 不要忘记按复选标记将此标记为正确答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-29
相关资源
最近更新 更多