macos JFrame 在 MAC OSX 上的窗口中心设置位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9925081/
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
JFrame set location at center of window on MAC OSX
提问by Asghar
Any body please can please help me, how to center a JFrame
on Mac. OS X?
任何机构都可以请帮助我,如何JFrame
在 Mac上居中。操作系统?
I have tried:
我努力了:
this.setLocationRelativeto(null);
this.setLocationRelativeto(this);
this.setLocationRelativeto(getRootPane());
..and
..和
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final Dimension screenSize = toolkit.getScreenSize();
final int x = (screenSize.width - this.getWidth()) / 2;
final int y = (screenSize.height - this.getHeight()) / 2;
this.setLocation(x, y);
None of the above worked, my frame is still at the bottom and hidden behind the Mac dock.
以上都没有奏效,我的框架仍在底部并隐藏在 Mac Dock 后面。
回答by yggdraa
Location should be set after you packed your frame (wich calculates size of the frame). And after that it should be made visible (it is hidden by default otherwize).
打包好框架后应设置位置(计算框架的大小)。之后它应该是可见的(默认情况下它是隐藏的)。
pack();
setLocationRelativeTo(null);
setVisible(true);