Java – combination of central JFrame and pack()
•
Java
I'm trying to package the JFrame I used as () and I got it, but I don't think it's a clean way
JFrame window = new JFrame(); //filling //window //with //stuff window.pack(); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); int x = (dim.width - window.getPreferredSize().width) / 2,y = (dim.height - window.getPreferredSize().height) / 2; window.setBounds(x,y,window.getPreferredSize().width,window.getPreferredSize().height);
翻译错误 TIMEOUT
Any better ideas?
Solution
To center the window in the screen, you need to call window. before the pack () call and after the window is visible. setLocationRelativeTo(null):
JFrame window = new JFrame(); ... window.pack(); window.setLocationRelativeTo(null); window.setVisible(true);
According to the window #setlocationrelativeto (component C) document:
on the other hand
Some developers may suggest that you use window #setlocationbyplatform (boolean flag) instead of setlocationrelativeto (...) to respect the default location of the native window system of the platform on which the desktop application is running This makes sense because your application must be designed to run on different platforms with different window systems and plaf
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码