2012-03-29 103 views
3

任何机构请帮助我,如何在Mac上对中JFrame。 OS X?JFrame在MAC OSX上的窗口中心设置位置

我曾尝试:

this.setLocationRelativeto(null); 

this.setLocationRelativeto(this); 
this.setLocationRelativeto(getRootPane()); 

..和

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

以上都不工作,我的框架仍处于底部,隐藏了Mac码头后面。

+1

参见[如何最佳位置摇摆GUI的(http://stackoverflow.com/a/7143398/418556)[什么是Java中设置JFrame的位置最好的做法是什么?(HTTP:/ /stackoverflow.com/a/7778332/418556)。请注意,这些答案都不涉及将GUI集中在屏幕上。有更好的选择。 – 2012-03-30 04:12:43

回答

8

位置应该在打包完帧后设置(计算帧的大小)。 之后,它应该是可见的(它默认情况下隐藏otherwize)。

pack(); 
setLocationRelativeTo(null); 
setVisible(true);