[英]How do I center a Text object to the exact center of a JavaFX Stage?
[英]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()
返回阶段。
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
如您所见,这些值确实发生了变化,但是弹出窗口的大小没有变化。
任何帮助,将不胜感激。
您可以使用initOwner()
方法将弹出窗口initOwner()
其父窗口的initOwner()
,假设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 quistion:当我说popup.hide()
是否删除该弹出窗口的实例? 现在,我只是让垃圾收集器执行其操作。 只是好奇。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.