Problem when performing a button event in Java Swing

0

I have a problem making an event on a button (using textfield to set values) and calling a method to calculate the IMC (I have not yet finished this method).

Code: (screen.java)

package calculadoraimc;
import javax.swing.*;
import java.awt.*;
 //evento para button
import java.awt.event.*; //evento para button

public class tela extends JFrame 
{
    private IMC[] i;
    private JLabel lb1,lb2,lb3;
    private JTextField tf1,tf2,tf3;
    private JButton bt1;
    public tela(){
        setTitle("Calculadora IMC");
        setLayout(new FlowLayout());
        setSize(600,600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        lb1 = new JLabel("Nome");
        tf1 = new JTextField(20);
        lb2 = new JLabel("Peso");
        tf2 = new JTextField(20);
        lb3 = new JLabel("Altura");
        tf3 = new JTextField(20);
        bt1 = new JButton ("Calcular");
        bt1.addActionListener(new ActionListener(){public void actionPerformed (ActionEvent ev){bConf();}});


        add(lb1);
        add(tf1);   
        add(lb2);
        add(tf2);
        add(lb3);
        add(tf3);
        add(bt1);
    }

    public void bConf() {
    i[Integer.parseInt(tf2.getText())].setNome(tf2.getText());// converter string para inteiro e adicionar em cliente e converte string em inteiro e adicionar em Nome
    i[Integer.parseInt(tf1.getText())].setPeso(Double.parseDouble(tf1.getText())); // converter string para inteiro e adicionar em cliente e converte string em inteiro e adicionar em codigo
    tf1.setText(" "); // limpar campos
    tf2.setText(" ");  
    }
}

IMC.java

package calculadoraimc;

public class IMC{
private String nome;
private double altura,peso,imc;

public void setNome(String n){
this.nome=n;
}
public String getNome(){
return this.nome;
}
public void setAltura(double a){
this.altura=a;
}
public double getAltura(){
return this.altura;
}
public void setPeso(double p){
this.peso=p;
}
public double getpeso(){
return this.peso;
}

public void calcularIMC(){
this.imc=this.peso/(this.altura*this.altura);
if (imc < 16.0){

            }
            if (16< imc && imc <16.99){

            }
            if (17<imc && imc <18.49){


            }
            if (18.50< imc && imc < 24.99){

            }
            if(25<imc && imc <29.99){

            }
            if(30<imc && imc <34.99){

            }
            if(35<imc && imc <39.99){

            }
            if(imc>=40){

            }
}


}

main:

package calculadoraimc;

import java.util.Scanner;

public class programa {
 public static void main(String[] args){
  tela t = new tela();
  IMC i = new IMC();
  t.setVisible(true);

}
}   
    
asked by anonymous 21.11.2018 / 11:06

1 answer

1

If the goal is to pass captured values into text fields when you click the button, there is no need to create an array of type IMC . I would suggest that you create a constructor that gets the values needed for the calculation, more or less like this:

public class IMC {

//seus campos...

    public IMC(String nome, double peso, double altura) {

        this.nome = nome;
        this.altura = altura;
        this.peso = peso;

    }

//restante da classe...

}

And instead of starting in main, which is also completely unnecessary since the scope of the IMC class does not need to be part of it, start as field of class Tela :

public class Tela {

//...
private IMC imc;
//...

}

and stay within the button event, passing the values:

bt1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ev) {
        String nome = tf1.getText();
        double peso = Double.parseDouble(tf2.getText());
        double altura = Double.parseDouble(tf3.getText()); 

        this.imc = new IMC(nome, peso, altura);

        tf1.setText(" ");
        tf2.setText(" ");
    }
});

Then just define where the result will appear on your screen and invoke the method that calculates.

However, your code has a lot of problems worth noting below:

  • You are not forwarding your application to the

21.11.2018 / 11:38