BMI calculator gui java program.

Consider the task of writing a graphical program to compute a person’s body mass index (BMI). The program should have a way for the user to type in a height and a weight and should use these values to compute the person’s BMI. A reasonable appearance for the GUI would be the following window, which uses text fields for input and a button to trigger a computation of the BMI:

Let’s figure out how to create a GUI with the proper components and layout first and worry about event handling later. Since we want the program to display text fields with labels next to them, aligned in a row/column format, a 2×2 GridLayout is a good choice. But we’ll want to use a composite layout, because we don’t want the central label and the Compute button to have the same size and position as the grid squares. We’ll use a BorderLayout on our frame and add the central “Type your height and weight” label and Compute button directly to it. We’ll also create a panel with a 2×2 GridLayout and place it in the north region of the frame. The code that follows implements the initial BMI user interface. The program sets the frame’s title by passing it as a parameter to its constructor :


import java.awt.*;
import javax.swing.*;

public class BmiGui1
{
public static void main(String[] args) 
{
// set up components
JTextField heightField = new JTextField(5);
JTextField weightField = new JTextField(5);
JLabel bmiLabel = new JLabel("Type your height and weight");
JButton computeButton = new JButton("Compute");

// layout
JPanel north = new JPanel(new GridLayout(2, 2));
north.add(new JLabel("Height: "));
north.add(heightField);
north.add(new JLabel("Weight: "));
north.add(weightField);

// overall frame
JFrame frame = new JFrame("BMI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(north, BorderLayout.NORTH);
frame.add(bmiLabel, BorderLayout.CENTER);
frame.add(computeButton, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
 
}
}

Output :



bmi gui java,bmi calculator gui java program,bmi calculator gui,bmi rechner java gui

Post a Comment

1 Comments

Thanks,To visit this blog.