Jack O'Connor
Jack O'Connor

Reputation: 195

Java how to make JFrames start off as a maximised window

I want to know how to make a java JFrame start out maximised. I don't want it to be fullscreen (no window around it) I just want it to start out like a normal program such as a web browser.

I already know about using:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

To get the screen size, but when you apply this Dimension to the JFrame it can still be dragged around even though it takes up almost all of the screen. You can press the maximse button to stop this but I would rather that the window started out maximised.

Also, I fear for the effects that maximising the window would have upon the contents of the window.

How do I go about doing this?

Upvotes: 5

Views: 5765

Answers (1)

Cyrille Ka
Cyrille Ka

Reputation: 15538

Use java.awt.Frame.setExtendedState():

public static void main(String[] args)  {

    JFrame frame = new JFrame();
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setVisible(true);
}

Upvotes: 9

Related Questions