Unable to get JPanel for the correct size in Swing

I just learn Swing and create a sample GUI for myself as a practice. Very simple, it has three panels on the western, central, and eastern sides of the JFrame . On the last panel there is another panel and a button. The panel is an extension of JPanel , DrawPanel . He dials a random Rectangle every time the button below it is pressed. This is my code:

 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Tester { public static DrawPanel panelEastDrawPanelCenter; public static void main() { Tester gui = new Tester(); gui.go(); } public void go() { JFrame frame = new JFrame("This is the title"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create all the Western Panel components JPanel panelWest = new JPanel(); // panelWest.setLayout(new BorderLayout()); JButton panelWestButtonWest = new JButton("Western-most West Button"); JButton panelWestButtonCenter = new JButton("Western-most Center Button"); JButton panelWestButtonEast = new JButton("Western-most East Button"); // Create all Center Panel components JPanel panelCenter = new JPanel(); panelCenter.setBackground(Color.darkGray); JButton panelCenterButtonCenter = new JButton("Center Button"); // Create all East Panel components JPanel panelEast = new JPanel(); panelEastDrawPanelCenter = new DrawPanel(); panelEastDrawPanelCenter.setSize(50,50); JButton panelEastButtonSouth = new JButton("Repaint"); panelEastButtonSouth.addActionListener(new panelEastButtonSouthListener()); // Add everything to the GUI // West Panel frame.getContentPane().add(BorderLayout.WEST, panelWest); panelWest.add(BorderLayout.WEST, panelWestButtonWest); panelWest.add(BorderLayout.CENTER, panelWestButtonCenter); panelWest.add(BorderLayout.EAST, panelWestButtonEast); // Center Panel frame.getContentPane().add(BorderLayout.CENTER, panelCenter); panelCenter.add(BorderLayout.CENTER, panelCenterButtonCenter); // East Panel frame.getContentPane().add(BorderLayout.EAST, panelEast); panelEast.add(panelEastDrawPanelCenter); panelEast.add(panelEastButtonSouth); frame.pack(); //frame.setSize(frame.getWidth(), 500); frame.setVisible(true); } class panelEastButtonSouthListener implements ActionListener { public void actionPerformed(ActionEvent event) { panelEastDrawPanelCenter.repaint(); } } class DrawPanel extends JPanel // JPanel that displays a rectangle upon clicking the button "Repaint" { public void paintComponent(Graphics g) { g.setColor(Color.WHITE); // Removes previous rectangle g.fillRect(0,0, this.getWidth(),this.getHeight()); g.setColor(randColor()); // Puts a new rectangle on screen, rand size and color int height = (int)(Math.random() * this.getHeight()); int width = (int)(Math.random() * this.getHeight()); int x = (int)(Math.random() * 20); int y = (int)(Math.random() * 20); g.fillRect(x,y, height,width); } public Color randColor() { int r = (int)(Math.random() * 255); int g = (int)(Math.random() * 255); int b = (int)(Math.random() * 255); return new Color(r, g, b); } } } 

The problem I am facing is that although I explicitly setSize() for the DrawPanel object ( panelEastDrawPanelCenter ) is 50 and 50 times when I run the program, it ( DrawPanel ) still represents a small panel next to the button and the East panel (DrawPanel container and button) still have the same width (and will never expand). I understand what I can say

 frame.pack(); frame.setSize(frame.getWidth(), 500); 

if I set the east panel to use either the BorderLayout or BoxLayout layout, and that will make the DrawPanel display bigger.

  • But I don’t understand why setting the size of the DrawPanel object does not actually change its size and why it remains small no matter what I do setSize(50,50);
  • How do I make the middle panel stop resizing so huge that the East panel can change on a larger scale? Or how to resize the east panel?
+4
source share
3 answers

BorderLayout respects the preferred width of its western and eastern components. And this corresponds to the preferred width and height of its central component. So your DrawPanel should override getPreferredSize() and return the appropriate preferred size.

Since it is located in the center of BorderLayout, which also has a south component, the preferred width of the east panel will be the maximum width of the preferred width of the drawing panel and button.

+8
source

Have you tried using panel.setPerferredSize(Dimension size);

+2
source

Try the setPreferredSize(Dimension) method instead of setSize(int, int) and call pack() after that.

+2
source

All Articles