Close screen through JMenuItem

0

I'm doing a simple system of registering students and teachers and wanted to interact with a JMenuItem called exit , the program closed.

I made the whole algorithm, but with that exclamation informing me that my ActionPerformed method is not being used, I do not understand that error follows the code below:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Janela {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setLocation(400, 300);

        JMenuBar barramenu = new JMenuBar();

        //Menu Cadastro
        JMenu cadastro = new JMenu("cadastro");

        JMenuItem professor = new JMenuItem("Professor");
        JMenuItem aluno = new JMenuItem("Aluno");
        cadastro.add(professor);
        cadastro.add(aluno);

        //Menu Pesquisa
        JMenu pesquisa = new JMenu("Pesquisa");

        JMenuItem professor1 = new JMenuItem("Professor");
        JMenuItem aluno1 = new JMenuItem("aluno");
        pesquisa.add(professor1);
        pesquisa.add(aluno1);

        //Menu Excluir 
        JMenu excluir = new JMenu("Excluir");

        JMenuItem professor2 = new JMenuItem("Professor");
        JMenuItem aluno2 = new JMenuItem("aluno");
        excluir.add(professor2);
        excluir.add(aluno2);

        //Menu sistema
        JMenu sistema = new JMenu("Sistema");

        JMenuItem sobre = new JMenuItem("Sobre");
        JMenuItem sair = new JMenuItem("Sair");
        sistema.add(sobre);
        sistema.add(sair);

        barramenu.add(cadastro);
        barramenu.add(pesquisa);
        barramenu.add(excluir);
        barramenu.add(sistema);
        frame.getContentPane().add(barramenu, BorderLayout.NORTH);
        frame.setVisible(true);





    sair.addActionListener(new ActionListener(){

        public void ActionPerformed(ActionEvent e){

            System.exit(0);

        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub

        }



    });


  }
}
    
asked by anonymous 07.05.2017 / 20:44

1 answer

0

First of all I want to leave an alert:

  

Always start the screen inside the Event-Dispatch-Thread because swing is not < in> Thread-Safe , and the entire GUI needs to start within this unique Thread. This answer further explains the reason for this and any issues that may occur. This other answer shows you some ways to start the application within this Thread.

To close the application, just call dispose() in your frame:

sair.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
           frame.dispose();

        }
    });

The interface ActionListener has only one method , and you have created another unnecessary and nonexistent. Just do this within the actionPerformed (Starting with "a" lowercase) method.

Note that this will force you to make the variable frame final, because of its local scope. The ideal is to work as the class being a whole screen, extending JFrame .

    
07.05.2017 / 20:52