2つの方法があります
コード
(免責事項、これはを使用したコード例で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");
}
}