Wrong JFrame wrapping using JPanel in borderlayout

I am writing a simple Java application that does some particle simulation on a bunch of sheep (don't ask). For this, I want a JPanel window for graphics (which will be resizable with a simple combobox containing some standard resolutions) and some other elements like buttons to start and pause modeling, etc.

My question: I am using the JFrame.pack method to put everything together nicely with borderLayout. But for some reason the JPanel is not wrapped correctly, it looks like the wrapping is ignoring it, so the window is resized to only fit the two buttons I currently have. What am I doing wrong?

This is the code so far (newbie bit, so no comments about my infirmity, if any;)):

public class Window {
 public Sheepness sheepness;

 public ButtonPanel buttonPanel;
 public PaintPanel paintPanel;
 public JFrame frame;

 public Window(Sheepness sheepness, int width, int height) {
  this.sheepness = sheepness;

  frame = new JFrame("Sheepness simulation");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  //frame.setSize(width, height);

  BorderLayout frameLayout = new BorderLayout();
  JPanel background = new JPanel(frameLayout);
  background.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

  buttonPanel = new ButtonPanel(this);
  background.add(BorderLayout.SOUTH, buttonPanel.buttonBox);

  paintPanel = new PaintPanel(this);
  paintPanel.setSize(600, 600);
  background.add(BorderLayout.CENTER, paintPanel);

  frame.getContentPane().add(background);
  frame.pack();
  frame.setResizable(false);
  frame.setVisible(true);
 }
}

public class PaintPanel extends JPanel {
 public Window window;

 public PaintPanel(Window window) {
  this.window = window;
 }

 @Override
 public void paintComponent(Graphics g) {
  g.setColor(Color.blue);
  g.fillRect(0, 0, 300, 200);
 }
}

public class ButtonPanel {
 public Window window;
 public Box buttonBox;

 public JButton startButton;
 public JButton resetButton;

 public ButtonPanel(Window window) {
  this.window = window;

  buttonBox = new Box(BoxLayout.X_AXIS);

  startButton = new JButton("Start");
  startButton.addActionListener(new startButtonListener());
  buttonBox.add(startButton);

  resetButton = new JButton("Reset");
  resetButton.addActionListener(new resetButtonListener());
  buttonBox.add(resetButton);
 }
}
      

+2


source to share


1 answer


Try:

paintPanel.setPreferredSize(600, 600);

      



As the size Window.pack()

to the preferred sizes of its subcomponents, and JPanel

gets its preferred size from its children (in your case, none).

+4


source







All Articles