GUI Java中的BMI计算器;

GUI Java中的BMI计算器;,java,swing,user-interface,layout,Java,Swing,User Interface,Layout,我想用Java中的GUI创建一个BMI计算器。我对GUI甚至Java都很陌生。假设计算器显示BMI,并附有建议,甚至还有时间和日期。但是,只有BMI显示,其余的不能显示。。我一直在网上搜索如何在线显示if-else条件的结果,但没有结果。这是我的密码 public class BMI extends JFrame implements ActionListener { private static final JButton JButton = null; private JF

我想用Java中的GUI创建一个BMI计算器。我对GUI甚至Java都很陌生。假设计算器显示BMI,并附有建议,甚至还有时间和日期。但是,只有BMI显示,其余的不能显示。。我一直在网上搜索如何在线显示if-else条件的结果,但没有结果。这是我的密码

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 HAVE@! 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

    }
}
公共类BMI扩展JFrame实现ActionListener{
私有静态最终JButton JButton=null;
私有JFrame;
私人JPanel小组;
私人JLabel高度标签、重量标签、体重标签;
私人JTextField身高、体重、成绩;
私有按钮计算;
绳子的高度、重量;
双倍数字1,数字2,体重指数;
静态字符串输出=“结果”;
静态int-jopIcon=JOptionPane.QUESTION\u消息;
布尔bFlag=true;//状态,true表示无异常
公共BMI(){
frame=新JFrame(“BMI计算器”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//为高度和重量文本字段创建标签
heightLabel=新的JLabel(“您的身高以米为单位:”);
weightLabel=新的JLabel(“您的体重单位:千克”);
//创建一个“这是你的体重指数”标签
BMILabel=新的JLabel(“您的BMI为”);
//创建一个结果标签以保存BMI值
结果=新的JTextField(“”);
//创建一个JTextField来保存人的身高(以千克为单位)
高度=新的JTextField(1);
//创建一个JTextField以容纳人的体重(以米为单位)
权重=新的JTextField(1);
计算=新按钮(“计算体重指数”);
//将JPanel设置为在JFrame上运行
panel=新的JPanel();
面板。添加(高度标签);
面板。添加(高度);
//将重量标签和重量文本字段添加到面板
面板。添加(重量标签);
面板。添加(重量);
//将按钮添加到面板
面板。添加(标签);
//将保存结果的标签添加到面板
添加(结果);
//将面板添加到框架中
面板。添加(计算);
//将BMI标签添加到面板
frame.getContentPane().add(面板);
JPanel p1=新的JPanel();
panel.setLayout(新网格布局(4,1));
新增(p1,南部边界布局);
calculate.addActionListener(这个);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架。设置尺寸(400400);
frame.setVisible(true);//重要信息必须有@!否则将不显示GUI
}
公共字符串getDateTime(){
DateFormat DateFormat=新的简化格式(“yyyy/MM/dd HH:MM:ss”);
日期=新日期();
返回日期格式。格式(日期);
}
public void calculateBMI(双倍数字1,双倍数字2){
试一试{
BMI=数字2/((数字1)*2);
}捕获(NumberFormatException nfe){
输出+=“\n\n哇!输入错误:必须输入有效整数”;//若出现异常,请准备错误消息
jopIcon=JOptionPane.ERROR\u消息;
}
}
公共空间计算(){
Height=Height.getText();
Weight=Weight.getText();//用Jtext Height声明高度字符串
试一试{
number1=Double.parseDouble(高度);
number2=Double.parseDouble(Weight);//可能会出现异常
calculateBMI(数字1,数字2);
}最后{
如果(体重指数>=27.5){
输出+=“\n\n您处于高风险区(不健康)。请开始减肥!这是必须的!”;

}否则如果(BMI我会这样做:

@Override
public void actionPerformed(ActionEvent arg0) {
    calculate();
    result.setText(output);
}
结果应为JTextArea类型,但应设置换行:

result = new JTextArea();
result.setLineWrap(true);
您应该考虑在匿名类中实现ActionListener,而不是直接在frame类中实现(特别是当您希望添加一些其他按钮或复选框时)


如果你想寻求免费帮助,那么要求你至少发布格式良好的代码并不过分。解决方案是简单地添加一个组件,该组件将显示你想在GUI上显示的数据,就像你用来显示体重、身高和结果一样。你已经知道如何添加这些JTextField组件,所以它可以对于你来说,添加更多的JTrdFieldor或JCLAP来显示附加信息应该是微不足道的,不是吗?我会先读一读,然后也会考虑找到你喜欢的布局的例子并尝试复制它们。layouts@HovercraftFullOfEels我接受世系命名约定:P@MaD程序员:阿门!但一次只能提出一个要求。:)谢谢!这很有帮助。。最终我的if-else状态可以显示出来。因此,实际上这与我获取BMI的方式相同。。我现在明白了。:D
calculate.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent ev) {
        calculate();
        result.setText(output);
    }
});