package BotonesYeventos;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Calculadora extends Frame implements ActionListener,KeyListener
{
private TextField t=null;
private int oper1=0;//primer operando
private int oper2=0;//segundo operando
private String command=null;//operacion
private boolean sw=true;//switch para añadir o brrar digitos.
public Calculadora()
{
this.setTitle("Calculadora");
this.addWindowListener(new MiWindowListener());
this.setLayout(new BorderLayout());
t=new TextField("0");
t.setBackground(Color.YELLOW);
t.addKeyListener(this);
Panel aux=new Panel();
aux.setLayout(new GridLayout(3,4));
Button b=null;
b=new Button("1");
b.addActionListener(this);
aux.add(b);
Button b1=null;
b1=new Button("2");
b1.addActionListener(this);
aux.add(b1);
Button b2=null;
b2=new Button("3");
b2.addActionListener(this);
aux.add(b2);
Button b3=null;
b3=new Button("+");
b3.addActionListener(this);
aux.add(b3);
Button b4=null;
b4=new Button("4");
b4.addActionListener(this);
aux.add(b4);
Button b5=null;
b5=new Button("5");
b5.addActionListener(this);
aux.add(b5);
Button b6=null;
b6=new Button("6");
b6.addActionListener(this);
aux.add(b6);
Button b7=null;
b7=new Button("-");
b7.addActionListener(this);
aux.add(b7);
Button b8=null;
b8=new Button("7");
b8.addActionListener(this);
aux.add(b8);
Button b9=null;
b9=new Button("8");
b9.addActionListener(this);
aux.add(b9);
Button b10=null;
b10=new Button("9");
b10.addActionListener(this);
aux.add(b10);
Button b11=null;
b11=new Button("=");
b11.addActionListener(this);
aux.add(b11);
this.add(t,BorderLayout.NORTH);
this.add(aux,BorderLayout.CENTER);
this.add(aux,BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent ev)
{
String aux=((Button)ev.getSource()).getLabel();
if(aux.equals("+")||aux.equals("-"))
{
command=aux;
oper1=Integer.parseInt(t.getText());
sw=true;
}
else if(aux.equals("="))
{
oper2=Integer.parseInt(t.getText());
if(command.equals("+"))
{
t.setText(new Integer(oper1+oper2).toString());
oper1=oper1+oper2;//permite teclear un numero
}
else
{
t.setText(new Integer(oper1-oper2).toString());
oper1=oper1-oper2;
}
sw=true;
}
else
{
if(sw)
{
t.setText(aux);
sw=false;
}
else
t.setText(t.getText()+aux);
}
}
public void keyPressed(KeyEvent ev)
{
}
//se ha producido un evento de tipo keyreleassed
public void keyReleased(KeyEvent ev)
{
char tmp=ev.getKeyChar();
if(!Character.isDigit(tmp))
{
t.setText(t.getText().substring(0,t.getText().length()-1));
t.setCaretPosition(t.getText().length());//posicionar al final del texto
}
}
//seha prodicido un key event de tipo keytyped
public void keyTyped(KeyEvent ev)
{
}
}
--------------------------------
package BotonesYeventos;
public class CalculadoraTest {
/**
* @param args
*/
public static void main(String[] args) {
Calculadora c =new Calculadora();
c.pack();
c.setVisible(true);
}
}
Y.... el package donde esta?
ResponderEliminarpackage BotonesYeventos;
ResponderEliminarEs el nombre del package al crear el proyecto.
Saludos..