Como implemntar KeyEvent em form com duas variaveis

3 respostas
S
Estou tentando fazer uma soma em um formulario que apos digitar um numero retorne um valor EX: campo1 = 5, campo2 = 0, resultado = 5 + 0 = 5. Sem "enter" ou seja o evento apos inserir o número gere a soma(keyEvent) O cap 12 do Deitel tem um exemplo, mais não da. Caso alguem tenha uma dica obrigado
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SomaFrame extends JFrame
{
	private JLabel lblval1, lblval2, lblresult;
	private JTextField tval1, tval2, tresult;
	private JPanel pOeste, pLeste;	
	private int a, b, c;
	
	
	public SomaFrame()
	{
		super ("Soma");
		
		Container c = getContentPane();
		c.setLayout(new FlowLayout());
		
		pOeste = new JPanel();
		pOeste.setLayout(new GridLayout(3,1));
		
		pLeste = new JPanel();
		pLeste.setLayout(new GridLayout(3,1));				
		
		lblval1 = new JLabel("Primeiro número");		
		tval1 = new JTextField("0",20);
		pOeste.add(lblval1,BorderLayout.EAST);
		pLeste.add(tval1);
		
		lblval2 = new JLabel("Segundo númeror");
		tval2 = new JTextField("0",20);
		pOeste.add(lblval2);
		pLeste.add(tval2);
		
		lblresult = new JLabel("Soma");
		tresult = new JTextField("0",20);
		tresult.setEditable(false);
		pOeste.add(lblresult);
		pLeste.add(tresult);
		
		c.add(pOeste, BorderLayout.WEST);
		c.add(pLeste, BorderLayout.EAST);
		
		// Registra os tratadores de eventos
		TrataEvento tratador = new TrataEvento();
		tval1.addActionListener(tratador);
		tval2.addActionListener(tratador);
		
		setSize(400, 200);
		setVisible(true);
		
	}
	public static void main (String args [])
	{
		SomaFrame somavalores = new SomaFrame();
		somavalores.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

3 Respostas

S

Gostaria que ao inves de “enter ou um botão” o evento sera gerado ao digitar o próprio número
Alguem tem uma dica?

F

usa o KeyListener (ou KeyAdapter)
tipo:

textField.addKeyListener(new KeyListener() {
            public void keyPressed(KeyEvent e) {
                System.out.println("keyPressed");
            }
            public void keyReleased(KeyEvent e) {
                System.out.println("keyReleased");
            }
            public void keyTyped(KeyEvent e) {
                System.out.println("keyTyped");
            }
    });

e nos metodos tu trata

S

Ai Fábio valeu KeyListener funciona legal, o propósito não é esse, mais como funcionou aqui funciona no conversor que estou implementando a soma ficou assim:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TesteEventoTeclado extends JFrame
{
	private JTextField jtf1, jtf2;
	private int a, b = 0;
	public TesteEventoTeclado()
	{		
		super ("Testando evento do teclado");
		Container container = new Container();
		container = getContentPane();
		container.setLayout(new FlowLayout()); 
		
		jtf1 = new JTextField(10);
		container.add(jtf1);
		jtf2 = new JTextField(30);
		container.add(jtf2);
		jtf1.addKeyListener(new KeyListener() 
		{
			public void keyPressed(KeyEvent e) 
			{			
			} 
			public void keyReleased(KeyEvent e) 
			{ 
				a = Integer.parseInt(jtf1.getText());
				b = b + a;
				jtf2.setText("Tecla Pressionada " + a  + "  valor da soma = " +b);						
			} 
			public void keyTyped(KeyEvent e) 
			{ 
				jtf1.setText("");
				
			} 
		}); 
		
		setSize(400, 150);
		setVisible(true);
	}
	public static void main (String args [])
	{
		TesteEventoTeclado teste = new TesteEventoTeclado();
		teste.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}
Criado 14 de março de 2005
Ultima resposta 29 de mar. de 2005
Respostas 3
Participantes 2