Página 1 de 1

Calculadora Java

Publicado: 11 Feb 2011, 03:43
por m4rtyr

Código: Seleccionar todo

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calc extends JFrame implements ActionListener
{
private long x,y,result;
public static int Opp=0,notopp=1;
private JTextField view=new JTextField();
private JButton adds=new JButton("+");
private JButton min=new JButton("-");
private JButton over=new JButton("/");
private JButton mul=new JButton("*");
private JButton equ=new JButton("equal");
private JButton Clear=new JButton("Clear");
public JButton about=new JButton("About");
private JButton b0=new JButton("0");
private JButton b1=new JButton("1");
private JButton b2=new JButton("2");
private JButton b3=new JButton("3");
private JButton b4=new JButton("4");
private JButton b5=new JButton("5");
private JButton b6=new JButton("6");
private JButton b7=new JButton("7");
private JButton b8=new JButton("8");
private JButton b9=new JButton("9");

public Calc (){
JPanel p1 = new JPanel();
p1.add(adds);
p1.add(min);
p1.add(mul);
p1.add(over);
p1.add(Clear);
p1.add(equ);
p1.setLayout(new GridLayout(3,2));
JPanel p2 = new JPanel();
p2.add(b0);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.setLayout(new GridLayout(4,3));
b0.addActionListener(this);
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);
adds.addActionListener(this);
mul.addActionListener(this);
min.addActionListener(this);
over.addActionListener(this);
Clear.addActionListener(this);
about.addActionListener(this);
equ.addActionListener(this);
about.setVisible(false);
add(about,BorderLayout.CENTER);
add(p1,BorderLayout.EAST);
add(p2,BorderLayout.WEST);
add(view,BorderLayout.NORTH);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==Clear){view.setText("");x=0;y=0;notopp=1;}
if(e.getSource()==adds){Opp=1;notopp=2;}
if(e.getSource()==about){JOptionPane.showMessageDialog(null,"m4rtyr - indetectables.net");about.setVisible(false);}
if(e.getSource()==min){Opp=2;notopp=2;}
if(e.getSource()==mul){Opp=3;notopp=2;}
if(e.getSource()==over){Opp=4;notopp=2;}
if(e.getSource()==equ){if(notopp!=1){view.setText(String.valueOf(result));
Opp=0;}about.setVisible(true);}

if(notopp==1){
x*=10;
if(e.getSource()==b0){x+=0;setx(x);}
if(e.getSource()==b1){x+=1;setx(x);}
if(e.getSource()==b2){x+=2;setx(x);}
if(e.getSource()==b3){x+=3;setx(x);}
if(e.getSource()==b4){x+=4;setx(x);}
if(e.getSource()==b5){x+=5;setx(x);}
if(e.getSource()==b6){x+=6;setx(x);}
if(e.getSource()==b7){x+=7;setx(x);}
if(e.getSource()==b8){x+=8;setx(x);}
if(e.getSource()==b9){x+=9;setx(x);}
}
else{
y*=10;
if(e.getSource()==b0){y+=0;setx(y);op();}
if(e.getSource()==b1){y+=1;setx(y);op();}
if(e.getSource()==b2){y+=2;setx(y);op();}
if(e.getSource()==b3){y+=3;setx(y);op();}
if(e.getSource()==b4){y+=4;setx(y);op();}
if(e.getSource()==b5){y+=5;setx(y);op();}
if(e.getSource()==b6){y+=6;setx(y);op();}
if(e.getSource()==b7){y+=7;setx(y);op();}
if(e.getSource()==b8){y+=8;setx(y);op();}
if(e.getSource()==b9){y+=9;setx(y);op();}
}
}
public void setx(long x){view.setText(String.valueOf(x));}
public void op(){
if(Opp==1)result=x+y;
else if(Opp==2)result=x-y;
else if(Opp==3)result=x*y;
else if(Opp==4){try{result=x/y;}catch(Exception e){result=0;}}
x=result;y=0;
}


public static void main(String a[]){
JFrame n=new Calc();
n.setTitle("Calc");
n.setSize(320,140);
n.setLocationRelativeTo(null);
n.setResizable(false);
n.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
n.setVisible(true);
}

}

Re: Calculadora Java

Publicado: 12 Feb 2011, 04:18
por Turo
bonito code :D Y lo mejor del lenguaje java es que podemos utilizar JNI(Java Native Interface) utlizar librerias de C y C++.