Close screen through Jmenuitem

Asked

Viewed 268 times

0

I’m doing a simple system of enrollment of students and teachers and wanted to, when interacting with a JMenuItem called get out of, the program closed.

I did the whole algorithm, but this with that exclamation stating that my Actionperformed method is not being used, I’m not understanding this 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

        }



    });


  }
}

1 answer

0


First of all I want to leave a warning:

Always start the screen inside the Event-Dispatch-Thread, because swing is not Thread-Safe, and the entire GUI needs to start within this single Thread. In this answer better explains the reason for this and any problems that may occur. This other answer shows some ways to start the application within this thread.

To close the app, simply call dispose() in your frame:

sair.addActionListener(new ActionListener(){

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

        }
    });

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

Realize this will force you to make the variable frame end, because of its local scope. The ideal is to work as the class being an entire canvas, extending JFrame.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.