2013-02-10 148 views
-2

我想用Java中的GUI創建一個BMI計算器。我對GUI甚至Java都很陌生。計算器假設顯示BMI的建議,甚至時間和日期。然而,只有BMI顯示,其餘不能。..我一直在網上搜索如何顯示結果從其他條件在線,但無濟於事。這是我的代碼;GUI中的BMI計算器;

public class BMI extends JFrame implements ActionListener { 

    private static final JButton JButton = null; 
    private JFrame frame; 
    private JPanel panel; 
    private JLabel heightLabel, weightLabel, BMILabel; 
    private JTextField height, weight, result; 
    private JButton calculate; 
    String Height, Weight; 
    double number1, number2, BMI; 
    static String output = "Results"; 
    static int jopIcon = JOptionPane.QUESTION_MESSAGE; 
    boolean bFlag = true; //state, true means no exception 

    public BMI() { 

     frame = new JFrame("BMI Calculator"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
//create labels for the height and weight textfields 
     heightLabel = new JLabel("Your height in meters:"); 
     weightLabel = new JLabel("Your weight in kilograms: "); 
//create a "this is your BMI" label 
     BMILabel = new JLabel("Your BMI is "); 
//create a result label to hold the BMI value 
     result = new JTextField(""); 
//create a JTextField to hold the person's height in kilograms 
     height = new JTextField(1); 
//create a JTextField to hold the person's weight in metres 
     weight = new JTextField(1); 


     calculate = new JButton("Calculate BMI"); 

//set up the JPanel to go on the JFrame 
     panel = new JPanel(); 


     panel.add(heightLabel); 
     panel.add(height); 
//add the weight label and weight textfield to the panel 
     panel.add(weightLabel); 
     panel.add(weight); 
//add the button to the panel 

     panel.add(BMILabel); 
//add the label that holds the result to the panel 
     panel.add(result); 
//add the panel to the frame 
     panel.add(calculate); 
//add the BMI label to the panel 



     frame.getContentPane().add(panel); 

     JPanel p1 = new JPanel(); 
     panel.setLayout(new GridLayout(4, 1)); 
     add(p1, BorderLayout.SOUTH); 




     calculate.addActionListener(this); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(400, 400); 
     frame.setVisible(true);//important must [email protected]! if not GUI will not be display 

    } 

    public String getDateTime() { 
     DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
     Date date = new Date(); 
     return dateFormat.format(date); 
    } 

    public void calculateBMI(double number1, double number2) { 
     try { 
      BMI = number2/((number1) * 2); 

     } catch (NumberFormatException nfe) { 
      output += "\n\n Whoa! Input error: must enter valid integers";//if exception comes out, prepare error message 
      jopIcon = JOptionPane.ERROR_MESSAGE; 
     } 
    } 

    public void calculate() { 
     Height = height.getText(); 
     Weight = weight.getText();//declare the Height string with Jtext height 

     try { 
      number1 = Double.parseDouble(Height); 
      number2 = Double.parseDouble(Weight);//exception may come out 

      calculateBMI(number1, number2); 



     } finally { 

      if (BMI >= 27.5) { 

       output += "\n\n You're in the High Risk zone(UnHealthy). Please start losing weight! It's a MUST!"; 

      } else if (BMI <= 23 || BMI < 27.4) { 

       output += "\n\n You're in the Moderate Risk zone. Please start going on diet and lose some weight"; 

      } else if (BMI <= 18.5 || BMI < 22.9) { 
       output += " You're in the Low Risk zone(Healthy). Hopefully you can maintain this way! ^^"; 

      } else if (BMI < 18.4) { 
       output += "\n\n You really need to start eating more. Too skinny and unhealthy for your body"; 

      } 
     } 

    } 

    public static void main(String[] args) { 




     BMI bmi = new BMI(); 



    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
//call the calculate 

     this.calculate(); 

     result.setText("" + BMI); 

// TODO Auto-generated method stub 

    } 
} 
+4

如果您要求免費幫助,請至少嘗試發佈格式良好的代碼並不多。 – 2013-02-10 05:19:00

+1

該解決方案僅僅是添加一個組件,該組件將顯示您想要顯示給GUI的數據*,就像您用來顯示重量,高度和結果*一樣。您已經知道如何添加這些JTextField組件,因此添加更多JTextFields或JLabel以顯示附加信息應該很簡單,不是嗎? – 2013-02-10 05:21:16

+0

我沒有看到你試圖解決問題的地方,所以你來這裏可能還爲時過早。爲什麼不首先嚐試顯示附加信息,並且只有在您的嘗試不起作用時纔會嘗試進行嘗試? – 2013-02-10 05:22:25

回答

2

我會做這樣的:

@Override 
public void actionPerformed(ActionEvent arg0) { 
    calculate(); 
    result.setText(output); 
} 

結果應該是類型的JTextArea但是換行應設置:

result = new JTextArea(); 
result.setLineWrap(true); 

而且你應該考慮實施的ActionListener在一個匿名類而不是直接在框架類(特別是當你想添加一些其他按鈕或複選框)

calculate.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent ev) { 
     calculate(); 
     result.setText(output); 
    } 
}); 
+0

謝謝!這有助於..最後我的if else條件可以顯示。所以它的實際情況與我如何得到體重指數一樣。我現在明白..:D – Khair 2013-02-10 05:48:19