为什么java写的程序,点击菜单没有反应
用JBuilder 产生的一段代码,做一个“File "的菜单,“File "菜单项下只有一项“Exit ",奇怪的是,我把System.exit(0);放在jMenu1_mouseClicked(MouseEvent e)函数中,运行程序,点击菜单“File ",程序会退出,但放在jMenuItem1_mouseClicked(MouseEvent e)函数中,运行程序,点击菜单“Exit ",程序不会退出,好像“Exit "菜单不能响应。刚刚学java,实在是不明白,哪位达人帮忙分析一下。
public class Frame1 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenu1 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
this.setJMenuBar(jMenuBar1);
setSize(new Dimension(400, 300));
setTitle( "Frame Title ");
jMenu1.setText( "File ");
jMenu1.addMouseListener(new Frame1_jMenu1_mouseAdapter(this));
jMenuItem1.setText( "exit ");
jMenuItem1.addMouseListener(new Frame1_jMenuItem1_mouseAdapter(this));
jMenuBar1.add(jMenu1);
jMenu1.add(jMenuItem1);
}
public void jMenuItem1_mouseClicked(MouseEvent e) {
System.exit(0);
}
public void jMenu1_mouseClicked(MouseEvent e) {
//System.exit(0);
}
}
class Frame1_jMenu1_mouseAdapter extends MouseAdapter {
private Frame1 adaptee;
Frame1_jMenu1_mouseAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.jMenu1_mouseClicked(e);
}
}
class Frame1_jMenuItem1_mouseAdapter extends MouseAdapter {
private Frame1 adaptee;
Frame1_jMenuItem1_mouseAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void mouseClicked(MouseEvent e) {
adaptee.jMenuItem1_mouseClicked(e);
}
}
[解决办法]
这个不是写在鼠标事件里面的,还是用actionListener