2012-05-30 60 views
6

我看着那個使用該代碼的代碼示例:如何在CardLayout中顯示不同的卡片?

cl.show(cardPanel, "" + (currentCard)); 

但是當我使用show我得到Eclipse的一個消息,它的過時,我不知道是否有另一種方式展現在CardLayout不同卡當我點擊按鈕?以下是我的CardLayout類的代碼。如果代碼的某些部分是不好的做法,建議也是受歡迎的。謝謝!

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

public class CardLayoutTest extends JFrame implements ActionListener { 

// Ref 
private JPanel cardPanel, jp1, jp2, buttonPanel; 
private JLabel jl1, jl2; 
private JButton btn1, btn2; 
private CardLayout cardLayout; 

// Konstruktor 
public CardLayoutTest() 
{ 
    setTitle("Test med CardLayout"); 
    setSize(600,400); 

    cardPanel = new JPanel(); 
    buttonPanel = new JPanel(); 

    cardPanel.setLayout(cardLayout); 

    jp1 = new JPanel(); 
    jp2 = new JPanel(); 

    jl1 = new JLabel("Card 1"); 
    jl2 = new JLabel("Card 2"); 

    jp1.add(jl1); 
    jp2.add(jl2); 

    cardPanel.add(jp1, "1"); 
    cardPanel.add(jp2, "2"); 

    btn1 = new JButton("Show Card 1"); 
    btn2 = new JButton("Show Card 2"); 

    buttonPanel.add(btn1); 
    buttonPanel.add(btn2); 

    getContentPane().add(cardPanel, BorderLayout.NORTH); 
    getContentPane().add(buttonPanel, BorderLayout.SOUTH); 

    btn1.addActionListener(this); 
} 

    public void actionPerformed(ActionEvent event) 
    { 
     // ??? Show card 1 ??? 

     // ??? Show card 2 ??? 
    } 

public static void main(String[] args) { 
    CardLayoutTest frame = new CardLayoutTest(); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
} 

回答

13

EDIT(我想你的代碼示例)

1.you忘了初始化最重要的變量

private CardLayout cardLayout = new CardLayout(); 

2.然後SSCCE可能是

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

public class CardLayoutTest extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private JPanel cardPanel, jp1, jp2, buttonPanel; 
    private JLabel jl1, jl2; 
    private JButton btn1, btn2; 
    private CardLayout cardLayout = new CardLayout(); 

    public CardLayoutTest() { 
     setTitle("Test med CardLayout"); 
     setSize(400, 300); 
     cardPanel = new JPanel(); 
     buttonPanel = new JPanel(); 
     cardPanel.setLayout(cardLayout); 
     jp1 = new JPanel(); 
     jp2 = new JPanel(); 
     jl1 = new JLabel("Card 1"); 
     jl2 = new JLabel("Card 2"); 
     jp1.add(jl1); 
     jp2.add(jl2); 
     cardPanel.add(jp1, "1"); 
     cardPanel.add(jp2, "2"); 
     btn1 = new JButton("Show Card 1"); 
     btn1.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       cardLayout.show(cardPanel, "1"); 
      } 
     }); 
     btn2 = new JButton("Show Card 2"); 
     btn2.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       cardLayout.show(cardPanel, "2"); 
      } 
     }); 
     buttonPanel.add(btn1); 
     buttonPanel.add(btn2); 
     add(cardPanel, BorderLayout.NORTH); 
     add(buttonPanel, BorderLayout.SOUTH); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       CardLayoutTest frame = new CardLayoutTest(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 
+0

感謝您的新代碼!它運作良好!但是我對你添加的一些行有一些疑問,比如這個:private static final long serialVersionUID = 1L;我可以看到,我在Eclipse中刪除了一個黃色警戒三角形,但它在幹什麼?而且你也替換了這一行:getContentPane()。add(cardPanel,BorderLayout.NORTH); whith this:add(cardPanel,BorderLayout.NORTH);有什麼不同?最後,你是否也可以善待並解釋你在主要方法中做了什麼?謝謝! –

8

這是最有可能的,你正在呼籲的JPanel顯示(),而不是CardLayout。

show()方法用於存在於JPanel(更具體地說Component)中並且已被setVisible()取代。這與CardLayout的show()方法完全不同。

確保你正在做的事情就像在你的動作監聽

CardLayout cardLayout = (CardLayout)(cardPanel.getLayout()); 
cardLayout.show(cardPanel, "CardToShow"); 

補充說明如下,它不出現你是「newing」你CardLayout。確保你這樣做。

+0

是的,我錯過了「新」我的CardLayout,我也做了一些錯誤的表演!謝謝! –

相關問題