this my code

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Panel extends JPanel implements ActionListener{

    private JTextField weight,height, answer;
    private JButton calc, clear, exit;

    public Panel(){
        this.setLayout(new GridLayout(2,1));
        JPanel topPanel = new JPanel();
        topPanel.add(new JLabel("WEIGHT(KG)"));
        topPanel.add(weight = new JTextField(5));
        topPanel.add(new JLabel("HEIGHT(M)"));
        topPanel.add(height = new JTextField(5));
        topPanel.add(new JLabel("BMI"));
        topPanel.add(answer =new JTextField(5));
        JPanel bottomPanel = new JPanel();
        bottomPanel.add(calc = new JButton("CALCULATE"));
        bottomPanel.add(clear = new JButton("CLEAR"));
        bottomPanel.add(exit = new JButton("EXIT"));

        this.add(topPanel);
        this.add(bottomPanel);
        calc.addActionListener(this);
        clear.addActionListener(this);
        exit.addActionListener(this);
        answer.setEditable(false);
}

public void actionPerformed(ActionEvent e)
{

    double w =Double.parseDouble(weight.getText());
    double h =Double.parseDouble(height.getText());
    double ans=0;
    if(e.getSource()==calc)
    {
        ans = w/(h*h);
    }
    else if(e.getSource()==clear)
    {
        height.setText("");
        weight.setText(""); 
    }
    else if(e.getSource()==exit)
    {
        System.exit(0);
    }
    answer.setText(""+ans);

    if(ans>0&&ans<18.5)
    {
        answer.setBackground(Color.YELLOW);
    }
    else if(ans>18.5&&ans<25.0)
    {
        answer.setBackground(Color.GREEN);
    }
    else if(ans>=25.0)
    {
        answer.setBackground(Color.RED);
    }
}
}

I need to calculate BMI value and display in JTextField with 5 significant figures..what should i do?

Recommended Answers

All 2 Replies

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.