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