Здравствуйте!
Почему такой код работает:
JPanel comboBoxPane = new JPanel(); //use FlowLayout
String comboBoxItems[] = { BUTTONPANEL, TEXTPANEL };
JComboBox cb = new JComboBox(comboBoxItems);
cb.setEditable(false);
cb.addItemListener(this);
comboBoxPane.add(cb);
//Create the "cards".
JPanel card1 = new JPanel();
card1.add(new JButton("Button 1"));
card1.add(new JButton("Button 2"));
card1.add(new JButton("Button 3"));
JPanel card2 = new JPanel();
card2.add(new JButton());
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);
а если сделать вместо:
JPanel card2 = new JPanel();
card2.add(new JButton());
так:
JPanel card2 = null;
try {
card2 = new WrmJPanel();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (AWTException e) {
throw new RuntimeException(e);
}
где:
public class WrmJPanel extends JPanel {
private JButton jButton1;
public JPanel panel = new JPanel(new FlowLayout());
public WrmJPanel() {
jButton1 = new JButton("A!");
panel.add(jButton1);
}
то при переключении на card2 там пусто.
Что не так.