【问题标题】:Popup width and height = 0 [duplicate]弹出窗口的宽度和高度 = 0 [重复]
【发布时间】:2012-11-29 17:12:03
【问题描述】:

我正在尝试将 Popup 置于舞台中心,但我无法正确操作,因为我需要 Popup 的大小并且它的大小为 0 (popup.getWidth()=0 和 popup .getHeight()=0)。

如何获得正确的尺寸?

我的代码如下:

                Popup popup = new Popup();
                popup.setAutoFix(true);
                popup.setAutoHide(true);
                popup.setHideOnEscape(true);
                Label label = new Label("Empty indentation char!");
                label.setOpacity(100);
                label.setStyle("-fx-background-color: cornsilk;");
                popup.getContent().add(label);
                Point2D center = Utils.getCenter(mainClass.getOptionsStage());
                popup.show(mainClass.getOptionsStage(),
                        center.getX() - popup.getWidth() / 2,
                        center.getY() - popup.getHeight() / 2);
  • Utils.getCenter() 返回窗口的中心点。

【问题讨论】:

  • 完全重复?如果您确实喜欢该链接,则表明 Popup 组件不会居中。向我展示如何将这样的弹出窗口居中,我挑战你!

标签: java user-interface javafx-2


【解决方案1】:

Popup 在被展示之前不知道他的界限。

显示后尝试移动:

popup.show(mainClass.getOptionsStage());
popup.setX(center.getX() - popup.getWidth() / 2);
popup.setY(center.getY() - popup.getHeight() / 2);

【讨论】:

  • 谢谢,它有效。我不喜欢在组件可见后设置它们的位置,但我认为这确实是目前唯一的方法。
猜你喜欢
  • 2011-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-14
  • 1970-01-01
相关资源
最近更新 更多