简体   繁体   English

JFrame在MAC OSX上设置窗口中心的位置

[英]JFrame set location at center of window on MAC OSX

Any body please can please help me, how to center a JFrame on Mac. 任何正文请随时帮助我,如何在Mac上集中JFrame OS X? OS X?

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底座后面。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM