• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Java AWT Calculadora
 

Java AWT Calculadora

on

  • 2,370 views

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.

Statistics

Views

Total Views
2,370
Views on SlideShare
1,002
Embed Views
1,368

Actions

Likes
0
Downloads
0
Comments
0

3 Embeds 1,368

http://bitcoach.wordpress.com 1343
http://www.slideshare.net 20
url_unknown 5

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Java AWT Calculadora Java AWT Calculadora Document Transcript

    • 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
    • //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
    • //--- 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