任何机构都可以帮助我,如何在Mac上居中JFrame
。 OS X?
我努力了:
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基座后面。
最佳答案
包装好框架后,应设置位置(由其计算框架的尺寸)。
之后,应该使它可见(默认情况下将其隐藏otherwize)。
pack();
setLocationRelativeTo(null);
setVisible(true);