import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Calculadora implements ActionListener {
private JFrame ventana;
private JTextField display;
private JPanel panelBotones;
private JButton boton1, boton2, boton3, boton4, boton5, boton6, boton7, boton8, boton9, boton0;
private JButton botonSuma, botonResta, botonMultiplicacion, botonDivision, botonIgual;
private String operacion;
private int num1, num2, resultado;
public Calculadora() {
ventana = new JFrame("Calculadora");
ventana.setSize(300, 300);
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.setLocationRelativeTo(null);
ventana.setLayout(new BorderLayout());
display = new JTextField();
display.setFont(new Font("Arial", Font.PLAIN, 18));
display.setHorizontalAlignment(SwingConstants.RIGHT);
display.setEditable(false);
ventana.add(display, BorderLayout.NORTH);
panelBotones = new JPanel();
panelBotones.setLayout(new GridLayout(4, 4));
boton1 = new JButton("1");
boton2 = new JButton("2");
boton3 = new JButton("3");
boton4 = new JButton("4");
boton5 = new JButton("5");
boton6 = new JButton("6");
boton7 = new JButton("7");
boton8 = new JButton("8");
boton9 = new JButton("9");
boton0 = new JButton("0");
botonSuma = new JButton("+");
botonResta = new JButton("-");
botonMultiplicacion = new JButton("*");
botonDivision = new JButton("/");
botonIgual = new JButton("=");
panelBotones.add(boton1);
panelBotones.add(boton2);
panelBotones.add(boton3);
panelBotones.add(botonSuma);
panelBotones.add(boton4);
panelBotones.add(boton5);
panelBotones.add(boton6);
panelBotones.add(botonResta);
panelBotones.add(boton7);
panelBotones.add(boton8);
panelBotones.add(boton9);
panelBotones.add(botonMultiplicacion);
panelBotones.add(boton0);
panelBotones.add(botonIgual);
panelBotones.add(botonDivision);
boton1.addActionListener(this);
boton2.addActionListener(this);
boton3.addActionListener(this);
boton4.addActionListener(this);
boton5.addActionListener(this);
boton6.addActionListener(this);
boton7.addActionListener(this);
boton8.addActionListener(this);
boton9.addActionListener(this);
boton0.addActionListener(this);
botonSuma.addActionListener(this);
botonResta.addActionListener(this);
botonMultiplicacion.addActionListener(this);
botonDivision.addActionListener(this);
botonIgual.addActionListener(this);
ventana.add(panelBotones, BorderLayout.CENTER);
ventana.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new Calculadora();
});
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == boton1) {
display.setText(display.getText() + "1");
} else if (e.getSource() == boton2) {
display.setText(display.getText() + "2");
} else if (e.getSource() == boton3) {
display.setText(display.getText() + "3");
} else if (e.getSource() == boton4) {
display.setText(display.getText() + "4");
} else if (e.getSource() == boton5) {
display.setText(display.getText() + "5");
} else if (e.getSource() == boton6) {
display.setText(display.getText() + "6");
} else if (e.getSource() == boton7) {
display.setText(display.getText() + "7");
} else if (e.getSource() == boton8) {
display.setText(display.getText() + "8");
} else if (e.getSource() == boton9) {
display.setText(display.getText() + "9");
} else if (e.getSource() == boton0) {
display.setText(display.getText() + "0");
} else if (e.getSource() == botonSuma) {
num1 = Integer.parseInt(display.getText());
operacion = "+";
display.setText("");
} else if (e.getSource() == botonResta) {
num1 = Integer.parseInt(display.getText());
operacion = "-";
display.setText("");
} else if (e.getSource() == botonMultiplicacion) {
num1 = Integer.parseInt(display.getText());
operacion = "*";
display.setText("");
} else if (e.getSource() == botonDivision) {
num1 = Integer.parseInt(display.getText());
operacion = "/";
display.setText("");
} else if (e.getSource() == botonIgual) {
num2 = Integer.parseInt(display.getText());
if (operacion.equals("+")) {
resultado = num1 + num2;
} else if (operacion.equals("-")) {
resultado = num1 - num2;
} else if (operacion.equals("*")) {
resultado = num1 * num2;
} else if (operacion.equals("/")) {
resultado = num1 / num2;
}
display.setText(Integer.toString(resultado));
}
}
}