Java JFrame Size according to screen resolution

94,572

Solution 1

You are calling pack() which changes the frame size so it just fits the components inside. That's why it is shrinking back I think. Remove the pack() line and it should work.

Solution 2

You can try using this to maximize the frame:

this.setExtendedState(JFrame.MAXIMIZED_BOTH);

Solution 3

Another way to do this is:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
pack();
setSize(screenSize.width,screenSize.height);

Solution 4

Calling pack() is vital to a correctly functioning GUI. Call it after all the components have been added, to have it validate the container and set it to it's natural size.

Then call setSize() & related methods like setBounds() afterwards.

Solution 5

Try this... Select all components of JFrame, right click, select 'Auto Resizing', check for both Horizontal and Vertical, close .

Share:
94,572
Asghar
Author by

Asghar

Write code to survive : Java, PHP, amazon web services, cloud computing, C#, ASP.net. Python, web services. Digital persona.

Updated on April 03, 2020

Comments

  • Asghar
    Asghar about 4 years

    I created java GUI using myEclipse Matisse. when my Screen Resolution is 1024x768 it works fine but when i change resolution my GUI is not working fine. I want my GUI window should be re-sized according to the screen Resolution I am extending JFrame to create the main window.

    public class MyClass extends JFrame {
    
        //I am putting some controls here.
    
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(0,0,screenSize.width, screenSize.height);
        setVisible(true);
    
        pack();
    }
    

    this is not working, what ever i do, setting size hardcoded or by ToolKit using, the Frame Size Remains same.

  • Andrew Thompson
    Andrew Thompson almost 13 years
  • Petar Minchev
    Petar Minchev almost 13 years
    -1, really?!?! I am explaining why it doesn't work... Sometimes you don't need the pack. It is up to him...
  • Andrew Thompson
    Andrew Thompson almost 13 years
    +1 For being the 1st answer not to advise removing the call to pack(). ;)
  • Andrew Thompson
    Andrew Thompson almost 13 years
    "Sometimes you don't need the pack." Sure, but besides "when you don't add anything to the frame", what other situations are included in that 'sometimes'?
  • developmentalinsanity
    developmentalinsanity almost 13 years
    download.oracle.com/javase/tutorial/uiswing/components/… See point 4. pack() is "preferable" to setSize. This doesn't make it wrong to not use it.
  • Andrew Thompson
    Andrew Thompson almost 13 years
    @ That same point goes on to add "..pack leaves the frame layout manager in charge of the frame size, and layout managers are good at adjusting to platform dependencies and other factors that affect component size."* So.. now we're adding null layouts to that 'sometimes'? That wreaks even more. Even if you are doing custom painting across the entire content area of the frame, that painting needs to be done in a JComponent that is then added to the frame. When you add it, it is simplicity itself to size & position the component using a layout manager.
  • developmentalinsanity
    developmentalinsanity almost 13 years
    The fact that the layout manager isn't in charge of the frame size doesn't mean you're not using a layout manager. In a maximized window (like the answer by Kowser you like), the layout manager isn't in charge of the window size and has to lay out the component within a fixed size. Not using pack results in the same scenario, just with the window size not necessarily taking up the entire screen.
  • Petar Minchev
    Petar Minchev almost 13 years
    I was the downvoter, because at first I thought it doesn't answer the OP question. Sorry for that. Now I see it is an OK answer and I give it a +1:)
  • Andrew Thompson
    Andrew Thompson almost 13 years
    "the layout manager isn't in charge of the window size and has to lay out the component within a fixed size." Nobody ever said the layout manager is (or should be) in charge of the size of the full-screen window. The layout manager is responsible for the size & layout of the children of the window.
  • Syed Muhammad Mubashir
    Syed Muhammad Mubashir over 11 years
    i used pack() after that i use the code for resizing but it's not working for me
  • gumuruh
    gumuruh about 2 years
    why the pack() called before calling setSize ?