所有
我有一个JPanel说JTabbedPane的容器,它包含一个设计和一个预览面板。
JPanel位于JFrame中,
预览面板包含JME3画布。
我想要实现的目标,每当用户移动到应用程序中的另一个面板时,我都可以捕获容器Jpanel的卸载并关闭JME3应用程序,
我找到了以下帖子,
我通过这些线程了解到HierarchyListener可能对我有用, 但我被困在如何在那里得到特定的面板卸载检测?
由于 米希尔
答案 0 :(得分:4)
有三个侦听器对这些事件做出反应AncestorListener,ComponentListener,HierarchyListener
默认使用ComponentListener
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import javax.swing.JButton;
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;
public CardLayout card = new CardLayout();
public CardlayoutTest() {
JPanel pnlA = new JPanel(new BorderLayout());
pnlA.add(new JButton("A"), BorderLayout.CENTER);
JPanel pnlB = new JPanel(new BorderLayout());
pnlB.add(new JButton("B"), BorderLayout.CENTER);
JPanel pnlC = new JPanel(new BorderLayout());
pnlC.add(new JButton("C"), BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(card);
add(pnlA, "A");
add(pnlB, "B");
add(pnlC, "C");
pnlA.addAncestorListener(new EventHandler());
pnlB.addAncestorListener(new EventHandler());
pnlC.addAncestorListener(new EventHandler());
pnlA.addHierarchyListener(new EventHandler());
pnlB.addHierarchyListener(new EventHandler());
pnlB.addHierarchyListener(new EventHandler());
pnlA.addComponentListener(new EventHandler());
pnlB.addComponentListener(new EventHandler());
pnlB.addComponentListener(new EventHandler());
}
class EventHandler implements AncestorListener, ComponentListener, HierarchyListener {
@Override
public void ancestorAdded(AncestorEvent event) {
System.out.println("CardlayoutTest.EventHandler.ancestorAdded()");
}
@Override
public void ancestorMoved(AncestorEvent event) {
System.out.println("CardlayoutTest.EventHandler.ancestorMoved()");
}
@Override
public void ancestorRemoved(AncestorEvent event) {
System.out.println("CardlayoutTest.EventHandler.ancestorRemoved()");
}
@Override
public void hierarchyChanged(HierarchyEvent e) {
System.out.println("Components Change: " + e.getChanged());
if ((e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0) {
if (e.getComponent().isDisplayable()) {
System.out.println("Components DISPLAYABILITY_CHANGED : " + e.getChanged());
} else {
System.out.println("Components DISPLAYABILITY_CHANGED : " + e.getChanged());
}
}
if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) {
if (e.getComponent().isDisplayable()) {
System.out.println("Components SHOWING_CHANGED : " + e.getChanged());
} else {
System.out.println("Components SHOWING_CHANGED : " + e.getChanged());
}
}
}
public void componentHidden(ComponentEvent e) {
System.out.println(e.getComponent().getClass().getName() + " --- Hidden");
}
public void componentMoved(ComponentEvent e) {
System.out.println(e.getComponent().getClass().getName() + " --- Moved");
}
public void componentResized(ComponentEvent e) {
System.out.println(e.getComponent().getClass().getName() + " --- Resized ");
}
public void componentShown(ComponentEvent e) {
System.out.println(e.getComponent().getClass().getName() + " --- Shown");
}
}
public static void main(String[] args) {
CardlayoutTest t = new CardlayoutTest();
t.setSize(500, 500);
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");
}
}
有事件
CardlayoutTest.main()------------------------第一个组件更改: javax.swing.JPanel中[,0,0,0x0,无效,布局= java.awt.BorderLayout中,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 9,MAXIMUMSIZE =,=的minimumSize,首选大小=] 组件DISPLAYABILITY_CHANGED: javax.swing.JPanel中[,0,0,0x0,无效,布局= java.awt.BorderLayout中,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 9,MAXIMUMSIZE =,=的minimumSize,首选大小=] 组件变更: javax.swing.JPanel中[,0,0,0x0,无效,隐藏,布局= java.awt.BorderLayout中,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 9,MAXIMUMSIZE =,=的minimumSize,首选大小=] 组件DISPLAYABILITY_CHANGED: javax.swing.JPanel中[,0,0,0x0,无效,隐藏,布局= java.awt.BorderLayout中,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 9,MAXIMUMSIZE =,=的minimumSize,首选大小=] 组件变更: javax.swing.JPanel中[,0,0,0x0,无效,隐藏,布局= java.awt.BorderLayout中,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 9,MAXIMUMSIZE =,=的minimumSize,首选大小=] 组件DISPLAYABILITY_CHANGED: javax.swing.JPanel中[,0,0,0x0,无效,隐藏,布局= java.awt.BorderLayout中,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 9,MAXIMUMSIZE =,=的minimumSize,首选大小=] 组件变更: Listeners.CardlayoutTest [frame0,0,0,500x500,布局= java.awt.BorderLayout中,标题=,可调整大小的,正常的,defaultCloseOperation = EXIT_ON_CLOSE,的rootPane = javax.swing.JRootPane中[,8,31,484x461,布局= javax.swing中。 JRootPane的$ RootLayout,alignmentX = 0.0,alignmentY = 0.0,边界=,=标志16777673,MAXIMUMSIZE =,=的minimumSize,首选大小=],rootPaneCheckingEnabled =真] 组件SHOWING_CHANGED: Listeners.CardlayoutTest [frame0,0,0,500x500,布局= java.awt.BorderLayout中,标题=,可调整大小的,正常的,defaultCloseOperation = EXIT_ON_CLOSE,的rootPane = javax.swing.JRootPane中[,8,31,484x461,布局= javax.swing中。 JRootPane的$ RootLayout,alignmentX = 0.0,alignmentY = 0.0,边界=,=标志16777673,MAXIMUMSIZE =,=的minimumSize,首选大小=],rootPaneCheckingEnabled =真] 组件变更: Listeners.CardlayoutTest [frame0,0,0,500x500,布局= java.awt.BorderLayout中,标题=,可调整大小的,正常的,defaultCloseOperation = EXIT_ON_CLOSE,的rootPane = javax.swing.JRootPane中[,8,31,484x461,布局= javax.swing中。 JRootPane的$ RootLayout,alignmentX = 0.0,alignmentY = 0.0,边界=,=标志16777673,MAXIMUMSIZE =,=的minimumSize,首选大小=],rootPaneCheckingEnabled =真] 组件SHOWING_CHANGED: Listeners.CardlayoutTest [frame0,0,0,500x500,布局= java.awt.BorderLayout中,标题=,可调整大小的,正常的,defaultCloseOperation = EXIT_ON_CLOSE,的rootPane = javax.swing.JRootPane中[,8,31,484x461,布局= javax.swing中。 JRootPane的$ RootLayout,alignmentX = 0.0,alignmentY = 0.0,边界=,=标志16777673,MAXIMUMSIZE =,=的minimumSize,首选大小=],rootPaneCheckingEnabled =真] 组件变更: Listeners.CardlayoutTest [frame0,0,0,500x500,布局= java.awt.BorderLayout中,标题=,可调整大小的,正常的,defaultCloseOperation = EXIT_ON_CLOSE,的rootPane = javax.swing.JRootPane中[,8,31,484x461,布局= javax.swing中。 JRootPane的$ RootLayout,alignmentX = 0.0,alignmentY = 0.0,边界=,=标志16777673,MAXIMUMSIZE =,=的minimumSize,首选大小=],rootPaneCheckingEnabled =真] 组件SHOWING_CHANGED: Listeners.CardlayoutTest [frame0,0,0,500x500,布局= java.awt.BorderLayout中,标题=,可调整大小的,正常的,defaultCloseOperation = EXIT_ON_CLOSE,的rootPane = javax.swing.JRootPane中[,8,31,484x461,布局= javax.swing中。 JRootPane的$ RootLayout,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 16777673,MAXIMUMSIZE =,=的minimumSize,首选大小=],rootPaneCheckingEnabled =真]
CardlayoutTest.EventHandler.ancestorMoved()javax.swing.JPanel --- 调整大小javax.swing.JPanel ---调整大小javax.swing.JPanel --- 调整大小的CardlayoutTest.EventHandler.ancestorMoved() CardlayoutTest.EventHandler.ancestorAdded() CardlayoutTest.main()------------------------ SECOND Components 更改: javax.swing.JPanel中[,0,0,484x461,隐藏,布局= java.awt.BorderLayout中,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 9,MAXIMUMSIZE =,=的minimumSize,首选大小=] 组件SHOWING_CHANGED: javax.swing.JPanel中[,0,0,484x461,隐藏,布局= java.awt.BorderLayout中,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 9,MAXIMUMSIZE =,=的minimumSize,首选大小=] 组件变更: javax.swing.JPanel中[,0,0,484x461,布局= java.awt.BorderLayout中,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 9,MAXIMUMSIZE =,=的minimumSize,首选大小=] 组件SHOWING_CHANGED: javax.swing.JPanel中[,0,0,484x461,布局= java.awt.BorderLayout中,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 9,MAXIMUMSIZE =,=的minimumSize,首选大小=] 组件变更: javax.swing.JPanel中[,0,0,484x461,布局= java.awt.BorderLayout中,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 9,MAXIMUMSIZE =,=的minimumSize,首选大小=] 组件SHOWING_CHANGED: javax.swing.JPanel中[,0,0,484x461,布局= java.awt.BorderLayout中,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 9,MAXIMUMSIZE =,=的minimumSize,首选大小=]
CardlayoutTest.EventHandler.ancestorRemoved()javax.swing.JPanel --- 隐藏的CardlayoutTest.EventHandler.ancestorAdded()javax.swing.JPanel ---显示了javax.swing.JPanel ---显示了CardlayoutTest.main()------------------------ THIRD组件更改: javax.swing.JPanel中[,0,0,484x461,隐藏,布局= java.awt.BorderLayout中,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 9,MAXIMUMSIZE =,=的minimumSize,首选大小=] 组件SHOWING_CHANGED: javax.swing.JPanel中[,0,0,484x461,隐藏,布局= java.awt.BorderLayout中,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 9,MAXIMUMSIZE =,=的minimumSize,首选大小=] 组件变更: javax.swing.JPanel中[,0,0,484x461,隐藏,布局= java.awt.BorderLayout中,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 9,MAXIMUMSIZE =,=的minimumSize,首选大小=] 组件SHOWING_CHANGED: javax.swing.JPanel中[,0,0,484x461,隐藏,布局= java.awt.BorderLayout中,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 9,MAXIMUMSIZE =,=的minimumSize,首选大小=] CardlayoutTest.EventHandler.ancestorRemoved()javax.swing.JPanel --- 隐藏的javax.swing.JPanel ---隐藏
CardlayoutTest.EventHandler.ancestorAdded()
答案 1 :(得分:1)
在面板上实现AncestorListener,您想知道加载/卸载事件。我添加了一个显示和隐藏创建的JPanel的按钮。下面是相同的代码。 AncestorListener告诉我何时完全加载和卸载。
public class PanelTrial extends javax.swing.JPanel implements AncestorListener {
private Container frame;
private javax.swing.JPanel btnPanel;
public PanelTrial(Container frame) {
this.frame = frame;
initComponents();
createBtnPanel();
btnPanel.addAncestorListener(this);
}
// Called on btn click event - to show/hide btnPanel
private void updateRightPanel(boolean showBtnPanel) {
jPanel1.removeAll();
GroupLayout layout = (GroupLayout) jPanel1.getLayout();
if (showBtnPanel) {
layout.setHorizontalGroup(layout.createSequentialGroup()
.addComponent(btnPanel));
layout.setVerticalGroup(layout.createParallelGroup(
Alignment.TRAILING).addComponent(btnPanel));
} else {
layout.setHorizontalGroup(layout.createSequentialGroup()
.addComponent(rightPanel));
layout.setVerticalGroup(layout.createParallelGroup(
Alignment.TRAILING).addComponent(rightPanel));
}
jPanel1.validate();
this.validate();
}
@Override
public void ancestorAdded(AncestorEvent e) {
if (e.getSource() == btnPanel)
System.out.println("BTNSPANEL LOADED / ADDED");
}
@Override
public void ancestorMoved(AncestorEvent e) {
// This is of no use for your purpose
System.out.println("Anc Moved - " + e.getSource());
}
@Override
public void ancestorRemoved(AncestorEvent e) {
if (e.getSource() == btnPanel)
System.out.println("BTNSPANEL UNLOADED / REMOVED");
}