Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set size of JPanel

Tags:

java

swing

i' m programming an application which works with swing components, i notice one thing on which i would an explanation i have these classes:

  1. this enum on which i instantiate 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;
     }
    }
    
  2. this class that starts 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);
        }
    }
    
  3. and this class that set the size of the panel

    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, it is true that enums are not really integers but objects ,but when I return

    • GuiDimension.WIDTH.getValue()

    • GuiDimension.HEIGHT.getValue()

they return integers that can well be used for other purposes once it has been taken.

now if I insert this on:

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

or

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

instead of this,which i inserted in the example

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

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

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

when tested this simple code you can see that.

like image 704
OiRc Avatar asked Mar 15 '14 22:03

OiRc


People also ask

How do I increase the size of a JPanel?

JPanel redPanel = new JPanel(); We change the background colour to red, and give the JPanel a size using setSize(width, height). We also give the widget a location using setLocation(x pixels, y pixels).

How do I change the size of a JFrame?

Change window Size of a JFrame To resize a frame, There is a method JFrame. setSize(int width, int height) which takes two parameters width and height.

How do I change the layout of a JPanel?

You can set a panel's layout manager using the JPanel constructor. For example: JPanel panel = new JPanel(new BorderLayout()); After a container has been created, you can set its layout manager using the setLayout method.

How do I change the size of a Jlabel?

You can set a fixed the size by setting the minimum, preferred and maximum size: setMinimumSize(width, height); setPreferredSize(width, height); setMaximumSize(width, height); As the Link from MadProgrammer, you need to override these methods, not using them from outside, based on the reasons mentioned in this link.


1 Answers

Most of the layout managers will ignore calls a component's size but will respect its preferredSize, and sometimes the minimum and maximum, and so when you call pack(), your size will change to what the layout managers and the constituent component preferred sizes think should be the best size.

Incidentally, per kleopatra (Jeanette), if you absolutely need to set a component's preferred size, you're better off overriding getPreferredSize() than by calling setPreferredSize(...). The latter can be overridden by calling setPreferredSize(...) on the same component elsewhere while the former can't.

As an aside, in your example code, you are using WIDTH twice and appear to be not using HEIGHT.


Edit
You had a comment that was deleted regarding pack and component sizes. My reply to it was:

The pack() method requests that the layout managers do their laying out of components, and its the layout managers that matter here -- what do they look at, the size vs. the preferredSizes. If you read the javadoc and tutorials for most of the layout managers, you'll see that they respect the preferred sizes most. Some, like BoxLayout, also look at the maximum size and minimum size as well.

like image 101
Hovercraft Full Of Eels Avatar answered Oct 28 '22 07:10

Hovercraft Full Of Eels