0

I have build a program that calculates the Body Mass Index. The method that does the calculation is bmi_calculator(); in the class BMI_Calculator. I have build also a GUI but without drag and drop the elements from the palette, but I have written the code by myself. The GUI is in class BMI_GUI

The user writes the weight and length, then clicks into the button Calculate and the result must be shown in the Text Field (jresult) there. But it doesn't work. This is my code:

public class BMI_Calculator extends BMI_GUI{
  public void bmi_calculator(){
    try{
    //Text aus dem Textfeld lesen
    String sgewicht= getGewicht().getText();
    String skorpergrosse= getKorpergrosse().getText();
    String salter= getAlter().getText();

    //String in Double umwandeln
    Double gewicht = Double.valueOf(sgewicht);
    Double korpergrosse = Double.valueOf(skorpergrosse);
    Double alter = Double.valueOf(salter);
    //Rechnen Formula
    Double result=gewicht/(korpergrosse*korpergrosse);
    //Ergebniss in String umwandeln
    String sresult= result.toString();
    // Ergbnis in das Textfeld Calculate schreiben
    getResult().setText(sresult); 

    }
   catch(Exception e){

        JOptionPane.showMessageDialog(null, "Das ist ein Error");
    }
}
public void jButtonActionPerformed(){
  if (jButton.getModel().isPressed()) {
           bmi_calculator();
    }  
}
}

The another class is:

public class BMI_GUI extends JFrame {

JPanel jpMain,jpFooter;
JLabel jTitle,jGewicht, jKorpergrosse, jAlter;
private JTextField jgew;
private JTextField jkor;
private JTextField jalt;
private JTextField jresult;
 JButton jButton;
 JButton jButton2;
 JButton jButton3;

 public BMI_GUI(){
         init();
 }
   private void init(){
    //Fenstergrosse bestimmen
    this.setSize(500,400);
    //fenster in der mitte darstellen
    this.setLocationRelativeTo(null);
    //fenster schliessen mit X
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    // fenster title festlegen
    this.setTitle("Body Mass Index");
    //verhinder dass benutzer fenster grosser kleiner machen kann
    this.setResizable(false);

    jpMain=new JPanel();
    jpMain.setLayout(null);

    //than comes the code per JLabels and JTextFields for weight,
    // height and age

    jButton=new JButton("Calculate");
    jButton.setLocation(130,210);
    jButton.setSize(110,20);

    jresult=new JTextField();
    jresult.setSize(60,20);
    jresult.setLocation(270,210);

    //Komponenten dem Panel hinzufugen
     jpMain.add(jTitle); 
     jpMain.add(jGewicht); 
     jpMain.add(jKorpergrosse);
     jpMain.add(jAlter);
     jpMain.add(jgew);
     jpMain.add(jkor);
     jpMain.add(jalt);
     jpMain.add(jresult);
     jpMain.add(jButton);
     jpMain.add(jButton2);
     jpMain.add(jButton3);


    //panels zum fenster hinzufugen
    this.add(jpMain);
    //this.add(jpFooter);

}
   //Set- und GET Methode

    public JTextField getGewicht(){
    return this.jgew;
}
    public void setGewicht(JTextField jgew){
    this.jgew=jgew;
}
    public JTextField getKorpergrosse(){
    return this.jkor;
}
    public void setKorpergrosse(JTextField jkor){
    this.jkor=jkor;
}
    public JTextField getAlter(){
    return this.jalt;
}
    public void setAlter(JTextField jalt){
    this.jalt=jalt;
}
    public JTextField getResult(){
    return this.jresult;
}
    public void setResult(JTextField jresult){
    this.jresult=jresult;
}

      public static void main(String [] args){
            new BMI_GUI().setVisible(true);

}


}
Iris
  • 23
  • 8
  • 1
    what precisely does not work? does your program compile? does your program generate any exceptions? does your program calculate the wrong value? what specifically is your problem? – Dan O Oct 28 '15 at 19:01
  • 1
    Don't use a null layout!!! Swing was designed to be used with layout managers. – camickr Oct 28 '15 at 19:13
  • Dan it doesn't brings any exceptions, the code compiles but when I click the Calculate button it doesn't happen anything – Iris Oct 28 '15 at 19:15

1 Answers1

1

you do not register an ActionListener on your JButton.

jButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent ae) {
    System.out.println("guten tag");
  }
});

(you do not need to examine the JButton model to check if the button is clicked. If the button is clicked, this method will be called.)

see also this link: How do you add an ActionListener onto a JButton in Java

Community
  • 1
  • 1
Dan O
  • 6,022
  • 2
  • 32
  • 50
  • I also tried this, instead of System.out I wrote the method name the bmi_calcultor(); and it brings and error: Create a method bmi_calculator() in because this method must be executed – Iris Oct 28 '15 at 19:13
  • With System.out.println("guten tag"); it works but I want to call the method instead of System.out – Iris Oct 28 '15 at 19:22