0

I have some panels in a card layout container (no idea if that is correct terminology). I can't find a way to set the location, or size of these panels inside the container. I tried setBounds and setLayout(null) and I still can't get anything to change. These are my fields and the constructor. I've gotten my frame working and I can see and use the buttons to change cards, but I really can't change much else about the cards. I set the two card panels two have different backgrounds, but they only make a small boarder of color around the button and leave it in the centre of the screen.

I also don't understand why this isn't pasting my code properly... So sorry!

public class TestPanel extends JPanel implements ActionListener {

CardLayout cl = new CardLayout();

private JPanel panelCont = new JPanel();

private JPanel panel1 = new JPanel();

private JPanel panel2 = new JPanel();

private static JButton but1 = new JButton("Change panels");

private static JButton but2 = new JButton("Change back");

public TestPanel() {

    panelCont.setLayout(cl);
    panel1.add(but1);
    panel2.add(but2);
    panel1.setBackground(Color.black);
    panel2.setBackground(Color.blue);
    panelCont.add(panel1, "1");
    panelCont.add(panel2, "2");
    cl.show(panelCont, "1");
    but1.addActionListener(this);
    but2.addActionListener(this);
    add(panelCont);
}
}

Thanks. I apologise in advance. I'm finding it hard to understand card layout.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Cammers
  • 11
  • 7
  • To use code formatting, select the sample and click the `{}` button above the messaged posting/editing form. – Andrew Thompson May 27 '13 at 13:46
  • *"I tried setBounds and setLayout(null) and I still can't get anything to change."* Don't use those methods. Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. For a robust GUI, instead use layout managers, or combinations of them, along with **layout padding** & **borders** for white space, to organize the components. For better help sooner, post an [SSCCE](http://sscce.org/), the size of the panels depends on the layout of whatever container they are added to. – Andrew Thompson May 27 '13 at 13:48

2 Answers2

3

A CardLayout respects the preferred size of the panels added to the layout. That is the size will be the size of the largest panel added to the layout.

I set the two card panels two have different backgrounds, but they only make a small boarder of color around the button and leave it in the centre of the screen.

The default layout for a panel is the FlowLayout. A FlowLayout by default has a 5 pixel horizontal/vertical gap around each component. So the preferred size of your panel is the size of the button plus the 5 pixel gap.

The panel is displaying correctly. When you add other components to the panel the size will change as required.

camickr
  • 321,443
  • 19
  • 166
  • 288
2

It's not clear where you pack() the enclosing Window. By default, pack() causes a panel having CardLayout to adopt the the size of the largest panel's preferred size, which is determined by the size of its contents. This example uses setPreferredSize() to specify an arbitrary size, but you can override getPreferredSize() as shown here.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045