2012-12-29 68 views
3

我使用的應用程序cardlayout,並希望得到當前活動卡 我試過很多方法,但沒有平均的名稱,例如,這種方法似乎不再存在卡布局獲得當前卡串

panel.getLayout().getActiveItem() 

也是我試過,但它不工作太

Panel card = null; 
     for (Component comp : cardPanel.getComponents()) { 
      if (comp.isVisible()) { 
       System.out.println(card.getName()); 

        } 
       } 

例如:下面的stamts增加了幾個小組到卡的佈局,我想回到1,2,3,4或5當前活動的卡片:

cardPanel.add(firstP, "1"); 
    cardPanel.add(secondP, "2"); 
    cardPanel.add(thirdP, "3"); 
    cardPanel.add(fourthP, "4"); 
    cardPanel.add(fifthP, "5"); 

有什麼可能的方法來做到這一點?

回答

4

CardLayout不暴露自己的地圖組件及其鍵(這裏1234 ...)之間,因此,使用佈局本身不會透露如何「卡」被散列。

如果您希望使用getName方法,記住,你必須先設置此自己作爲字段默認情況下不設置:

firstPanel.setName("1"); 
cardPanel.add(firstPanel, firstPanel.getName()); 

然後,用你for循環,你將能夠得到當前卡String

1

有兩種方法

代碼

(聲明,注意,這是與只Thread.sleep(int)代碼示例,只有在這種形式的工作原理,這裏特意鎖定Event Dispatch Thread

import java.awt.BorderLayout; 
import java.awt.CardLayout; 
import java.awt.event.HierarchyEvent; 
import java.awt.event.HierarchyListener; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.event.AncestorEvent; 
import javax.swing.event.AncestorListener; 

public class CardlayoutTest extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private JPanel pnlA, pnlB, pnlC; 
    public CardLayout card = new CardLayout(); 

    public CardlayoutTest() { 
     EventHandler eventHandle = new EventHandler(); 
     pnlA = new JPanel(new BorderLayout()); 
     pnlA.add(new JButton("A"), BorderLayout.CENTER); 
     pnlA.putClientProperty("JPanel", "JPanel_a"); 
     pnlB = new JPanel(new BorderLayout()); 
     pnlB.add(new JButton("B"), BorderLayout.CENTER); 
     pnlB.putClientProperty("JPanel", "JPanel_b"); 
     pnlC = new JPanel(new BorderLayout()); 
     pnlC.add(new JButton("C"), BorderLayout.CENTER); 
     pnlC.putClientProperty("JPanel", "JPanel_c"); 
     pnlA.addAncestorListener(eventHandle); 
     pnlA.addHierarchyListener(eventHandle); 
     pnlB.addAncestorListener(eventHandle); 
     pnlB.addHierarchyListener(eventHandle); 
     pnlC.addAncestorListener(eventHandle); 
     pnlC.addHierarchyListener(eventHandle); 
     setLayout(card); 
     add(pnlA, "A"); 
     add(pnlB, "B");// 
     add(pnlC, "C"); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 

    class EventHandler implements AncestorListener, HierarchyListener { 

     @Override 
     public void ancestorAdded(AncestorEvent event) { 
      JComponent comp2 = event.getComponent(); 
      String str = (String) comp2.getClientProperty("JPanel"); 
      System.out.println("ancestorAdded " + str); 
     } 

     @Override 
     public void ancestorMoved(AncestorEvent event) { 
      JComponent comp2 = event.getComponent(); 
      String str = (String) comp2.getClientProperty("JPanel"); 
      System.out.println("ancestorMoved " + str); 
     } 

     @Override 
     public void ancestorRemoved(AncestorEvent event) { 
      JComponent comp2 = event.getComponent(); 
      String str = (String) comp2.getClientProperty("JPanel"); 
      System.out.println("ancestorRemoved " + str); 
     } 

     @Override 
     public void hierarchyChanged(HierarchyEvent e) { 
      JComponent comp2 = (JComponent) e.getComponent(); 
      String str = (String) comp2.getClientProperty("JPanel"); 
      System.out.println("hierarchyChanged " + str); 
     } 
    } 

    public static void main(String[] args) { 
     CardlayoutTest t = new CardlayoutTest(); 
     t.setSize(300, 200); 
     System.out.println("CardlayoutTest.main()------------------------ FIRST"); 
     t.card.show(t.getContentPane(), "A"); 
     t.setVisible(true); 
     System.out.print("\n"); 
     try { 
      Thread.sleep(2000); 
     } catch (InterruptedException e) { 
     } 
     System.out.println("CardlayoutTest.main()------------------------ SECOND"); 
     t.card.show(t.getContentPane(), "B"); 
     System.out.print("\n"); 
     try { 
      Thread.sleep(2000); 
     } catch (InterruptedException e) { 
     } 
     System.out.println("CardlayoutTest.main()------------------------ THIRD"); 
     t.card.show(t.getContentPane(), "C"); 
     System.out.print("\n"); 
     try { 
      Thread.sleep(2000); 
     } catch (InterruptedException e) { 
     } 
     System.out.println("CardlayoutTest.main()------------------------ SECOND"); 
     t.card.show(t.getContentPane(), "B"); 
     System.out.print("\n"); 
     try { 
      Thread.sleep(2000); 
     } catch (InterruptedException e) { 
     } 
     System.out.println("CardlayoutTest.main()------------------------ FIRST"); 
     t.card.show(t.getContentPane(), "A"); 
     System.out.print("\n"); 
    } 
}