|
Use the code generated by JBuilder to make a "File" menu. There is only one item "Exit" under the "File" menu item. Strangely, I put System.exit (0); in the jMenu1_mouseClicked (MouseEvent e) function. , Run the program, click the menu "File", the program will exit, but placed in the jMenuItem1_mouseClicked (MouseEvent e) function, run the program, click the menu "Exit", the program will not exit, as if the "Exit" menu can not respond. Just learned java, I really don't understand, which expert helps analyze it.
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);
}
} |
|