To center an app window, you have to tap the Shift key three times, consecutively. The shortcut should not clash with any app on Windows 10. This app is especially useful if you have apps that consistently open off-screen because it has an option to automatically center new apps/windows that you open.
Just click on form and go to JFrame properties, then Code tab and check Generate Center .
To change the position of JFrame on the screen, JFrame provides the method JFrame. setlocation(int x, int y), you need two parameters 'x' represents the position of the x axis and 'y' represents the position of the y axis. The upper left corner of your screen is (0,0). If you give NULL as parameter to JFrame.
From this link
If you are using Java 1.4 or newer, you can use the simple method setLocationRelativeTo(null) on the dialog box, frame, or window to center it.
This should work in all versions of Java
public static void centreWindow(Window frame) {
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
frame.setLocation(x, y);
}
setLocationRelativeTo(null)
should be called after you either use setSize(x,y)
, or use pack()
.
Note that both the setLocationRelativeTo(null) and Tookit.getDefaultToolkit().getScreenSize() techniques work only for the primary monitor. If you are in a multi-monitor environment, you may need to get information about the specific monitor the window is on before doing this kind of calculation.
Sometimes important, sometimes not...
See GraphicsEnvironment javadocs for more info on how to get this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With