提问者:小点点

来自不同类的JAVA WindowListener,然后是JFrame


我正在尝试分离我的一些代码,这样我就可以为不同的项目创建一个可重用的类。我现在拥有的类叫做MainFrame,它现在所做的就是用一个有一个JMenu文件的JMenuBar创建一个窗口。那个菜单有一个项目JMenuItem Exit。

我正在尝试让WindowListener类从我的菜单栏工作,以便在关闭应用程序时能够执行dispos()和System. gc()。

有人告诉我,这是一种退出应用程序的更干净的方式,然后是System. Ex(0);

public class MainFrame extends JFrame {

    private MenuBar menuBar;

    public MainFrame() {

        super("Sales");

        menuBar = new MenuBar();
        setLayout(new BorderLayout());
        setJMenuBar(createMenuBar());

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.out.println("test");
                dispose();
                System.gc();
            }
        });

        setMinimumSize(new Dimension(500, 400));
        setSize(600, 500);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();

        JMenu fileMenu = new JMenu("File");
        JMenuItem exitItem = new JMenuItem("Exit");

        fileMenu.add(exitItem);

        menuBar.add(fileMenu);      

        exitItem.setMnemonic(KeyEvent.VK_X);
        exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));

        exitItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                WindowListener[] listeners = getWindowListeners();

                for(WindowListener listener: listeners) {
                    listener.windowClosing(new WindowEvent(MainFrame.this, 0));
                }
            }
        });

        return menuBar;
    }
}

这是我试图创建的两个类。

public class MainFrame extends JFrame {

private MenuBar menuBar;

public MainFrame() {

    super("Sales");

    menuBar = new MenuBar();

    setLayout(new BorderLayout());

    setJMenuBar(menuBar.getMenuBar());

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.out.println("test");
            dispose();
            System.gc();
        }
    });

    setMinimumSize(new Dimension(500, 400));
    setSize(600, 500);
    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
}

}

public class MenuBar extends JMenuBar {

private JMenuBar menuBar;
private JMenu fileMenu, settingsMenu, helpMenu;

public MenuBar() {

    menuBar = new JMenuBar();

    setFileMenu();

    menuBar.add(fileMenu);
}


//// Method to return the menu bar
public JMenuBar getMenuBar() {

    return menuBar;
}


//// Private methods to set up the menu bar
private void setFileMenu() {
    fileMenu = new JMenu("File");

    JMenuItem exitItem = new JMenuItem("Exit");



    exitItem.setMnemonic(KeyEvent.VK_X);
    exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
    fileMenu.add(exitItem);

    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            WindowListener[] listeners = getWindowListeners();

            for(WindowListener listener: listeners) {
                listener.windowClosing(new WindowEvent(MainFrame.this, 0));
            }
        }
    });


}

关于如何让WindowListener从MenuBar类工作的任何建议?


共1个答案

匿名用户

listener.windowClosing(new WindowEvent(MainFrame.this, 0));

如果你想生成一个事件,那么你需要使用组件类的调度事件(…)方法。所以你最终会将事件分派到窗口。

基本代码是:

Window window = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();

if (window != null)
{
    WindowEvent windowClosing = new WindowEvent(window, WindowEvent.WINDOW_CLOSING);
    window.dispatchEvent(windowClosing);
}

此外,您可以摆脱WindowListener,然后将默认的关闭操作更改为:

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

窗口将被释放,如果它是最后一个打开的窗口,那么JVM也将退出。