2013-10-07 194 views
0

即時嘗試使用卡片佈局,並且我有兩個按鈕在頂端,supoose改變卡,但由於某種原因它不會工作,下一個方法的工作,但工作,但展會或第一\最後的犯規着,當然我不能在下次使用,因爲我要爲每一個按鍵一個特定的卡,這裏是我的代碼:試圖使用卡片佈局,但不能使顯示工作

 cards = new CardLayout(); 
    cardPanel = new JPanel(); 
    cardPanel.setLayout(cards); 
    cards.show(cardPanel, "gapas"); 

    JPanel firstCard = new JPanel(); 
    firstCard.setBackground(Color.WHITE);; 


    JPanel secondCard = new JPanel(); 
    secondCard.setBackground(Color.blue); 


    cardPanel.add(firstCard, "kalam"); 
    cardPanel.add(secondCard, "gapan"); 

    guiFrame.add(tabsPanel,BorderLayout.NORTH); 
    guiFrame.add(cardPanel,BorderLayout.CENTER); 
    guiFrame.setVisible(true); 
} 

ActionListener action = new ActionListener() { 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     if(e.getActionCommand().matches("kalam")){ 
      cards.show(cardPanel,"kalam"); 
      System.out.println("kalam"); 
     } 
     else{ 
      cards.show(cardPanel, "gapas"); 
      System.out.println("gapas"); 
     } 

    } 
}; 
+1

您將需要創建併發布http:/sscce.org。 –

回答

0

我想你想要這樣的東西。

public class TestCard extends JFrame implements ActionListener { 

CardLayout cards; 
JPanel cardPanel, tabsPanel; 
JButton b1, b2; 

public TestCard() { 
    b1= new JButton("kalam"); 
    b2= new JButton("gapas"); 
    tabsPanel = new JPanel(); 
    cards = new CardLayout(); 
    cardPanel = new JPanel(); 
    cardPanel.setLayout(cards); 

    JPanel firstCard = new JPanel(); 
    firstCard.setBackground(Color.WHITE); 


    JPanel secondCard = new JPanel(); 
    secondCard.setBackground(Color.blue); 


    cardPanel.add(firstCard, "kalam"); 
    cardPanel.add(secondCard, "gapas"); 
    tabsPanel.add(b1); 
    tabsPanel.add(b2); 

    add(tabsPanel, BorderLayout.NORTH); 
    add(cardPanel, BorderLayout.CENTER); 
    b1.addActionListener(this); 
    b2.addActionListener(this); 
    setSize(800, 600); 

    cards.show(cardPanel, "gapas"); 
    setVisible(true); 
} 

@Override 
public void actionPerformed(ActionEvent e) { 
    if (e.getActionCommand().matches("kalam")) { 
     cards.show(cardPanel, "kalam"); 
     System.out.println("kalam"); 
    } else { 
     cards.show(cardPanel, "gapas"); 
     System.out.println("gapas"); 
    } 
} 
public static void main(String[] args) { 
    new TestCard(); 
} 
} 
0

文檔狀態: 秀(集裝箱父,字符串名稱)翻轉到使用addLayoutComponent方法添加到具有指定名稱的佈局的組件。

增加了兩個項目:

  • 卡拉姆
  • gapan

,但你試圖表明:gapas。

此外,我會先添加,然後嘗試2節目。