Question: Complete the TODO sections commented: import java.awt.FlowLayout; import java.awt.Font; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JTextField;
Complete the TODO sections commented:
import java.awt.FlowLayout; import java.awt.Font;
import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent;
import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.JTextArea; import javax.swing.JOptionPane; import javax.swing.JScrollPane;
// TODO -- create BodyMassIndex class (separate file)
public class BodyMassIndexGUI extends JFrame implements ActionListener {
// TODO -- declare class instance variables
// TODO -- declare maximum persons constant // TODO -- declare BodyMassIndex object array // TODO -- declare current person variable
private JLabel titleLabel = new JLabel("CQUniversity BMI Management System"); // program title private JLabel nameLabel = new JLabel("Person name: ");// for prompt private JTextField nameField = new JTextField(26); // for name entry
private JLabel heightLabel = new JLabel("Person height (cm): ");// for prompt private JTextField heightField = new JTextField(4); // for height entry
private JLabel weightLabel = new JLabel("Person weight (kg): ");// for prompt private JTextField weightField = new JTextField(4); // for weight entry
private JTextArea displayTextArea = new JTextArea("", 15, 56); // declare text area private JScrollPane scrollPane; // scroll pane for the text area
// declare all of the buttons private JButton enterButton = new JButton("Enter"); // buttons private JButton displayButton = new JButton("Display All"); private JButton searchButton = new JButton("Search"); private JButton exitButton = new JButton("Exit");
public BodyMassIndexGUI() { // constructor create the Gui this.setLayout(new FlowLayout()); // set JFrame to FlowLayout
titleLabel.setFont(new Font("Ariel", Font.BOLD, 24)); add(titleLabel); add(nameLabel); add(nameField); add(heightLabel); add(heightField); add(weightLabel); add(weightField);
// set text area to a monospaced font so the columns can be aligned using a format string displayTextArea.setFont(new Font("Monospaced", Font.PLAIN, 12)); displayTextArea.setEditable(false); // make text area read only scrollPane = new JScrollPane(displayTextArea); // add text area to the scroll pane scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); // just need vertical scrolling
add(scrollPane);
add(enterButton); add(displayButton); add(searchButton); add(exitButton);
enterButton.addActionListener(this); // add the action listener to the buttons displayButton.addActionListener(this); searchButton.addActionListener(this); exitButton.addActionListener(this);
// when the user pushes the system close (X top right corner) addWindowListener( // override window closing method new WindowAdapter() { public void windowClosing(WindowEvent e) { exit(); // Attempt to exit application } } ); } // BodyMassIndexGUI
public void actionPerformed(ActionEvent e) { // process the clicks on all of the buttons String command = e.getActionCommand();
if (command.compareTo("Enter") == 0) enterPersonNameHeightAndWeight(); else if (command.compareTo("Display All") == 0) displayAll(); else if (command.compareTo("Search") == 0) search(); else if (command.compareTo("Exit") == 0) exit(); } // actionPerformed
private void enterPersonNameHeightAndWeight() {
// TODO -- Read in person name, height and weight information and add to the BodyMassIndex array
displayHeading();
// TODO -- Display person name, height, weight, BMI and rating to the text area
// TODO -- Clear input fields and return focus to the person name field and increment current person variable
// TODO -- complete error message code (put this code at the beginning of the method)
} // enterPersonNameHeightAndWeight
private void displayHeading() { displayTextArea.setText(String.format("%-21s%-9s%-9s%-4s%12s ", "Person Name", "Height", "Weight", "BMI", "Rating")); appendLine(); } // displayHeading
private void appendLine() { displayTextArea.append("------------------------------------------------------- "); } // appendLine
private void displayAll() { displayHeading();
// TODO -- display all of the entries entered so far
// TODO -- display average BMI
// TODO -- complete error message code } // displayAll
private void search() {
// TODO -- read search key (person name) from input dialog
// TODO -- iterate through array to search for the search key
// TODO -- display the entry if it exists or an error message if it doesn't
// TODO -- complete error message code } // search
private void exit() { // TODO -- display exit message here
System.exit(0); } // exit
// Main method create instance of class public static void main(String[] args) { BodyMassIndexGUI f = new BodyMassIndexGUI(); // Create instance of class f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // allow the code to close the program f.setBounds(200, 100, 480, 425); // Define position and size of app f.setTitle("BMI application"); // Set the title of the app f.setVisible(true); // Make the application visible } // main
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
