0

I am using the following code, I tried to implement an Exit button, but it does nothing when it is pressed.

JMenuBar menuBar = new JMenuBar();
setJMenuBar( menuBar );
JMenuItem menuClose = new JMenu( "Exit" );
menuClose.setToolTipText("Exit application");

menuClose.addActionListener( new ActionListener(){
    public void actionPerformed( ActionEvent e ){
        System.exit(0);
    }
});

menuBar.add( menuClose );

Ideas?

2 Answers2

2

You should probably create a JMenu, then create a JMenuItem and add it to the JMenu.

So, your code would look like :

JMenuBar menuBar = new JMenuBar();
setJMenuBar( menuBar );
JMenu menu = new JMenu("Exit");
JMenuItem menuClose = new JMenuItem( "Exit application" );
menuClose.setToolTipText("Exit application");
menu.add(menuClose);
menuBar.add(menu);
APerson
  • 8,140
  • 8
  • 35
  • 49
0

Try this instead

setVisible(false);
dispose();

as seen here https://stackoverflow.com/a/1235321/2833217

Community
  • 1
  • 1
jrasm91
  • 394
  • 2
  • 10