日期:2014-05-20 浏览次数:20874 次
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestFrame extends JFrame
{
private JMenuBar menuBar = new JMenuBar();
private JMenu menu = new JMenu("Menu");
private JMenuItem exitItem = new JMenuItem("EXIT");
public TestFrame()
{
initMenuBar();
setJMenuBar(menuBar);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
closeWindow();
}
});
}
private void initMenuBar()
{
menuBar.add(menu);
menu.add(exitItem);
exitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
closeWindow();
}
});
exitItem.setMnemonic('E');
}
private void closeWindow()
{
int type = JOptionPane.showConfirmDialog(this, "是否关闭窗口?", "Question", JOptionPane.YES_NO_OPTION);
if (type == JOptionPane.OK_OPTION)
{
System.exit(0);
}
}
public static void main(String[] args)
{
JFrame frame = new TestFrame();
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
}