Your SlideShare is downloading. ×
Java AWT Calculadora
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Java AWT Calculadora

2,409

Published on

En esta práctica vamos a crear una sencilla calculadora utilizando el framework AWT (Abstract Window Toolkit) de Java.

En esta práctica vamos a crear una sencilla calculadora utilizando el framework AWT (Abstract Window Toolkit) de Java.

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,409
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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.javaimport java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class Escuchador_WindowListener extends WindowAdapter { public void windowClosing(WindowEvent evento){ System.exit(0); }}Calculadora.javaimport 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. //Constructorpublic 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 ---@Overridepublic 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.javapublic 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

×