像:
  this.addAncestorListener(new AncestorListener() {
            public void ancestorAdded(AncestorEvent event) {
            }            public void ancestorRemoved(AncestorEvent evt) {
                hidePopupMenu();
            }            public void ancestorMoved(AncestorEvent event) {
                hidePopupMenu();
            }
        });
这样,不过要有具体作用的。

解决方案 »

  1.   

    大体写了个例子,不太好,楼主改改package demo;import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.event.AncestorEvent;
    import javax.swing.event.AncestorListener;public class DemoAncestorListener extends JFrame { /**
     * 
     */
    private static final long serialVersionUID = 1854825083460143245L;
    private JLabel label; /**
     * Launch the application
     * 
     * @param args
     */
    public static void main(String args[]) {
    try {
    DemoAncestorListener frame = new DemoAncestorListener();
    frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    } /**
     * Create the frame
     */
    public DemoAncestorListener() {
    super();
    getContentPane().setLayout(null);
    setBounds(100, 100, 437, 375);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JPanel panel = new JPanel();
    panel.setLayout(null);
    panel.setBounds(0, 0, 429, 341);
    getContentPane().add(panel); final JButton button = new JButton();
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    label.setVisible(true);
    panel.repaint();
    }
    });
    button.setBounds(99, 262, 99, 23);
    button.setText("标签可见");
    panel.add(button); final JButton button_1 = new JButton();
    button_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    label.setVisible(false);
    panel.repaint();
    }
    });
    button_1.setText("标签不可见");
    button_1.setBounds(246, 262, 99, 23);
    panel.add(button_1); final JButton button_2 = new JButton();
    button_2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    label = new JLabel();
    label.addAncestorListener(new AncestorListenerForLabel());
    label.setText("测试用标签");
    label.setBounds(146, 82, 120, 30);
    panel.add(label);
    panel.repaint();
    }
    });
    button_2.setText("添加标签");
    button_2.setBounds(99, 291, 99, 23);
    panel.add(button_2); final JButton button_3 = new JButton();
    button_3.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    panel.remove(label);
    panel.repaint();
    }
    });
    button_3.setText("删除标签");
    button_3.setBounds(246, 291, 99, 23);
    panel.add(button_3); label = new JLabel();
    label.setText("测试用标签");
    label.addAncestorListener(new AncestorListenerForLabel());
    label.setBounds(146, 82, 120, 30);
    panel.add(label);
    //
    } class AncestorListenerForLabel implements AncestorListener {
    /**
     * 当通过调用 setVisible(true) 或将其添加到组件层次结构中而使源组件或其祖先之一变得可见时调用。
     */
    public void ancestorAdded(AncestorEvent event) {
    JOptionPane.showMessageDialog(null, "当前调用的方法为ancestorAdded()!事件源为:"
    + event.getSource().getClass().getName());
    } /**
     * 在源组件或其祖先之一被移动时调用。
     */
    public void ancestorMoved(AncestorEvent event) {
    JOptionPane.showMessageDialog(null, "当前调用的方法为ancestorMoved()!事件源为:"
    + event.getSource().getClass().getName());
    } /**
     * 当通过调用 setVisible(false) 或将其从组件层次结构中移除而使源组件或其祖先之一变得不可见时调用。
     */
    public void ancestorRemoved(AncestorEvent event) {
    JOptionPane.showMessageDialog(null,
    "当前调用的方法为ancestorRemoved()!事件源为:"
    + event.getSource().getClass().getName());
    } }
    }
      

  2.   

    讲java事件处理的书,基本会涉及的。
    1楼的代码貌似很详细了