My JFrame
window always starts at the upper left corner. I want it to start in the middle of the screen. How can I solve this problem?
Asked
Active
Viewed 343 times
0

Salman Ahmed
- 141
- 1
- 3
- 14
4 Answers
2
Use setBounds
to declare the window's x
and y
position, along with it's height
and width
, if you know the size
of the screen.
The other option is to use the frame.setLocationRelativeTo()
method, and pass in null
.

christopher
- 26,815
- 5
- 55
- 89
1
Another simple method:
frame.setLocationRelativeTo(null);
0
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
Source: How to set JFrame to appear centered, regardless of monitor resolution?
0
Try this:
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);
}

sareeshmnair
- 441
- 1
- 6
- 18