Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 958
Jack O'Connor Avatar asked Feb 14 '13 17:02

Jack O'Connor


People also ask

How do I make my JFrame maximized by default?

By default, we can minimize a JFrame by clicking on minimize button and maximize a JFrame by clicking on maximize button at the top-right position of the screen. We can also do programmatically by using setState(JFrame. ICONIFIED) to minimize a JFrame and setState(JFrame. MAXIMIZED_BOTH) to maximize a JFrame.

How do I change the size of my Java window?

To resize a window perform one of the following actions: Point to a corner of the window. The mouse pointer changes to indicate that you can resize the window. Grab the corner and drag the window to the new size.

How do you make a JFrame fit the screen?

Select all components of JFrame, right click, select 'Auto Resizing', check for both Horizontal and Vertical, close . Show activity on this post. Calling pack() will usually result in the window being resized to fit the contents' preferred size.

How do you remove minimize maximize close button from JFrame?

Try this code: JFrame frame = new JFrame("Example"); frame. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); frame.


1 Answers

Use java.awt.Frame.setExtendedState():

public static void main(String[] args)  {

    JFrame frame = new JFrame();
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setVisible(true);
}
like image 52
Cyrille Ka Avatar answered Nov 06 '22 12:11

Cyrille Ka