JavaFX-2:如果不是手动设置,如何获取窗口大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19025935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX-2: how to get window size if it wasn't set manually?
提问by Dragon
I want to open a dialog window above the center of its parent window, so I use the following formula:
我想在其父窗口中心上方打开一个对话窗口,因此我使用以下公式:
Window window = ((Node) actionEvent.getSource()).getScene().getWindow();
Scene scene = new Scene(new Group(new DialogWindow()));
Stage dialog = new Stage();
dialog.initOwner(window);
dialog.sizeToScene();
dialog.setX(stage.getX() + stage.getWidth() / 2 - dialog.getWidth() / 2); //dialog.getWidth() = NaN
dialog.setY(stage.getY() + stage.getHeight() / 2 - dialog.getHeight() / 2); //dialog.getHeight() = NaN
dialog.setScene(scene);
dialog.show(); //it is better to showAndWait();
I don't set size the manually because I need the window to be sized automatically to the size of its content.
我不手动设置大小,因为我需要将窗口的大小自动调整为其内容的大小。
Under Linux it sets window straight in the center of the parent window. But in Windows it doesn't work and leads to different results.
在 Linux 下,它将窗口直接设置在父窗口的中心。但在 Windows 中它不起作用并导致不同的结果。
How can I get the dialog's width and height if I don't set them manually?
如果我不手动设置它们,如何获得对话框的宽度和高度?
采纳答案by Uluk Biy
The width and height of Stage
are calculated after it has been shown (.show()
). Do the calculation after it:
的宽度和高度Stage
是在显示后计算的 ( .show()
)。之后进行计算:
...
dialog.show();
dialog.setX(stage.getX() + stage.getWidth() / 2 - dialog.getWidth() / 2); //dialog.getWidth() = not NaN
dialog.setY(stage.getY() + stage.getHeight() / 2 - dialog.getHeight() / 2); //dialog.getHeight() = not NaN
EDIT:
If showAndWait()
is used instead of show()
, then since showAndWait()
blocks the caller event the calculations after the showAndWait()
are also blocked. The one way of workaround could be doing calculation before in new Runnable
:
编辑:
如果showAndWait()
使用代替show()
,则由于showAndWait()
阻塞了调用者事件,showAndWait()
因此也阻塞了之后的计算。一种解决方法可能是在 new 之前进行计算Runnable
:
final Stage dialog = new Stage();
dialog.initOwner(window);
dialog.initModality(Modality.WINDOW_MODAL);
dialog.sizeToScene();
dialog.setScene(scene);
Platform.runLater(new Runnable() {
@Override
public void run() {
dialog.setX(primaryStage.getX() + primaryStage.getWidth() / 2 - dialog.getWidth() / 2); //dialog.getWidth() = NaN
dialog.setY(primaryStage.getY() + primaryStage.getHeight() / 2 - dialog.getHeight() / 2); //dialog.getHeight() = NaN
}
});
dialog.showAndWait();
Note also on initModality
. The modality must be set in case of showAndWait()
. Otherwise using showAndWait()
has no sense.
还要注意initModality
。在 的情况下必须设置模态showAndWait()
。否则使用showAndWait()
没有意义。
回答by Jay Thakkar
Try this:
尝试这个:
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
System.out.println(screenBounds.getHeight());
System.out.println(screenBounds.getWidth());