我在应用程序中使用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");
可能的方法是什么?
答案 0 :(得分:4)
CardLayout
不公开组件及其键之间的映射(此处为1
,2
,3
,4
...),所以使用布局本身不会揭示“卡片”是如何散列的。
如果您希望使用getName方法,请记住您必须先自己设置,因为默认情况下未设置字段:
firstPanel.setName("1");
cardPanel.add(firstPanel, firstPanel.getName());
然后,使用for
循环,您将能够获得当前卡String
。
答案 1 :(得分:1)
有两种方式
putClientProperty
(setName
等)按AncestorListener
或HierarchyListener
码
(免责声明,请注意,这只是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");
}
}