Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

¿Como recibo un entero de una 'jTextField'? en java-eclipse?

Soy principiante en java y estoy haciendo un programa que mediante un boton, tres campos de texto y tres etiquetas, me sume dos numeros, me sale un error en el main, supongo que es aqui el error

public void actionPerformed(ActionEvent evt) {

System.out.println("jButton1.actionPerformed, event="+evt);

int numero1=0;

int numero2=0;

numero1=Integer.parseInt(this.jTextField1.getText());

numero2=Integer.parseInt(this.jTextField2.getText());

int resultado=numero1+numero2;

jTextField3.setText(String.valueOf(resultado));

1 Answer

Rating
  • Amos
    Lv 6
    10 years ago
    Favorite Answer

    bueno no entendo mucho tu error, pero te voy a pasar este

    import java.awt.event.*;

    import javax.swing.*;

    public class yahoo extends JFrame implements ActionListener{

    private JLabel label1, label2, label3;

    private JTextField tf1, tf2, res;

    private JButton boton;

    public yahoo(){

    super("Suma");

    setLayout(null);

    label1 = new JLabel("primer numero");

    label1.setBounds(10, 10, 100, 30);

    add(label1);

    label2 = new JLabel("segundo numero");

    label2.setBounds(10, 70, 100, 30);

    add(label2);

    label3 = new JLabel("Resultado");

    label3.setBounds(290, 18, 100, 30);

    add(label3);

    res = new JTextField();

    res.setBounds(270, 40, 100, 30);

    add(res);

    tf1 = new JTextField();

    tf1.setBounds(120,10,100,30);

    add(tf1);

    tf2 = new JTextField();

    tf2.setBounds(120,70,100,30);

    add(tf2);

    boton = new JButton("Sumar");

    boton.setBounds(20,150,100,30);

    boton.addActionListener(this);

    add(boton);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public void actionPerformed(ActionEvent e) {

    if(e.getSource() == boton){

    String cad = tf1.getText();

    int num1 = Integer.parseInt(tf1.getText());

    int num2 = Integer.parseInt(tf2.getText());

    int resultado = num1+num2;

    res.setText(String.valueOf(resultado));

    }

    }

    }

    ************************************************************************************************************************************************************

    public class prueba {

    public static void main(String[] args) {

    yahoo s = new yahoo();

    s.setBounds(0,0,400,250);

    s.setVisible(true);

    }

    }

    si tienes duda o no se ve completo el codigo alli escribeme

    amos.1790@yahoo.com

Still have questions? Get your answers by asking now.