package gui;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Calculadora extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JButton somar, subtrair, multiplicar, dividir, limpar, voltar, ponto, igual, raizQuadrada;
private JButton bt0, bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9;
private JTextField display;
private double n1, n2;
private boolean segundo = true;
private boolean utd = false;
private char operacao = ' ';
public Calculadora() {
//Ajuste dos Formulários
setTitle("Calculadora");
setSize(245, 260);
setLocation(WIDTH, WIDTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
getContentPane().setLayout(null);
setLocationRelativeTo(null);
//Inicialização dos botões
display = new JTextField();
display.setEditable(false);
display.setHorizontalAlignment(JTextField.RIGHT);
somar = new JButton("+");
subtrair = new JButton("-");
multiplicar = new JButton("*");
dividir = new JButton("/");
raizQuadrada = new JButton("√");
limpar = new JButton("CE");
voltar = new JButton("←");
ponto = new JButton(".");
igual = new JButton("=");
bt0 = new JButton("0");
bt1 = new JButton("1");
bt2 = new JButton("2");
bt3 = new JButton("3");
bt4 = new JButton("4");
bt5 = new JButton("5");
bt6 = new JButton("6");
bt7 = new JButton("7");
bt8 = new JButton("8");
bt9 = new JButton("9");
//Posicionar componentes no Formulário
display.setBounds(10, 9, 220, 40);
voltar.setBounds(10, 55, 50, 30);
limpar.setBounds(65, 55, 50, 30);
raizQuadrada.setBounds(120, 55, 50, 30);
dividir.setBounds(180, 55, 50, 30);
bt7.setBounds(10, 90, 50, 30);
bt8.setBounds(65, 90, 50, 30);
bt9.setBounds(120, 90, 50, 30);
multiplicar.setBounds(180, 90, 50, 30);
bt4.setBounds(10, 125, 50, 30);
bt5.setBounds(65, 125, 50, 30);
bt6.setBounds(120, 125, 50, 30);
subtrair.setBounds(180, 125, 50, 30);
bt1.setBounds(10, 160, 50, 30);
bt2.setBounds(65, 160, 50, 30);
bt3.setBounds(120, 160, 50, 30);
igual.setBounds(180, 160, 50, 65);
bt0.setBounds(10, 195, 50, 30);
ponto.setBounds(65, 195, 50, 30);
somar.setBounds(120, 195, 50, 30);
//Cores dos componentes
bt0.setForeground(Color.BLUE);
bt1.setForeground(Color.BLUE);
bt2.setForeground(Color.BLUE);
bt3.setForeground(Color.BLUE);
bt4.setForeground(Color.BLUE);
bt5.setForeground(Color.BLUE);
bt6.setForeground(Color.BLUE);
bt7.setForeground(Color.BLUE);
bt8.setForeground(Color.BLUE);
bt9.setForeground(Color.BLUE);
ponto.setForeground(Color.BLACK);
igual.setForeground(Color.red);
voltar.setForeground(Color.red);
limpar.setForeground(Color.red);
somar.setForeground(Color.red);
subtrair.setForeground(Color.red);
multiplicar.setForeground(Color.red);
dividir.setForeground(Color.red);
raizQuadrada.setForeground(Color.red);
//Registro de componentes que executam ações
bt0.addActionListener(this);
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
bt5.addActionListener(this);
bt6.addActionListener(this);
bt7.addActionListener(this);
bt8.addActionListener(this);
bt9.addActionListener(this);
somar.addActionListener(this);
subtrair.addActionListener(this);
multiplicar.addActionListener(this);
dividir.addActionListener(this);
raizQuadrada.addActionListener(this);
limpar.addActionListener(this);
voltar.addActionListener(this);
ponto.addActionListener(this);
igual.addActionListener(this);
//Colocar os componentes no formulário
this.getContentPane().add(display);
this.getContentPane().add(somar);
this.getContentPane().add(subtrair);
this.getContentPane().add(multiplicar);
this.getContentPane().add(dividir);
this.getContentPane().add(raizQuadrada);
this.getContentPane().add(limpar);
this.getContentPane().add(voltar);
this.getContentPane().add(ponto);
this.getContentPane().add(igual);
this.getContentPane().add(bt0);
this.getContentPane().add(bt1);
this.getContentPane().add(bt2);
this.getContentPane().add(bt3);
this.getContentPane().add(bt4);
this.getContentPane().add(bt5);
this.getContentPane().add(bt6);
this.getContentPane().add(bt7);
this.getContentPane().add(bt8);
this.getContentPane().add(bt9);
}
public static void main(String[] args) {
JFrame frame = new Calculadora();
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent acao) {
if (acao.getSource() == bt0) {
if (display.getText().equals("")) {
return;
} else {
numDig("0");
}
} else if (acao.getSource() == bt1) {
numDig("1");
} else if (acao.getSource() == bt2) {
numDig("2");
} else if (acao.getSource() == bt3) {
numDig("3");
} else if (acao.getSource() == bt4) {
numDig("4");
} else if (acao.getSource() == bt5) {
numDig("5");
} else if (acao.getSource() == bt6) {
numDig("6");
} else if (acao.getSource() == bt7) {
numDig("7");
} else if (acao.getSource() == bt8) {
numDig("8");
} else if (acao.getSource() == bt9) {
numDig("9");
} else if (acao.getSource() == ponto) {
decimal();
} if (acao.getSource() == limpar) {
display.setText("");
operacao = ' ';
segundo = true;
utd = false;
n1 = 0;
n2 = 0;
} if (acao.getSource() == voltar) {
int tam = display.getText().length();
if (tam > 0) {
display.setText(display.getText().substring(0, tam - 1));
} else {
return;
}
} if (acao.getSource() == somar) {
igual();
calcular('+');
} if (acao.getSource() == subtrair) {
igual();
calcular('-');
} if (acao.getSource() == multiplicar) {
igual();
calcular('*');
} if (acao.getSource() == dividir) {
igual();
calcular('/');
} if (acao.getSource() == raizQuadrada) {
calcular('√');
igual();
} if (acao.getSource() == igual) {
igual();
}
}
private void calcular(char c) {
if (!display.getText().equals("")) {
operacao = c;
segundo = false;
utd = false;
n1 = Double.parseDouble(display.getText());
} else {
return;
}
}
public void numDig(String n) {
if (segundo == true) {
display.setText(display.getText() + n);
} else {
display.setText("");
display.setText(display.getText() + n);
segundo = true;
}
}
private void decimal() {
if (utd == false) {
if (display.getText().length() < 1) {
numDig("0.");
} else if (possuePonto() == true) {
return;
} else {
numDig(".");
}
utd = true;
}
}
private void igual() {
if (operacao == '+') {
n2 = n1 + Double.parseDouble(display.getText());
display.setText("" + n2);
n1 = 0;
n2 = 0;
operacao = ' ';
segundo = false;
} else if (operacao == '-') {
n2 = n1 - Double.parseDouble(display.getText());
display.setText("" + n2);
n1 = 0;
n2 = 0;
operacao = ' ';
segundo = false;
} else if (operacao == '*') {
n2 = n1 * Double.parseDouble(display.getText());
display.setText("" + n2);
n1 = 0;
n2 = 0;
operacao = ' ';
segundo = false;
} else if (operacao == '/') {
n2 = n1 / Double.parseDouble(display.getText());
display.setText("" + n2);
n1 = 0;
n2 = 0;
operacao = ' ';
segundo = false;
} else if (operacao == '√') {
n2 = Math.sqrt(n1) % Double.parseDouble(display.getText());
display.setText("" + n2);
n1 = 0;
n2 = 0;
operacao = ' ';
segundo = false;
}
}
private boolean possuePonto() {
String v = display.getText();
boolean b = false;
for(int i=0; i < v.length(); i++) {
if (v.substring(i, i).equals(".")) {
b = true;
} else {
b = false;
}
}
return b;
}
}