1. En esta práctica vamos a crear una sencilla calculadora utilizando el framework AWT (Abstract Window Toolkit) de Java.
Etiquetas: Frame, Panel, paquete AWT, Gestión de eventos, Notificador, Listener, interfaces y adaptadores, ActionListener,
WindowAdapter,…
Escuchador_WindowListener.java
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Escuchador_WindowListener extends WindowAdapter {
public void windowClosing(WindowEvent evento){
System.exit(0);
}
}
Calculadora.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.Panel;
import java.awt.GridLayout;
import java.awt.Button;
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 static final long serialVersionUID = 1L;
private TextField pantalla = null;
private Button tecla = null;
private int operando1 = 0;
private int operando2 = 0;
private String operacion = null;
private boolean newDigitBlock = true;
bitCoach::Juan Bautista Cascallar Lorenzo
2. //Constructor
public Calculadora(){
this.setTitle("Calculadora");
this.setSize(200, 200);
this.setLayout(new BorderLayout());
this.addWindowListener(new Escuchador_WindowListener());
//--- Display ---
pantalla = new TextField();
pantalla.setText("0");
pantalla.addKeyListener(this);
pantalla.setBackground(Color.CYAN);
this.add(pantalla,BorderLayout.NORTH);
//--- Teclas ---
Panel panel01 = new Panel();
panel01.setLayout(new GridLayout(5,3));
tecla = new Button("0"); tecla.addActionListener(this); panel01.add(tecla);
tecla = new Button("1"); tecla.addActionListener(this); panel01.add(tecla);
tecla = new Button("+"); tecla.addActionListener(this); panel01.add(tecla);
tecla = new Button("2"); tecla.addActionListener(this); panel01.add(tecla);
tecla = new Button("3"); tecla.addActionListener(this); panel01.add(tecla);
tecla = new Button("-"); tecla.addActionListener(this); panel01.add(tecla);
tecla = new Button("4"); tecla.addActionListener(this); panel01.add(tecla);
tecla = new Button("5"); tecla.addActionListener(this); panel01.add(tecla);
tecla = new Button("="); tecla.addActionListener(this); panel01.add(tecla);
tecla = new Button("6"); tecla.addActionListener(this); panel01.add(tecla);
tecla = new Button("7"); tecla.addActionListener(this); panel01.add(tecla);
tecla = new Button("C"); tecla.addActionListener(this); panel01.add(tecla);
tecla = new Button("8"); tecla.addActionListener(this); panel01.add(tecla);
tecla = new Button("9"); tecla.addActionListener(this); panel01.add(tecla);
tecla = new Button(); tecla.setEnabled(false); panel01.add(tecla);
this.add(panel01,BorderLayout.CENTER);
}
//--- Método de la interfaz ActionListener ---
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String teclaPulsada = ((Button)e.getSource()).getLabel();
if(teclaPulsada.equals("C")){
pantalla.setText("0");
operando1 = 0;
operando2 = 0;
newDigitBlock = true;
}else if(teclaPulsada.equals("=")){
operando2 = Integer.parseInt(pantalla.getText());
if(operacion != null){
if(operacion.equals("+")){
pantalla.setText(new Integer(operando1 + operando2).toString());
}else if (operacion.equals("-")){
pantalla.setText(new Integer(operando1 - operando2).toString());
}
}
newDigitBlock = true;
operacion = null;
}else if(teclaPulsada.equals("+") || teclaPulsada.equals("-")){
operacion = teclaPulsada;
operando1 = Integer.parseInt(pantalla.getText());
newDigitBlock = true;
}else{
if(newDigitBlock == true){
pantalla.setText(teclaPulsada);
newDigitBlock = false;
}else{
pantalla.setText(pantalla.getText() + teclaPulsada);
//--- Quitar 0 al principio ---
if((pantalla.getText().length() > 1) && (pantalla.getText().charAt(0) == '0')){
pantalla.setText(pantalla.getText().substring(1));
}
}
}
}//Fin function
bitCoach::Juan Bautista Cascallar Lorenzo
3. //--- Métodos de la interfaz KeyListener ---
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
TestCalculadora.java
public class TestCalculadora {
public static void main(String[] args) {
// TODO Auto-generated method stub
Calculadora c = new Calculadora();
//c.pack();
c.setVisible(true);
}
}
bitCoach::Juan Bautista Cascallar Lorenzo