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.
– user28595
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.)
– Aegis Dev
I think your approach is not very good, may I suggest a better approach?
– user28595
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.
– Aegis Dev
Gabriel, see the answer below.
– user28595