Foro de elhacker.net

Programación => Java => Mensaje iniciado por: leliCabello en 12 Octubre 2009, 02:47 am



Título: operaciones aritmeticas en JAVA ECLIPSE
Publicado por: leliCabello en 12 Octubre 2009, 02:47 am
Hola...Porfavor ayudenme con esto, al escribir 2 numeros y hacer clic en sumar, tiene que salir la respuesta, al igual para restar, dividir y multiplicar....pero no me sale :-(...AYUDENMEEEE PLEASE....aqui les dejo mi avance
Código:
package prueba;
import javax.swing.*;

public class oper extends JFrame{
public oper(){
super();
}
private static final long serialVersionUID = 1L;
JPanel panel = new JPanel();
JLabel label = new JLabel("Ingresar primer número");
JTextField texto = new JTextField(10);
JLabel label2 = new JLabel("Ingresar segundo número");
JTextField texto2 = new JTextField(10);
JButton boton = new JButton ("Sumar");
JButton boton2 = new JButton ("Restar");
JButton boton3 = new JButton ("Multiplicar");
JButton boton4 = new JButton ("Dividir");
public void mipanel(){
add(panel);
panel.add(label);
panel.add(texto);
panel.add(label2);
panel.add(texto2);
panel.add(boton);
panel.add(boton2);
panel.add(boton3);
panel.add(boton4);
super.setTitle("Operaciones Aritméticas");
super.setVisible(true);
super.setSize(300, 170);

OyenteExternoAccion oyenteBotonUno = new OyenteExternoAccion(texto,texto2);
boton.addActionListener(oyenteBotonUno);

}
public static void main(String[] args) {
oper obj=new oper();
obj.mipanel();

}

}

Código:
package prueba;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class OyenteExternoAccion implements ActionListener{
String x;
String y;
int num1;
int num2;
int suma;
private JTextField texto;
private JTextField texto2;

public OyenteExternoAccion (JTextField texto, JTextField texto2)
{
this.texto = texto;
this.texto2 = texto2;

}

@Override
public void actionPerformed(ActionEvent evento) {
JButton boton=(JButton) evento.getSource();
x=texto.getText();
y=texto2.getText();
num1=Integer.parseInt(x);
num2=Integer.parseInt(y);
if (x.equals(num1)&& y.equals(num2)){
suma = suma + num1;}


}

}