java 居中 jFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11232131/
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
Centering a jFrame
提问by Loop Masters
I'm trying to locate my frames at the center of the screen , I know that this code must work well:
我正在尝试将我的框架定位在屏幕中央,我知道此代码必须运行良好:
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((screen.getWidth() - getWidth()) /2);
int y = (int) ((screen.getHeight() -getHeight()) /2);
setLocation(x, y);
or this one :
或者这个:
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
setSize(screenWidth / 2, screenHeight / 2);
setLocation(screenWidth / 4, screenHeight / 4);
I have tried all the possible codes but I don't know why they don't work, the just move the frame to the bottom right corner of the screen I have used setRelative to null but again it doesn't work.
我已经尝试了所有可能的代码,但我不知道为什么它们不起作用,只是将框架移动到屏幕的右下角我已经使用 setRelative 为 null 但它再次不起作用。
I could finally bring the frame very close to the center by multiplying the width by 5 and dividing it by something but I know that this is not the right way.
我终于可以通过将宽度乘以 5 并将其除以某些东西来使框架非常靠近中心,但我知道这不是正确的方法。
could anyone please explain to me what could be wrong ?
任何人都可以向我解释什么可能是错的?
Problem Fixed :
问题修复:
solution: I found the best way myself , if you ever come up with the same problem simply let the netbeans do that automatically by going to properties and checking generate center , forget about the stupid codes.
解决方案:我自己找到了最好的方法,如果您遇到同样的问题,只需让 netbeans 通过转到属性并检查 generate center 自动执行此操作,忘记愚蠢的代码。
回答by Mikle Garin
The easiest way to center frame on the main screen (there are options if you have multiply screens) is to use setLocationRelativeTo(...) frame method with null as argument:
在主屏幕上居中框架的最简单方法(如果您有多个屏幕,则有选项)是使用 setLocationRelativeTo(...) 框架方法,并以 null 作为参数:
JFrame frame = new JFrame ();
frame.setSize ( 500, 300 );
frame.setLocationRelativeTo ( null );
frame.setVisible ( true );
In case that doesn't work - try this one (direct coordinates):
如果这不起作用 - 试试这个(直接坐标):
Dimension ss = Toolkit.getDefaultToolkit ().getScreenSize ();
Dimension frameSize = new Dimension ( 500, 300 );
JFrame frame = new JFrame ();
frame.setBounds ( ss.width / 2 - frameSize.width / 2,
ss.height / 2 - frameSize.height / 2,
frameSize.width, frameSize.height );
frame.setVisible ( true );
Last and "ultimate" example - it will display centered frame @ each available system screen:
最后一个“终极”示例 - 它将在每个可用的系统屏幕上显示居中的框架:
public static void main ( String[] args )
{
Dimension frameSize = new Dimension ( 500, 300 );
for ( GraphicsDevice screen : getGraphicsDevices () )
{
GraphicsConfiguration gc = screen.getDefaultConfiguration ();
Rectangle sb = gc.getBounds ();
JFrame frame = new JFrame ( gc );
frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
frame.setBounds ( sb.x + sb.width / 2 - frameSize.width / 2,
sb.y + sb.height / 2 - frameSize.height / 2, frameSize.width,
frameSize.height );
frame.setVisible ( true );
}
}
public static List<GraphicsDevice> getGraphicsDevices ()
{
List<GraphicsDevice> devices = new ArrayList<GraphicsDevice> ();
for ( GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment ()
.getScreenDevices () )
{
if ( gd.getType () == GraphicsDevice.TYPE_RASTER_SCREEN )
{
if ( gd == GraphicsEnvironment.getLocalGraphicsEnvironment ()
.getDefaultScreenDevice () )
{
devices.add ( 0, gd );
}
else
{
devices.add ( gd );
}
}
}
return devices;
}
回答by Honza Zidek
It's enough if you use
如果你使用就足够了
setLocationRelativeTo(null);
According to Javadoc for java.awt.Window.setLocationRelativeTo(),
根据java.awt.Window.setLocationRelativeTo() 的 Javadoc,
If the component is null, ... the window is placed in the center of the screen.
如果组件为空,... 窗口将放置在屏幕中央。
回答by mKorbel
I've already tried it, it is not working , could anything be wrong with the netbeans ??
我已经试过了,它不起作用,netbeans 有什么问题吗??
maybe not wrong with netbeans
netbeans 可能没有错
I'm trying to locate my frames at the center of the screen
我正在尝试将我的框架定位在屏幕中央
you can to try
你可以试试
JFrame frame = new JFrame ();
frame.pack(); // or frame.setSize(int, int);
frame.setLocationByPlatform(true);
frame.setVisible(true);
回答by Nerdizzle
I'd bet dollars to donuts that you set the location relative to null / set the location through x and y coordinates before setting the size of the JFrame. If you haven't set the size yet, it is treated as 0x0, setting the dead center of it to the center of the screen. If you then change the size, it leaves the topleft corner where it was (the center of the screen) and scales it towards the bottom-right of your screen.
在设置 JFrame 的大小之前,我敢打赌你设置相对于 null 的位置/通过 x 和 y 坐标设置位置的甜甜圈。如果你还没有设置大小,则将其视为0x0,将其死点设置为屏幕中心。如果您随后更改大小,它会将左上角留在原来的位置(屏幕中心)并将其缩放到屏幕的右下角。
In other words, make setSize() come before centering it on the screen.
换句话说,让 setSize() 在它在屏幕上居中之前出现。
Everything else you did looks fine to me.
你做的其他一切在我看来都很好。