import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Main extends Applet implements ActionListener {
Label l1, l2,l3;
TextField t1,t2,t3;
Button b1,b2,b3,b4,b5,b6,b7,b8;
public Main () {
l1 = new Label("Numero 1");
t1 = new TextField(8);
l2 = new Label("Numero 2");
t2 = new TextField(8);
b1 = new Button("Suma");
b2 = new Button("Resta");
b3 = new Button("Multiplica");
b4 = new Button("Divide");
b5 = new Button("Raiz de 1");
b6 = new Button("Raiz de 2");
b7 = new Button("Mayor");
b8 = new Button("Limpia");
l3 = new Label("Resultado");
t3 = new TextField(8);
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(l3);
add(t3);
b1. addActionListener(this);
b2. addActionListener(this);
b3. addActionListener(this);
b4. addActionListener(this);
b5. addActionListener(this);
b6. addActionListener(this);
b7. addActionListener(this);
b8. addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b1) {
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int suma = 0;
suma = n1 + n2;
t3.setText("" + suma);
}
if (ae.getSource() == b2) {
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int resta = 0;
resta = n2 - n1;
t3.setText("" + resta);
}
if (ae.getSource() == b3) {
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int multi = 0;
multi = n1 * n2;
t3.setText("" + multi);
}
if (ae.getSource() == b4) {
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int div = 0;
div = n1 / n2;
t3.setText("" + div);
}
if (ae.getSource() == b5) {
int n1 = Integer.parseInt(t1.getText());
double raiz = 0;
raiz = Math.sqrt(n1);
t3.setText("" + raiz);
}
if (ae.getSource() == b6) {
int n2 = Integer.parseInt(t2.getText());
double raiz = 0;
raiz = Math.sqrt(n2);
t3.setText("" + raiz);
}
if (ae.getSource() == b7) {
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
int mayor = 0;
if (n1 > n2)
mayor = n1;
else
mayor = n2;
t3.setText("" + mayor);
}
if (ae.getSource() == b8) {
t1.setText("");
t2.setText("");
t3.setText("");
}
}
}
Resultado:
1 comentario:
hola amigo mira lo que pasa es que tengo mi codigo asi
package calculadora;
import java.awt.*;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Visual implements ActionListener {
Frame frame=new Frame ("Sergio Marin");
Panel panel=new Panel ();
Button buton1=new Button ("+ ");
Button buton2=new Button ("- ");
Button buton3=new Button ("* ");
Button buton4=new Button ("/ ");
Button buton5=new Button ("= ");
TextField campo=new TextField(10);
TextField campo2=new TextField(10);
TextField campo3=new TextField(10);
Label label=new Label("Calculadora By Sergio Marin");
public Visual (){
frame.setBackground(Color.CYAN);
frame.add(panel);
panel.add(buton1);
panel.add(buton2);
panel.add(buton3);
panel.add(buton4);
panel.add(buton5);
panel.add(campo);
panel.add(campo2);
panel.add(campo3);
campo.addActionListener(this);
campo2.addActionListener(this);
campo3.addActionListener(this);
panel.add(label);
frame.setSize(300,300);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}
Calculadora obj=new Calculadora ();
public void actionPerformed(ActionEvent e) {
int x=0, y=0;
if (e.getSource()==campo2){
x=Integer.parseInt(campo2.getText());
}
if (e.getSource()==campo3){
y=Integer.parseInt(campo3.getText());
}
if (e.getSource()==buton1){
campo.setText(""+x+y);
}
}
}
//y aki va la clase principal
package calculadora;
public class Principal {
public static void main(String[] args) {
Visual obj=new Visual();
}
}
//mi facebook es este
//http://www.facebook.com/zergiio.marin ojala me puedas ayudar
Publicar un comentario