JavaFX - 如何在舞台上居中 JFXpopup 和弹出窗口的动态大小
JavaFX - how to center JFXpopup on stage and dynamic size of popup
我想在屏幕中央制作一个具有动态宽度和高度的 JFXpopup。这是我目前拥有的:
这是应用程序的第一眼。要打开弹出窗口,我必须单击上下文菜单中的一个项目:
之后你会看到弹出窗口:
弹出窗口以 x 轴为中心,但显然不在 y 轴上。此外,当我调整舞台大小时并重新打开弹出窗口时,大小不会改变。
现在输入一些代码:
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()
returns舞台
rootPane
是井...第一个Pane。
以下是 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
如您所见,这些值确实发生了变化,但弹出窗口的大小没有变化。
如有任何帮助,我们将不胜感激。
您可以使用 initOwner()
方法将弹出阶段置于其父级 window 的中央,假设 popup
是 一个阶段:
popup.initOwner(globals.getPrimaryStage())
看起来您每次调用操作时都在创建一个新的 popup
并根据 primaryStage
大小设置大小。如果您想让它记住您自己的尺码,您需要在 popup
关闭时的某个时候保存这些尺寸。
首先我注意到获取舞台高度包括顶部栏 ( - [] 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()
时,是否会删除该弹出窗口的实例?现在我只是让垃圾收集器做它的事情。只是好奇。
我想在屏幕中央制作一个具有动态宽度和高度的 JFXpopup。这是我目前拥有的:
弹出窗口以 x 轴为中心,但显然不在 y 轴上。此外,当我调整舞台大小时并重新打开弹出窗口时,大小不会改变。
现在输入一些代码:
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()
returns舞台
rootPane
是井...第一个Pane。
以下是 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
如您所见,这些值确实发生了变化,但弹出窗口的大小没有变化。
如有任何帮助,我们将不胜感激。
您可以使用 initOwner()
方法将弹出阶段置于其父级 window 的中央,假设 popup
是 一个阶段:
popup.initOwner(globals.getPrimaryStage())
看起来您每次调用操作时都在创建一个新的 popup
并根据 primaryStage
大小设置大小。如果您想让它记住您自己的尺码,您需要在 popup
关闭时的某个时候保存这些尺寸。
首先我注意到获取舞台高度包括顶部栏 ( - [] 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()
时,是否会删除该弹出窗口的实例?现在我只是让垃圾收集器做它的事情。只是好奇。