检测用户何时关闭浮动工具栏框架

时间:2013-04-25 09:33:50

标签: java swing user-interface toolbar

当用户试图在秋千中关闭浮动工具栏窗口时,是否可以捕获事件? 提前谢谢。

1 个答案:

答案 0 :(得分:3)

可能有一些非常简单的解决方案,但为什么要使用它呢?

我能想出的最好的结果是(不扩展我自己的工具栏)是在工具栏中添加AncestorListener并监控它的事件。

我有这种方法的问题是,您需要知道最初连接的主框架,这可能不方便。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

public class TestFloatingToolBar {

    public static void main(String[] args) {
        new TestFloatingToolBar();
    }

    public TestFloatingToolBar() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JFrame frame = new JFrame("Test");

                final JToolBar tb = new JToolBar();
                tb.add(new JButton("Pop"));
                tb.setFloatable(true);

                tb.addAncestorListener(new AncestorListener() {
                    @Override
                    public void ancestorAdded(AncestorEvent event) {
                        tell();
                        if (SwingUtilities.getWindowAncestor(tb).equals(frame)) {
                            System.out.println("...In Main Frame");
                        } else {
                            System.out.println("...Maybe floating");
                        }
                    }

                    @Override
                    public void ancestorRemoved(AncestorEvent event) {
                        tell();
                        if (SwingUtilities.getWindowAncestor(tb).equals(frame)) {
                            System.out.println("...In Main Frame");
                        } else {
                            System.out.println("...Maybe floating");
                        }
                    }

                    @Override
                    public void ancestorMoved(AncestorEvent event) {
                    }
                });

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(tb, BorderLayout.NORTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public void tell() {
        Exception exp = new Exception();
        StackTraceElement[] stackTrace = exp.getStackTrace();
        System.out.println(stackTrace[1].getMethodName());
    }

}