Como fazer um actionperformad manual [Resolvido]

6 respostas
Q
package Interface;

import javax.swing.JFrame;  
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


public class Principal extends JFrame {  
   
    public Principal () {  
    super ("Principal");  
    initComponents();
    }  
  
    private void initComponents() {
    	JMenuBar menuBar = new JMenuBar();
    	JMenu arquivo = new javax.swing.JMenu("Arquivo");
    	JMenuItem sair = new javax.swing.JMenuItem("Sair");
    	JMenu cadastro = new javax.swing.JMenu("Cadastro");
    	JMenuItem cliente = new javax.swing.JMenuItem("Cliente");
		
    	this.setJMenuBar(menuBar);
    	menuBar.add(arquivo);
    	arquivo.add(sair);
    	menuBar.add(cadastro);
    	cadastro.add(cliente);
    	
    	  	    
        
    }
            
	public static void main (String [] args) {  
    	Principal janela = new Principal ();
        janela.setExtendedState(JFrame.MAXIMIZED_BOTH);
        janela.setVisible(true);
        
	}
 
	
}

queria saber como fazer action performad para a hora que clickar em cadastrar e sair?

6 Respostas

mauricioadl

sair.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ // seu codigo aqui } });

Luan_Kevin
sair.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

sairActionPerformed(evt);

}

});
private void sairActionPerformed(java.awt.event.ActionEvent evt) {

// seu código aqui

}

espero que ajude abraço :thumbup:

Q
package Interface;

import java.awt.event.ActionEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


public class Principal extends JFrame {  
   
    public Principal () {  
    super ("Principal");  
    initComponents();
    }  
  
    private void initComponents() {
    	JMenuBar menuBar = new JMenuBar();
    	JMenu arquivo = new javax.swing.JMenu("Arquivo");
    	JMenuItem sair = new javax.swing.JMenuItem("Sair");
    	JMenu cadastro = new javax.swing.JMenu("Cadastro");
    	JMenuItem cliente = new javax.swing.JMenuItem("Cliente");
		
    	this.setJMenuBar(menuBar);
    	menuBar.add(arquivo);
    	arquivo.add(sair);
    	menuBar.add(cadastro);
    	cadastro.add(cliente);
    	  	            
    }
            
	public static void main (String [] args) {  
    	Principal janela = new Principal ();
        janela.setExtendedState(JFrame.MAXIMIZED_BOTH);
        janela.setVisible(true);
        
	

	sair.addActionListener(new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent evt) {
		sairActionPerformed(evt);
		}

		private void sairActionPerformed(ActionEvent evt) {
			System.exit(0);
			
		}
		});

	}
	
}

Exception in thread “main” java.lang.Error: Unresolved compilation problem:
sair cannot be resolved

at Interface.Principal.main(Principal.java:40)
mauricioadl

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

faz assim, nao faz sentido vc criar um metodo para ser chamado de outro metodo.

F

Vc deve colocar seu action logo depois de instanciar o JMenu sair, não no main....

private void initComponents() {  
        JMenuBar menuBar = new JMenuBar();  
        JMenu arquivo = new javax.swing.JMenu("Arquivo");  
        JMenuItem sair = new javax.swing.JMenuItem("Sair");
        sair.addActionListener(new ActionListener(){  
           public void actionPerformed(ActionEvent evt){  
           // seu codigo aqui  
           }  
        });
        JMenu cadastro = new javax.swing.JMenu("Cadastro");  
        JMenuItem cliente = new javax.swing.JMenuItem("Cliente");  
          
        this.setJMenuBar(menuBar);  
        menuBar.add(arquivo);  
        arquivo.add(sair);  
        menuBar.add(cadastro);  
        cadastro.add(cliente);  
                          
    }
Q

Muito Obrigado, deu certo

Criado 26 de março de 2012
Ultima resposta 26 de mar. de 2012
Respostas 6
Participantes 4