0% found this document useful (0 votes)
24 views3 pages

Calcula Dora

This document contains the code for a basic calculator application created in Java. It defines a Calculadora1 class that extends JFrame and implements ItemListener and ActionListener interfaces. The class contains code to create a graphical user interface with number buttons, operator buttons, and a display field. It also contains event handling code to update the display field when buttons are clicked and add numbers or operators to the calculation. The main method creates an instance of the class and sets properties of the window before making it visible.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Calcula Dora

This document contains the code for a basic calculator application created in Java. It defines a Calculadora1 class that extends JFrame and implements ItemListener and ActionListener interfaces. The class contains code to create a graphical user interface with number buttons, operator buttons, and a display field. It also contains event handling code to update the display field when buttons are clicked and add numbers or operators to the calculation. The main method creates an instance of the class and sets properties of the window before making it visible.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package calculadora1;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

/**
*
* @author UDEA-LAB2
*/
public class Calculadora1 extends JFrame implements ItemListener, ActionListener{
// las variables van dentro de la clase principal
JLabel txt1;
JTextField Valor1;
public static String result ="";
JButton B1,B2,B3,B4,B5,B6,B7,B8,B9,B10,B11,B12,B13,B14,B15,B16;// Botones para
la nusmeros
public Calculadora1(){//CONSTRUCTOR
setLayout(null);
B1 = new JButton("0");
B1.setBounds(120,270,50,30);
add(B1);
B2 = new JButton("1");
B2.setBounds(60,220,50,30);
add(B2);
B3 = new JButton("2");
B3.setBounds(120,220,50,30);
add(B3);
B4 = new JButton("3");
B4.setBounds(180,220,50,30);
add(B4);
B5 = new JButton("4");
B5.setBounds(60,170,50,30);
add(B5);
B6 = new JButton("5");
B6.setBounds(120,170,50,30);
add(B6);
B7 = new JButton("6");
B7.setBounds(180,170,50,30);
add(B7);
B8 = new JButton("7");
B8.setBounds(60,120,50,30);
add(B8);
B9 = new JButton("8");
B9.setBounds(120,120,50,30);
add(B9);
B10 = new JButton("9");
B10.setBounds(180,120,50,30);
add(B10);
B11 = new JButton("+");
B11.setBounds(240,270,50,30);
add(B11);
B12 = new JButton("-");
B12.setBounds(240,220,50,30);
add(B12);
B13 = new JButton("x");
B13.setBounds(240,170,50,30);
add(B13);
B14 = new JButton("/");
B14.setBounds(240,120,50,30);
add(B14);
B15 = new JButton("=");
B15.setBounds(180,270,50,30);
add(B15);
B16 = new JButton("C");
B16.setBounds(60,270,50,30);
add(B16);
// label
txt1 = new JLabel("Calculadora");
txt1.setBounds(40,20,300,30);
add(txt1);
// caja de texto
Valor1 = new JTextField();
Valor1.setBounds(60,70,230,30);
add(Valor1);
B1.addActionListener(this);
B2.addActionListener(this);
B3.addActionListener(this);
B4.addActionListener(this);
B5.addActionListener(this);
B6.addActionListener(this);
B7.addActionListener(this);
B8.addActionListener(this);
B9.addActionListener(this);
B10.addActionListener(this);
B11.addActionListener(this);
B12.addActionListener(this);
B13.addActionListener(this);
B14.addActionListener(this);
B15.addActionListener(this);
B16.addActionListener(this);

}
public void itemStateChanged(ItemEvent e){
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==B1){
result = result + "0";
Valor1.setText(result);
}
if(e.getSource()==B2){
result = result + "1";
Valor1.setText(result);
}
if(e.getSource()==B3){
result = result + "2";
Valor1.setText(result);
}
if(e.getSource()==B4){
result = result + "3";
Valor1.setText(result);
}
if(e.getSource()==B5){
result = result + "4";
Valor1.setText(result);
}
if(e.getSource()==B6){
result = result + "5";
Valor1.setText(result);
}
if(e.getSource()==B7){
result = result + "6";
Valor1.setText(result);
}
if(e.getSource()==B8){
result = result + "7";
Valor1.setText(result);
}
if(e.getSource()==B9){
result = result + "8";
Valor1.setText(result);
}
if(e.getSource()==B10){
result = result + "9";
Valor1.setText(result);
}
if(e.getSource()==B11){
Valor1.setText("+");
}
if(e.getSource()==B12){
Valor1.setText("-");
}
if(e.getSource()==B13){
Valor1.setText("x");
}
if(e.getSource()==B14){
Valor1.setText("/");
}
if(e.getSource()==B15){
Valor1.setText("=");
}
if(e.getSource()==B16){
result = result + clear;
Valor1.setText(result);
}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Calculadora1 sc = new Calculadora1();// libreria con un nuevo nombre
sc.setBounds(100,110,600,600);// se crea una ventana de intefas, dando
medidas de posicionamiento x,y,ancho y largo
sc.setVisible(true);// este es para que la ventana sea visible
}

You might also like