Dan Dan - 10 months ago 60
Java Question

Maximizing JFrame in Java

There have been several questions related to this, for example here and here, that both say the way to maximize a

is to use the following code:

frame.setExtendedState(JFrame.MAXIMIZED_BOTH); //Some answers have these lines
frame.setVisible(true); //reversed

However, for me, not sure if this is a windows 10 bug/java 8 bug or not, when I use this code the result is this (no matter which way round the two lines of code above are):

Offset <code>JFrame</code>

As you can see in the image, the window is the correct size, however, it slightly overlays the bottom, and it is slightly offset from the left. Is there a way to fix this problem, or actually maximise the program by hitting the maximise button on the
with code?


Here is a MCVE that demonstrates the problem:

import java.awt.GridLayout;
import javax.swing.JFrame;

public class CDBurner extends JFrame {
private static final long serialVersionUID = -6027473114929970648L;

private CDBurner() {
setLayout(new GridLayout(1, 1));
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

public static void main(String[] args) {
new CDBurner();

Answer Source

The line


sets the position of the frame. If it comes after

setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

it changes the location of the frame after it maximizes, causing the gap you observed. You should remove that line since maximizing the frame sets its position anyway (though you can just put it before setting the extended state, in which case it does nothing).