4

我在应用程序中使用卡片布局并想要获取当前活动卡片的名称我尝试了很多方法但没有意思,例如这种方法似乎不再存在

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

2 回答 2

4

CardLayout不会暴露自己的组件与其键之间的映射(此处1为 , 2, 3, 4...),因此使用布局本身不会显示“卡片”是如何散列的。

如果您希望使用getName方法,请记住您必须先自行设置,因为默认情况下未设置该字段:

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

然后,使用您的for循环,您将能够获得当前卡String

于 2012-12-29T21:50:10.667 回答
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");
    }
}
于 2012-12-29T22:00:35.357 回答