首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JavaFX -如何在舞台上使JFXpopup居中并动态调整弹出窗口的大小

JavaFX -如何在舞台上使JFXpopup居中并动态调整弹出窗口的大小
EN

Stack Overflow用户
提问于 2018-06-14 03:46:51
回答 1查看 1.1K关注 0票数 0

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

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

然后你就可以看到弹出窗口了:

弹出窗口位于x轴的中心,但显然不在y轴上。同样,当我调整我的舞台大小并重新打开弹出窗口时,大小没有改变。

现在来看一些代码:

代码语言:javascript
复制
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()的一些值:

代码语言:javascript
复制
stage - width: 800.0height: 550.4000244140625
width: 533.3333333333334 height: 366.933349609375

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

正如您所看到的,这些值确实会更改,但弹出窗口的大小不会更改。

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-20 00:26:59

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

其次,我现在将更改后的内容添加到以下代码中:我制作了它,以便在单击按钮时创建弹出窗口。因此,我没有创建1个jfxpopup,而是覆盖了指向popup的指针,并让垃圾收集器完成剩下的工作。

代码语言:javascript
复制
// 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()的时候,是不是删除了弹出窗口的实例?现在,我只是让垃圾收集器来做它的事情。只是好奇而已。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50845124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档