0

我想在屏幕中央制作一个具有动态宽度和高度的 JFXpopup。这是我目前拥有的:

申请第一眼 这是应用程序的第一眼。要打开弹出窗口,我必须单击上下文菜单中的一个项目:

上下文菜单 之后你会看到弹出窗口: 弹出窗口

弹出窗口以 x 轴为中心,但显然不在 y 轴上。此外,当我调整舞台大小并重新打开弹出窗口时,大小不会改变。 弹出窗口2

现在看一些代码:

contextMenu.getNewLeaf().setOnAction(event -> {
            popup.setPopupContent(createRegistrationFormPane());

            popup.setAutoFix(true);

            double width = globals.getPrimaryStage().getWidth() / 1.5;
            double height = globals.getPrimaryStage().getHeight() / 1.5;

            System.out.println("stage - width: " + globals.getPrimaryStage().getWidth() + "height: " + globals.getPrimaryStage().getHeight());
            System.out.println("width: " + width + " height: " + height);

            popup.getPopupContent().setPrefWidth(width);
            popup.getPopupContent().setPrefHeight(height);

            double anchorX = (globals.getPrimaryStage().getWidth() - width) / 2;
            double anchorY = (globals.getPrimaryStage().getHeight() - height) / 2;

            popup.show(rootPane, JFXPopup.PopupVPosition.TOP, JFXPopup.PopupHPosition.LEFT, anchorX, anchorY);

            item.getChildren().add(new TreeItem<>(new Twitter()));
            contextMenu.freeActionListeners();
        });

globals.getPrimaryStage()返回舞台。
rootPane是井...第一个窗格。

以下是来自 的一些值system.out.println()

stage - width: 800.0height: 550.4000244140625
width: 533.3333333333334 height: 366.933349609375

stage - width: 1339.199951171875height: 684.7999877929688  
width: 892.7999674479166 height: 456.5333251953125

如您所见,这些值确实发生了变化,但弹出窗口的大小没有改变。

任何帮助,将不胜感激。

4

2 回答 2

0

您可以使用该initOwner()方法将弹出阶段置于其父窗口的中心,假设popup 一个阶段:

popup.initOwner(globals.getPrimaryStage())

看起来你popup每次调用动作时都在创建一个新的,并根据大小设置primaryStage大小。如果您希望它记住您自己的尺寸,则需要在popup关闭时保存这些尺寸。

于 2018-06-13T20:53:04.917 回答
0

首先,我注意到获取舞台高度包括顶栏( - [] X )。这就是弹出窗口不能正确垂直居中的原因。为了解决这个问题,我添加了.getScene()然后.getHeight().

其次,我现在将更改的内容添加到以下内容:我这样做是为了在单击按钮时创建弹出窗口。因此,我没有制作 1 个 jfxpopup,而是重写了指向弹出窗口的指针,让垃圾收集器完成剩下的工作。

// Option add new leaf
CONTEXT_MENU.getNewLeaf().setOnAction(event -> {
  popup = new JFXPopup();
  try {
    popup.setPopupContent((Region) UTIL
        .getPopupView(UTIL.getView("view/application/popup/FormTest.fxml"), popup));
  } catch (IOException e) {
    e.printStackTrace();
  }
  popup.setAutoFix(true);
  // Width & height of the popup
  double width = GLOBALS.getPrimaryStage().getScene().getWidth() / 1.5;
  double height = GLOBALS.getPrimaryStage().getScene().getHeight() / 1.5;
  popup.getPopupContent().setPrefWidth(width);
  popup.getPopupContent().setPrefHeight(height);
  // X & y axis of the popup
  double anchorX = (GLOBALS.getPrimaryStage().getScene().getWidth() - width) / 2;
  double anchorY = (GLOBALS.getPrimaryStage().getScene().getHeight() - height) / 2;
  popup.show(ROOT_PANE, JFXPopup.PopupVPosition.TOP, JFXPopup.PopupHPosition.LEFT, anchorX,
      anchorY);
  item.getChildren().add(new TreeItem<>(new Twitter()));
  CONTEXT_MENU.freeActionListeners();
});

1 个问题:当我说popup.hide()这是否会删除该弹出窗口的实例时?现在我只是让垃圾收集器做它的事情。只是好奇。

于 2018-06-19T16:26:59.087 回答