Ver Mensaje Individual
  #3 (permalink)  
Antiguo 15/06/2009, 10:23
matheo
 
Fecha de Ingreso: mayo-2008
Mensajes: 75
Antigüedad: 16 años
Puntos: 0
Respuesta: Escucha de teclado con un JPANEL

No tengo ese JTextField que tu dices. Te pongo todo el código:

Código:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

 
public class Calculadora{
	
	static float numero;
	static float ans;
	static String operacion;
	static float solucion;
	static JTextField pantalla = new JTextField(); //Declaracion de la pantalla
	
	static boolean isOperador (String str){
		if ((str == "+") || (str == "-") || (str == "*") || (str == "/")){
			return true;
		}
		else{
			return false;
		}
	}
	
	//Escribe en la pantalla
	static void Escribir (String str){
		if (str.endsWith(".0")){
			str = str.replace(".0","");
		}
		pantalla.setText(str);
	}
	
	static void Anadir (String str){
		if ( isOperador(str)){
			ans = numero; //Se guarda el numero actual de la pantalla.
			numero = 0;  //Se limpia el numero
			operacion = str; //Se guarda la operacion a realizar
		}
		else if (str == "."){
			
		}
		else if (str == "="){ //Se realiza la operacion
			
			if (operacion == "+"){
				solucion = ans + numero;
			}
			if (operacion == "-"){
				solucion = ans - numero;
			}
			if (operacion == "*"){
				solucion = ans * numero;
			}
			if (operacion == "/"){
				solucion = ans / numero;
			}
			Escribir(Float.toString(solucion));
			numero = solucion; //Para seguir la operacion
		}
		else{
			numero = (numero*10) + Float.parseFloat(str); //Si es un numero, se va creando por decimas
		}
	}
	
	
	
	public static void main (String [] arg){
		JFrame ventana = new JFrame("Calculadora");
		ventana.setSize(330,400); //Tamaño de la ventana
		
		//CONSTRUCCION DE LA PANTALLA
		pantalla.setBounds(new Rectangle(30,20,261,60));
		Font f = new Font( "Arial",Font.BOLD,22 );
		pantalla.setFont(f);
		pantalla.setHorizontalAlignment(SwingConstants.RIGHT);
		//CONSTRUCCION DE BOTONES
		JButton boton0 = new JButton(" 0 ");
     	boton0.setBounds(new Rectangle(30,280,50,50));
     	JButton boton1 = new JButton(" 1 ");
     	boton1.setBounds(new Rectangle(30,220,50,50));
     	JButton boton2 = new JButton(" 2 ");
     	boton2.setBounds(new Rectangle(90,220,50,50));
     	JButton boton3 = new JButton(" 3 ");
     	boton3.setBounds( new Rectangle(150,220,50,50));
     	JButton boton4 = new JButton(" 4 ");
     	boton4.setBounds( new Rectangle(30,160,50,50));
     	JButton boton5 = new JButton(" 5 ");
     	boton5.setBounds( new Rectangle(90,160,50,50));
     	JButton boton6 = new JButton(" 6 ");
     	boton6.setBounds( new Rectangle(150,160,50,50));
     	JButton boton7 = new JButton(" 7 ");
     	boton7.setBounds( new Rectangle(30,100,50,50));
     	JButton boton8 = new JButton(" 8 ");
     	boton8.setBounds( new Rectangle(90,100,50,50));
     	JButton boton9 = new JButton(" 9 ");
     	boton9.setBounds( new Rectangle(150,100,50,50));
     	
     	
     	JButton botonSuma = new JButton(" + ");
     	botonSuma.setBounds( new Rectangle(210,280,80,50));
     	JButton botonResta = new JButton(" - ");
     	botonResta.setBounds( new Rectangle(210,220,80,50));
     	JButton botonPor = new JButton(" X ");
     	botonPor.setBounds( new Rectangle(210,160,80,50));
     	JButton botonDiv = new JButton(" / ");
     	botonDiv.setBounds( new Rectangle(210,100,80,50));
     	
     	JButton botonComa = new JButton(" . ");
     	botonComa.setBounds( new Rectangle(90,280,50,50));
     	JButton botonRes = new JButton("=");
     	botonRes.setBounds( new Rectangle(150,280,50,50));
     
     	
     	//COMPORTAMIENTO DE LA APLICACION SEGUN LOS EVENTOS DEL USUARIO
     	boton0.addActionListener( 
     			new ActionListener() { public void actionPerformed(ActionEvent e) { Anadir("0"); Escribir(Float.toString(numero));  }}
     	);
     	
     	boton1.addActionListener( 
     			new ActionListener() { public void actionPerformed(ActionEvent e) { Anadir("1"); Escribir(Float.toString(numero)); }}
     	);
     	boton2.addActionListener( 
     			new ActionListener() { public void actionPerformed(ActionEvent e) { Anadir("2"); Escribir(Float.toString(numero)); }}
     	);
     	boton3.addActionListener( 
     			new ActionListener() { public void actionPerformed(ActionEvent e) { Anadir("3"); Escribir(Float.toString(numero)); }}
     	);
     	boton4.addActionListener( 
     			new ActionListener() { public void actionPerformed(ActionEvent e) { Anadir("4"); Escribir(Float.toString(numero)); }}
     	);
     	boton5.addActionListener( 
     			new ActionListener() { public void actionPerformed(ActionEvent e) { Anadir("5"); Escribir(Float.toString(numero)); }}
     	);
     	boton6.addActionListener( 
     			new ActionListener() { public void actionPerformed(ActionEvent e) { Anadir("6"); Escribir(Float.toString(numero)); }}
     	);
     	boton7.addActionListener( 
     			new ActionListener() { public void actionPerformed(ActionEvent e) { Anadir("7"); Escribir(Float.toString(numero)); }}
     	);
     	boton8.addActionListener( 
     			new ActionListener() { public void actionPerformed(ActionEvent e) { Anadir("8"); Escribir(Float.toString(numero)); }}
     	);
     	boton9.addActionListener( 
     			new ActionListener() { public void actionPerformed(ActionEvent e) { Anadir("9"); Escribir(Float.toString(numero)); }}
     	);
     	botonSuma.addActionListener( 
     			new ActionListener() { public void actionPerformed(ActionEvent e) { Anadir("+"); Escribir("+"); }}
     	);
     	botonResta.addActionListener( 
     			new ActionListener() { public void actionPerformed(ActionEvent e) { Anadir("-"); Escribir("-"); }}
     	);
     	botonPor.addActionListener( 
     			new ActionListener() { public void actionPerformed(ActionEvent e) { Anadir("*"); Escribir("*"); }}
     	);
     	botonDiv.addActionListener( 
     			new ActionListener() { public void actionPerformed(ActionEvent e) { Anadir("/"); Escribir("/"); }}
     	);
     	botonComa.addActionListener( 
     			new ActionListener() { public void actionPerformed(ActionEvent e) { Anadir("."); Escribir("."); }}
     	);
     	botonRes.addActionListener( 
     			new ActionListener() { public void actionPerformed(ActionEvent e) { Anadir("=");  }}
     	);
     	
     	
     	
     	boton0.addKeyListener( new KeyAdapter(){
     		public void keyPressed( KeyEvent e ){
     		System.out.println("Usted ha presionado la tecla: "+e.getKeyText(e.getKeyCode()));
     		}
     		}); 
     	
     	
     	//CONSTRUIMOS LA VENTANA
     	ventana.add(pantalla);
     	ventana.add(boton0);
     	ventana.add(boton1);
     	ventana.add(boton2);   
     	ventana.add(boton3);
     	ventana.add(boton4);
     	ventana.add(boton5);
     	ventana.add(boton6);
     	ventana.add(boton7);
     	ventana.add(boton8);
     	ventana.add(boton9);
     	ventana.add(botonSuma);
     	ventana.add(botonResta);
     	ventana.add(botonPor);
     	ventana.add(botonDiv);
     	ventana.add(botonComa);
     	ventana.add(botonRes);
     	
		ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //Permite cerrar la ventana
     	ventana.setLayout(null);  //Para que aparezcan los botones correctamente situados.
     	ventana.setResizable(false); //No permite ridimensionar la ventana
     	ventana.setLocationRelativeTo(null); //Centra la ventana en la pantalla
    	ventana.setVisible(true); //Ventana visible
	}
}