Problemas com listener

2 respostas
A

Olá pessoal!!!

Não estou conseguindo fazer com que meus botoes sejam “ouvidos”

verja meu codigo abaixo…

import java.awt.Font;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ContadorGinastica {

	private JFrame tela;
	private JLabel labelTexto;


	
	

	/**
	 * @throws InterruptedException
	 */
	private void Formulario()  throws InterruptedException {
		
		
		labelTexto = new JLabel ("teste");
		
		
		
		JPanel painel = new JPanel();
		painel.add(labelTexto);
		
		
		tela = new JFrame("Contador Ginastica");
		tela.add(painel);
		
		JButton botaoA = new JButton("5");
		JButton botaoB = new JButton("7");
		JButton botaoC = new JButton("10");
		JButton botaoD = new JButton("15");
		JButton botaoE = new JButton("30");
		
		
		botaoA.setActionCommand("5");
		botaoB.setActionCommand("7");
		botaoC.setActionCommand("10");
		botaoD.setActionCommand("15");
		botaoE.setActionCommand("30");
		
		botaoA.addActionListener((ActionListener) this);
		botaoB.addActionListener((ActionListener) this);
		botaoC.addActionListener((ActionListener) this);
		botaoD.addActionListener((ActionListener) this);
		botaoE.addActionListener((ActionListener) this);
		
		
		
		
		painel.add(botaoA);
		painel.add(botaoB);
		painel.add(botaoC);
		painel.add(botaoD);
		painel.add(botaoE);
		
		painel.setSize(50, 100);
		
		
		tela.add(painel);
		
		tela.pack();

		tela.setSize(800, 640);
		
		tela.setVisible(true);
	
		tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		Font fonte = new Font("Arial", Font.BOLD, 180);
		
		labelTexto.setFont(fonte);
		labelTexto.setHorizontalAlignment(JLabel.CENTER);
		labelTexto.setVerticalAlignment(JLabel.BOTTOM);
		tela.setLocation(80, 80);

		
		int quantidade = 10;
		
		for (int i = 0; i <= quantidade; i++) {
			
			labelTexto.setText(i+"");
			
		//	tela.add(labelTexto);
			
			Thread.sleep(1000);

		}
	
		
		public void (actionPerformed(ActionEvent e)) {
			if ("5".equals(e.getActionCommand())) {
					/// aqui vou implementar o codigo
			} 

		}
		
	}
	
	
	
	
	
	public static void main(String[] args) throws InterruptedException {
		
				
		
		new ContadorGinastica().Formulario();
		
		
		
	}
	
	
}

2 Respostas

R

Geralmente se faz assim:

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

...

private void botaoAActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // coloca seu código aqui
    }
A

cara, perdoai minha burrice, mais naum deu…

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ContadorGinastica {

	private JFrame tela;
	private JLabel labelTexto;


	
	

	/**
	 * @throws InterruptedException
	 */
	private void Formulario()  throws InterruptedException {
		
		
		labelTexto = new JLabel ("teste");
		
		
		
		JPanel painel = new JPanel();
		painel.add(labelTexto);
		
		
		tela = new JFrame("Contador Ginastica");
		tela.add(painel);
		
		JButton botaoA = new JButton("5");
		JButton botaoB = new JButton("7");
		JButton botaoC = new JButton("10");
		JButton botaoD = new JButton("15");
		JButton botaoE = new JButton("30");
		
		painel.add(botaoA);
		painel.add(botaoB);
		painel.add(botaoC);
		painel.add(botaoD);
		painel.add(botaoE);
		
		painel.setSize(50, 100);
		
		
		tela.add(painel);
		
		tela.pack();

		tela.setSize(800, 640);
		
		tela.setVisible(true);
	
		tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		Font fonte = new Font("Arial", Font.BOLD, 180);
		
		labelTexto.setFont(fonte);
		labelTexto.setHorizontalAlignment(JLabel.CENTER);
		labelTexto.setVerticalAlignment(JLabel.BOTTOM);
		tela.setLocation(80, 80);
		
		
		botaoA.addActionListener(new java.awt.event.ActionListener() {  
			public void actionPerformed(java.awt.event.ActionEvent evt) {  
				botaoAActionPerformed(evt);   //da erro aqui
			}  
		}); 
		
		
		private void botaoAActionPerformed  /*<<<<<e aqui tambem */  (java.awt.event.ActionEvent evt) {                                              
			
			int quantidade = 5;
			
			for (int i = 0; i <= quantidade; i++) {
				
				labelTexto.setText(i+"");
				
			//	tela.add(labelTexto);
				
				Thread.sleep(1000);

			}
		}  
		
		
			
		
		
	}
	
	
	
	
	
	public static void main(String[] args) throws InterruptedException {
		
				
		
		new ContadorGinastica().Formulario();
		
		
		
	}
	
	
}
Criado 9 de abril de 2008
Ultima resposta 9 de abr. de 2008
Respostas 2
Participantes 2