import java.awt.;
import java.awt.event.;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import java.awt.GridLayout;
// Incio classe Calculadora
public class Calculadora
extends JFrame implements ActionListener
{
//Variaveis
private BorderLayout frm1;
private JPanel pnl1;
private JPanel pnl2;
private JMenuBar mnb1;
private JMenu mn1;
private JMenuItem mi1;
private JButton btn[];
private JButton btnLimpar;
private JButton btnVoltar;
private JButton botoes;
private boolean click = false;
private double v1=0, result=0;
//Botoes
//Posicao do vetor 0=1, 1=2, 2=3, 3=+, 4=4, 5=5,
//6=6, 7=-, 8=7, 9=8, 10=9, 11="=", 12=0, 13=., 14=*, 15=/
private final String valoresBts[] = { "1", "2", "3",
"+", "4", "5",
"6", "-", "7",
"8", "9","=","0",".","*","/"};
private final String valoresClr[] = { "Voltar", "Limpar"};
// Visor
private TextField txtF1 = new TextField("");
// variaveis de valor
private float valor1 = 0;
// Variavel de Operacao
// 1 - Somar
// 2 - Subtrair
// 3 - Dividir
// 4 - Multiplicar
private int operacao = 0;
public void setValor1()
{
this.valor1 = Float.parseFloat(txtF1.getText());
}
public float getValor1()
{
return valor1;
}
public Calculadora(){
//Menu
mnb1 = new JMenuBar();
mn1 = new JMenu("Arquivo");
mi1 = new JMenuItem("Sair");
//Painel
frm1= new BorderLayout(5,5);
pnl1= new JPanel(new GridLayout(4,3,2,2));
pnl2= new JPanel(new GridLayout(1,1));
btn= new JButton[valoresBts.length];
//Criando os botoes
for (int i=0;i < valoresBts.length;i++)
{
btn[i]=new JButton (valoresBts[i]);
pnl1.add(btn[i]);
}
btnLimpar= new JButton("Limpar");
btnVoltar= new JButton("Voltar");
pnl2.add(btnVoltar);
pnl2.add(btnLimpar);
for(int i=0;i<valoresBts.length;i++){
btn[i].addActionListener(this);
}
btnLimpar.addActionListener(this);
btnVoltar.addActionListener(this);
// Criando Painel
setLayout(frm1);
setSize(230,300); // tamamho do Frame
setLocation(250,300);// posiciona na tela
setResizable(false); // congela a janela
//criando meu menu!!
mn1.add(mi1);
mnb1.add(mn1);
setJMenuBar(mnb1);
//Adicionando o campo de texto
add(txtF1,BorderLayout.NORTH);
//Adicionando os GridLayouts
add(pnl1,BorderLayout.CENTER);
add(pnl2,BorderLayout.SOUTH );
//Habilita mostrar o frame // sempre deixa-la como ultima linha!!
setVisible(true);
}
public void actionPerformed(ActionEvent evento){
// limpar
//Posicao do vetor 0=1, 1=2, 2=3, 3=+, 4=4, 5=5,
//6=6, 7=-, 8=7, 9=8, 10=9, 11="=", 12=0, 13=., 14=*, 15=/
if(evento.getSource() == btnLimpar)
{
click = false;
txtF1.setText("");
}
if(evento.getSource() == btn[12])
{
if(click)
{
txtF1.setText("0");
}
else
{
txtF1.setText(txtF1.getText()+"0");
}
click = false;
}
if(evento.getSource() == btn[0])
{
if(click)
{
txtF1.setText("1");
}
else
{
txtF1.setText(txtF1.getText()+"1");
}
click = false;
}
if(evento.getSource() == btn[1])
{
if(click)
{
txtF1.setText("2");
}
else
{
txtF1.setText(txtF1.getText()+"2");
}
click = false;
}
if(evento.getSource() == btn[2])
{
if(click)
{
txtF1.setText("3");
}
else
{
txtF1.setText(txtF1.getText()+"3");
}
click = false;
}
if(evento.getSource() == btn[4])
{
if(click)
{
txtF1.setText("4");
}
else
{
txtF1.setText(txtF1.getText()+"4");
}
click = false;
}
if(evento.getSource() == btn[5])
{
if(click)
{
txtF1.setText("5");
}
else
{
txtF1.setText(txtF1.getText()+"5");
}
click = false;
}
if(evento.getSource() == btn[6])
{
if(click)
{
txtF1.setText("6");
}
else
{
txtF1.setText(txtF1.getText()+"6");
}
click = false;
}
if(evento.getSource() == btn[8])
{
if(click)
{
txtF1.setText("7");
}
}
if(evento.getSource() == btn[9])
{
if(click)
{
txtF1.setText("8");
}
else
{
txtF1.setText(txtF1.getText()+"8");
}
click = false;
}
if(evento.getSource() == btn[10])
{
if(click)
{
txtF1.setText("9");
}
else
{
txtF1.setText(txtF1.getText()+"9");
}
click = false;
}
// Operacoes
if(evento.getSource() == btn[3])
{
click=true;
setValor1();
operacao = 1;
}
if(evento.getSource() == btn[7])
{
click=true;
setValor1();
operacao = 2;
}
if(evento.getSource() == btn[15])
{
click=true;
setValor1();
operacao = 3;
}
if(evento.getSource() == btn[14])
{
click=true;
setValor1();
operacao = 4;
}
// Menu de operacoes
switch (operacao){
//somar
case 1 :
{
operacao = 0;
txtF1.setText(Float.toString(getValor1() + Float.parseFloat(txtF1.getText())));
break;
}
//diminuir
case 2 :
{
operacao = 0;
txtF1.setText(Float.toString(getValor1() - Float.parseFloat(txtF1.getText())));
break;
}
//dividir
case 3 :
{
try
{
operacao = 0;
txtF1.setText(Float.toString(getValor1() / Float.parseFloat(txtF1.getText())));
}
catch(ArithmeticException ex)
{
txtF1.setText("");
System.out.println("? impossÌvel divisao por zero ! erro : " + ex.getMessage());
}
break;
}
//multipicar
case 4 :
{
operacao = 0;
txtF1.setText(Float.toString(getValor1() / Float.parseFloat(txtF1.getText())));
break;
}
default :
System.out.println("Escolha uma operacao");
break;
}
}
public static void main(String [] args)
{
new Calculadora();
}
}
// fim classe Calculadora