我正在尝试将一些代码分开,以便为不同的项目提供可重用的类。我现在所拥有的类称为MainFrame,它现在所做的就是创建一个带有JMenuBar的窗口,该窗口有一个JMenu文件。该菜单有一个项目JMenuItem Exit。
我正在尝试从我的菜单栏中使WindowListener类工作,以便在关闭应用程序时能够执行dispose()和System.gc()。
我被告知这是一种更简洁的退出应用程序的方法,然后是System.exit(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));
}
}
});
}
有关如何从MenuBar类获取WindowListener的任何建议吗?
答案 0 :(得分:3)
listener.windowClosing(new WindowEvent(MainFrame.this, 0));
如果要生成事件,则需要使用Component类的dispatchEvent(...)方法。所以你最终会把事件发送到窗口。
基本代码是:
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也将退出。