Set JPanel Size

I am programming an application that works with swing components, I notice one thing on which I would explain I have these classes:

  • this is the enum on which i create the gui dimension

    public enum GuiDimension { WIDTH(700), HEIGHT(400); private final int value; private GuiDimension(int value) { this.value = value; } public int getValue(){ return value; } } 
  • this class that launches the application

     private GamePanel gamePanel = new GamePanel(); public static void main(String[] args) { new MainFrame(); } public MainFrame() { initGameFrame(); } private void initGameFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(gamePanel); setResizable(false); setUndecorated(true); pack(); setVisible(true); setLocationRelativeTo(null); } } 
  • and this class that sets the panel size

     public class GamePanel extends JPanel { public GamePanel() { setPreferredSize(new Dimension(GuiDimension.WIDTH.getValue(),GuiDimension.HEIGHT.getValue())); //it makes other stuff that are not of interest for this contest } } 

    What I noticed is that the enumerations are not integers, but objects, but when I return

    • GuiDimension.WIDTH.getValue()

    • GuiDimension.HEIGHT.getValue()

they return integers that can be used for other purposes after they are accepted.

now if i insert this in :

 SetSize (new Dimension (GuiDimension.WIDTH.getValue (), GuiDimension.HEIGHT.getValue ())); 

or

 SetSize (GuiDimension.WIDTH.getValue (), GuiDimension.HEIGHT.getValue ()); 

instead, which I inserted as an example

 setPreferredSize(new Dimension(GuiDimension.WIDTH.getValue(),GuiDimension.HEIGHT.getValue())); 

the frame is displayed with the wrong size, and I do not understand why. If GuiDimension.WIDTH.getValue () and GuiDimension.WIDTH.getValue ()) are true for setPreferredSize (...) ,

why is it not the same for setSize (int,int) and for setSize(Dimension) ?

when testing this simple code you can see it.

+6
source share
1 answer

Most layout managers will ignore calls with the size of the component, but will respect its preferred size, and sometimes the minimum and maximum, and so when you call pack() your size will change to what layout managers and component components prefer, the size thinks this should be the best size.

By the way, for kleopatra (Jeanette), if you absolutely need to set the preferred size of the component, you'd better refuse getPreferredSize() than call setPreferredSize(...) . The latter can be overridden by calling setPreferredSize(...) on the same component elsewhere, while the former cannot.

As an aside, in your code example you use WIDTH twice and don't seem to use HEIGHT.


Edit
You had a comment that was deleted regarding package sizes and components. My answer to this was:

The pack() method requests that layout managers perform their component layouts, and its layout managers, which are important here — what they look at — are size compared to preferred ones. If you read javadoc and tutorials for most layout managers, you'll see that they prefer the most preferred sizes. Some, such as BoxLayout, also look at maximum size and minimum size.

+6
source

All Articles