| |

VerySource

 Forgot password?
 Register
Search
View: 765|Reply: 1

Why does the program written in java not respond when I click the menu?

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-4 17:10:01
| Show all posts |Read mode
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);
    }
}
Reply

Use magic Report

0

Threads

13

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-1-4 17:30:02
| Show all posts
This is not written in the mouse event, but using actionListener
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list