Está en la página 1de 7

package esteban;

/**
*
* @author Esteban
*/
import
import
import
import
import
import
import

java.awt.Color;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;
javax.swing.JButton;
javax.swing.JFrame;
javax.swing.JPanel;
javax.swing.JTextField;

public class Main extends JFrame {


private JButton boton1, boton2, boton3, boton4, boton5, boton6, boton7, boto
n8,
boton9, boton0, botonMultiplicar, botonSumar, botonRestar, botonLimp
iar, botonIgualar,botondividiendo;
private boolean sumando = false, restando = false, multiplicando = false, di
vidiendo = false;
private JTextField campoTexto;
String display = "";
int numero1, numero2, resultado;
public Main() {
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setSize(220, 200);
panel.add(boton1 = new JButton("1"));
panel.add(boton2 = new JButton("2"));
panel.add(boton3 = new JButton("3"));
panel.add(boton4 = new JButton("4"));
panel.add(boton5 = new JButton("5"));
panel.add(boton6 = new JButton("6"));
panel.add(boton7 = new JButton("7"));
panel.add(boton8 = new JButton("8"));
panel.add(boton9 = new JButton("9"));
panel.add(boton0 = new JButton("0"));
panel.add(botonMultiplicar = new JButton("x"));
panel.add(botonSumar = new JButton("+"));
panel.add(botonRestar = new JButton("-"));
panel.add(botonLimpiar = new JButton("c"));
panel.add(botondividiendo = new JButton("/"));
panel.add(botonIgualar = new JButton("="));
panel.add(campoTexto = new JTextField(30));
campoTexto.setText("0");
add(panel);
campoTexto.setLocation(0, 0);
campoTexto.setSize(200, 35);
boton1.addActionListener(new EscucharAUno());
boton2.addActionListener(new EscucharADos());

boton3.addActionListener(new EscucharATres());
boton4.addActionListener(new EscucharACuatro());
boton5.addActionListener(new EscucharACinco());
boton6.addActionListener(new EscucharASeis());
boton7.addActionListener(new EscucharASiete());
boton8.addActionListener(new EscucharAOcho());
boton9.addActionListener(new EscucharANueve());
boton0.addActionListener(new EscucharACero());
botonIgualar.addActionListener(new EscucharAIgual());
botondividiendo.addActionListener(new EscucharADivide());
botonSumar.addActionListener(new EscucharASuma());
botonRestar.addActionListener(new EscucharAResta());
botonMultiplicar.addActionListener(new EscucharAMultiplica());
botonLimpiar.addActionListener(new EscucharALimpia());
boton1.setLocation(0, 120);
boton1.setSize(45, 45);
boton2.setLocation(40, 120);
boton2.setSize(45, 45);
boton3.setLocation(80, 120);
boton3.setSize(45, 45);
boton4.setLocation(0, 80);
boton4.setSize(45, 45);
boton5.setLocation(40, 80);
boton5.setSize(45, 45);
boton6.setLocation(80, 80);
boton6.setSize(45, 45);
boton7.setLocation(0, 35);
boton7.setSize(45, 45);
boton8.setLocation(40, 35);
boton8.setSize(45, 45);
boton9.setLocation(80, 35);
boton9.setSize(45, 45);
boton0.setLocation(40, 165);
boton0.setSize(45, 45);
botonIgualar.setLocation(80, 165);
botonIgualar.setSize(45, 45);
botonLimpiar.setLocation(125, 35);
botonLimpiar.setSize(45, 45);
botonSumar.setLocation(125, 120);
botonSumar.setSize(45, 45);
botonRestar.setLocation(125, 165);
botonRestar.setSize(45, 45);
botondividiendo.setLocation(85, 80);
botondividiendo.setSize(0, 45);
botonMultiplicar.setLocation(125, 80);
botonMultiplicar.setSize(45, 45);
} //public main
class EscucharAUno implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = campoTexto.getText();
if (sumando == false && restando == false && multiplicando == false
&& dividiendo == false) {
if (display.equals("0")) {
campoTexto.setText("1");
} else {
campoTexto.setText(display + "1");
}

} else {
campoTexto.setText("1");
numero2 = Integer.parseInt(campoTexto.getText());
System.out.println(numero2);
}
}
}
class EscucharADos implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = campoTexto.getText();
if (sumando == false && restando == false && multiplicando == false
&& dividiendo == false) {
if (display.equals("0")) {
campoTexto.setText("2");
} else {
campoTexto.setText(display + "2");
}
} else {
campoTexto.setText("2");
numero2 = Integer.parseInt(campoTexto.getText());
System.out.println(numero2);
}
}
}
class EscucharATres implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = campoTexto.getText();
if (sumando == false && restando == false && multiplicando == false
&& dividiendo == false) {
if (display.equals("0")) {
campoTexto.setText("3");
} else {
campoTexto.setText(display + "3");
}
} else {
campoTexto.setText("3");
numero2 = Integer.parseInt(campoTexto.getText());
System.out.println(numero2);
}
}
}
class EscucharACuatro implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = campoTexto.getText();
if (sumando == false && restando == false && multiplicando == false
&& dividiendo == false) {
if (display.equals("0")) {
campoTexto.setText("4");
} else {
campoTexto.setText(display + "4");
}
} else {

campoTexto.setText("4");
numero2 = Integer.parseInt(campoTexto.getText());
}
}
}
class EscucharACinco implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = campoTexto.getText();
if (sumando == false && restando == false && multiplicando == false
&& dividiendo == false) {
if (display.equals("0")) {
campoTexto.setText("5");
} else {
campoTexto.setText(display + "5");
}
} else {
campoTexto.setText("5");
numero2 = Integer.parseInt(campoTexto.getText());
System.out.println(numero2);
}
}
}
class EscucharASeis implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = campoTexto.getText();
if (sumando == false && restando == false && multiplicando == false
&& dividiendo == false) {
if (display.equals("0")) {
campoTexto.setText("6");
} else {
campoTexto.setText(display + "6");
}
} else {
campoTexto.setText("6");
numero2 = Integer.parseInt(campoTexto.getText());
System.out.println(numero2);
}
}
}
class EscucharASiete implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = campoTexto.getText();
if (sumando == false && restando == false && multiplicando == false
&& dividiendo == false) {
if (display.equals("0")) {
campoTexto.setText("7");
} else {
campoTexto.setText(display + "7");
}
} else {
campoTexto.setText("7");
numero2 = Integer.parseInt(campoTexto.getText());

System.out.println(numero2);
}
}
}
class EscucharAOcho implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = campoTexto.getText();
if (sumando == false && restando == false && multiplicando == false
&& dividiendo == false) {
if (display.equals("0")) {
campoTexto.setText("8");
} else {
campoTexto.setText(display + "8");
}
} else {
campoTexto.setText("8");
numero2 = Integer.parseInt(campoTexto.getText());
System.out.println(numero2);
}
}
}
class EscucharANueve implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = campoTexto.getText();
if (sumando == false && restando == false && multiplicando == false
&& dividiendo == false) {
if (display.equals("0")) {
campoTexto.setText("9");
} else {
campoTexto.setText(display + "9");
}
} else {
campoTexto.setText("9");
numero2 = Integer.parseInt(campoTexto.getText());
System.out.println(numero2);
}
}
}
class EscucharACero implements ActionListener {
public void actionPerformed(ActionEvent e) {
display = campoTexto.getText();
if (sumando == false && restando == false && multiplicando == false
&& dividiendo == false) {
if (display.equals("0")) {
campoTexto.setText("0");
} else {
campoTexto.setText(display + "0");
}
} else {
campoTexto.setText("0");
numero2 = Integer.parseInt(campoTexto.getText());

System.out.println(numero2);
}
}
}
class EscucharAIgual implements ActionListener {
public void actionPerformed(ActionEvent e) {
display=campoTexto.getText();
botonIgualar.setBackground(Color.GRAY);
campoTexto.setText(display+("="));
numero1=Integer.parseInt(campoTexto.getText());
System.out.println(numero1=numero2);
botonIgualar.setBackground(Color.BLUE);
boolean Igualar = true;
System.out.println("igualando="+Igualar);
}
}
class EscucharADivide implements ActionListener {
public void actionPerformed(ActionEvent e) {
display=campoTexto.getText();
botondividiendo.setBackground(Color.GRAY);
campoTexto.setText(display+("/"));
numero1=Integer.parseInt(campoTexto.getText());
System.out.println(numero1);
botondividiendo.setBackground(Color.BLUE);
dividiendo=true;
System.out.println("dividiendo="+dividiendo);
}
}
class EscucharASuma implements ActionListener {
public void actionPerformed(ActionEvent e) {
display=campoTexto.getText();
botonSumar.setBackground(Color.GRAY);
campoTexto.setText(display+("+"));
numero1=Integer.parseInt(campoTexto.getText());
System.out.println(numero1);
botonSumar.setBackground(Color.BLUE);
sumando=true;
System.out.println("sumando="+sumando);
}
}
class EscucharAResta implements ActionListener {
public void actionPerformed(ActionEvent e) {
display=campoTexto.getText();
botonRestar.setBackground(Color.GRAY);
campoTexto.setText(display+("-"));
numero1=Integer.parseInt(campoTexto.getText());
System.out.println(numero1);
botonRestar.setBackground(Color.BLUE);
restando=true;
System.out.println("restando="+restando);
}
}

class EscucharAMultiplica implements ActionListener {


public void actionPerformed(ActionEvent e) {
display=campoTexto.getText();
botonMultiplicar.setBackground(Color.GRAY);
campoTexto.setText(display+("*"));
numero1=Integer.parseInt(campoTexto.getText());
System.out.println(numero1);
botonMultiplicar.setBackground(Color.BLUE);
multiplicando=true;
System.out.println("multiplicando="+multiplicando);
}
}
class EscucharALimpia implements ActionListener {
public void actionPerformed(ActionEvent e) {
campoTexto.setText("0");
}
}
public static void main(String[] args) {
Main calc = new Main();
calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calc.setVisible(true);
calc.setSize(220, 260);
}
}

También podría gustarte