Problem when holding a button event in Java Swing

Asked

Viewed 83 times

0

I’m having a problem doing an event on a button (using textfield to set values) and calling a method to calculate BMI (I haven’t finished this method yet).

Code: (java screen.)

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);

}
}   
  • What’s the doubt? "I’m having a problem" doesn’t say much about what you’re trying to do and has difficulties.

  • I talked about doing an event on the button, by clicking on the set button the values placed on the textfield for my set’s and calling a method (which I haven’t finished yet.)

  • I think your approach is not very good, may I suggest a better approach?

  • can yes bro, it is a college job, I tried to do for an example that the teacher passed me and unfortunately I am not getting.

  • 1

    Gabriel, see the answer below.

1 answer

1


If the goal is to pass the captured values in the text fields by clicking the button, there is no need to create an array of type IMC. I would suggest that you create a constructor that receives the necessary values for the calculation, more or less in this way:

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 this, start as class field Tela:

public class Tela {

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

}

and stand inside 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 set where the result will appear on your screen and invoke the method you calculate.

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

It is essential to follow these tips in order to improve your codes and make them more readable.

  • Opa this part of dispatching to application Event-Dispatch I gave a read but I did not understand much, and I was also a doubt with respect to composition, have that is inside the screen builder? Because I threw it out and gave it a problem: this.imc = new BMI(name, weight, height);

  • @Gabrielcosta as I explained in the answer, you need to start within the Canvas class, either as a field or as a local variable. If you keep doing what you were doing, it won’t work.

  • @Gabrielcosta as to dispatch, you ALWAYS start the screen as shown in the yellow frame of this reply: https://answall.com/a/193456/28595

  • Yes, actually I started inside the screen class, but inside the public screen constructor(){}, bro I was thinking about creating a new screen name c class 2 and set as Visible when I click the button to show the result, is that a good one? And at this button event I can call my method that calculate the BMI? ( but I believe that if I do as I said, it is not necessary to call the method in the Calculate button).

  • 1

    @Gabrielcosta, why start the BMI class without values? What is the need? To calculate, you will need, necessarily, the 3 values, so, nothing more coherent than instantiating the class with the 3 values. Codes need to have logical sense with what they represent, initiating BMI without values makes no sense at all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.