Java 无论显示器分辨率如何,如何将 JFrame 设置为居中显示?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2442599/
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
How to set JFrame to appear centered, regardless of monitor resolution?
提问by AmateurProgrammer
While working with Java, I find it hard to position my main window in the center of the screen when I start the application.
在使用 Java 时,我发现启动应用程序时很难将主窗口定位在屏幕中央。
Is there any way I can do that? It doesn't have to be vertically centered, horizontal alignment is the more important goal for me. But vertical alignment is also welcome.
有什么办法可以做到吗?它不必垂直居中,水平对齐对我来说是更重要的目标。但垂直对齐也是受欢迎的。
采纳答案by Hyman
I always did it in this way:
我一直是这样做的:
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
where this
is the JFrame involved.
this
涉及的 JFrame在哪里。
回答by JRL
Use setLocationRelativeTo(null)
This method has a special effect when you pass it a null
. According to the Javadoc:
这个方法在你传递一个null
. 根据 Javadoc:
If the component is null, or the GraphicsConfiguration associated with this component is null, the window is placed in the center of the screen.
如果该组件为空,或者与该组件关联的 GraphicsConfiguration 为空,则窗口将放置在屏幕中央。
This should be done after setting the size or calling pack()
, but before setting it visible, like this:
这应该在设置 size 或调用之后完成pack()
,但在将其设置为可见之前,如下所示:
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
回答by Greg
You can call JFrame.setLocationRelativeTo(null)
to center the window. Make sure to put this before JFrame.setVisible(true)
您可以调用JFrame.setLocationRelativeTo(null)
以将窗口居中。确保把这个放在前面JFrame.setVisible(true)
回答by Asaf Magen
i am using NetBeans IDE 7.2.1 as my developer environmental and there you have an option to configure the JForm properties.
我使用 NetBeans IDE 7.2.1 作为我的开发人员环境,您可以选择配置 JForm 属性。
in the JForm Properties go to the 'Code' tab and configure the 'Generate Center'. you will need first to set the Form Size Policy to 'Generate Resize Code'.
在 JForm 属性中,转到“代码”选项卡并配置“生成中心”。您首先需要将表单大小策略设置为“生成调整大小代码”。
回答by Gstuntz
I am using NetBeans IDE 7.3 and this is how I go about centralizing my JFrame Make sure you click on the JFrame Panel and go to your JFrame property bar,click on the Code bar and select Generate Center check box.
我正在使用 NetBeans IDE 7.3,这就是我集中 JFrame 的方式 确保单击 JFrame 面板并转到 JFrame 属性栏,单击代码栏并选择生成中心复选框。
回答by Gstuntz
As simple as this...
就这么简单...
setSize(220, 400);
setLocationRelativeTo(null);
or if you are using a frame then set the frame to
或者,如果您使用的是框架,则将框架设置为
frame.setSize(220, 400);
frame.setLocationRelativeTo(null);
For clarification, from the docs:
为了澄清,来自文档:
If the component is null, or the GraphicsConfiguration associated with this component is null, the window is placed in the center of the screen.
如果该组件为空,或者与该组件关联的 GraphicsConfiguration 为空,则窗口将放置在屏幕中央。
回答by Kingsley Ijike
If you use NetBeans, simply click on the frame on the design view, then the code tab on its properties. Next, check 'Generate Center'. That will get the job done.
如果您使用 NetBeans,只需单击设计视图上的框架,然后单击其属性上的代码选项卡。接下来,选中“生成中心”。这将完成工作。
回答by ???? ????
回答by The Appsolit
You can use this method, which allows the JFrame to be centered and full screen at the same time.
您可以使用此方法,它允许 JFrame 同时居中和全屏显示。
yourframe.setExtendedState(JFrame.MAXIMIZED_BOTH);
回答by The Appsolit
If you explicitly setPreferredSize(new Dimension(X, Y));
then it is better to use:
如果你明确setPreferredSize(new Dimension(X, Y));
那么最好使用:
setLocation(dim.width/2-this.getPreferredSize().width/2, dim.height/2-this.getPreferredSize().height/2);
setLocation(dim.width/2-this.getPreferredSize().width/2, dim.height/2-this.getPreferredSize().height/2);