【发布时间】:2014-07-10 11:25:55
【问题描述】:
我有一个用于 InternalFrame 的 cardLayout
public class CardWindow extends InternalWindowTemplate{
CardLayout cardLayout;
private final JLabel label1, label2;
private final JPanel panel1, panel2, cards, card1, card2;
public CardWindow(){
super("Elements", false, true, false, false,438,520);
this.panel1= new JPanel();
panel1.setBackground(Color.red);
this.panel2= new JPanel();
panel2.setBackground(Color.CYAN);
this.label1= new JLabel("Label #1");
panel1.add(label1);
this.label2= new JLabel("Label #2");
panel2.add(label2);
// create first card
this.card1 = new JPanel();
card1.setBackground(Color.ORANGE);
card1.add(panel1);
card1.add(panel2);
// create second card
this.card2 = new JPanel();
card2.setBackground(Color.BLACK);
label1.setText("1");
label2.setText("2");
card2.add(panel1);
card2.add(panel2);
this.cards = new JPanel();
this.cardLayout = new CardLayout();
cards.setLayout(cardLayout);
cards.add(card1);
cards.add(card2);
addComponentToInternalWindow(cards);
}
最后一条语句来自我的内部窗口抽象类
public abstract class InternalWindowTemplate{
//...
public void addComponentToInternalWindow(Component component){
setFlowLayout();
getContainer().add(component);
}
}
当我运行它时,我只能看到 card1 的橙色背景,但看不到我添加的面板。这是为什么呢?
【问题讨论】:
标签: java swing jpanel cardlayout