Class 1 ~ Chapter 1

Download Report

Transcript Class 1 ~ Chapter 1

Class 8
1
Chapter Objectives
Use Swing components to build the
GUI for a Swing program
 Implement an ActionListener to
handle events
 Add interface components to a
JFrame

2
The Body Mass Index Calculator

An interactive program
– Accepts the weight and height from the
user
– Calculates the BMI to gauge total body
fat
– Displays the result
3
The Body Mass Index Calculator

Create a requirements document
4
5
Problem Analysis

Convert user input to metric
measurements
 Calculate the BMI
 Display the result
6
Design the Solution
Design the user interface with
storyboards
 Design the logic of the program
– Use pseudocode for sequential
flow for all programs
 Validate the design
– Compare the program design with
the original requirements

7
8
9
10
Coding the Console application
/* Chapter 3: Body Mass Index Calculator
Programmer: J. Starks
Date: Sept. 10, 2007
Purpose: This window application calculates the
body mass index based on user input
from text fields. */
What import statements are needed??
11
Import Packages

Use the import statement to access
classes in the SDK
– The java.lang package is
automatically imported
– Place the import statement before
the class header
– Use an asterisk (*) after the
package name and period delimiter
to import all necessary classes in
the package
12
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// add the class header
public class BodyMass extends JFrame {
}
13
Create the user interface
Declare the gui components in the class’s
declaration section
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BodyMass extends JFrame {
private JLabel heightLabel, weightLabel,
outputLabel;
private JTextField heightField, weightField;
private JButton calcButton;
14
Code the constructor that creates and adds
the gui components to the user interface
public BodyMass()
{Container myWindow = getContentPane();
myWindow.setLayout(new FlowLayout());
myWindow. setBackground(Color.PINK);
15
heightLabel = new JLabel (
“Enter your height “ + “to the nearest inch”);
weightLabel = new JLabel (
“Enter your weight” + “to the nearest pound”);
outputLabel = new JLabel(
“Click the Calculate” +
“ button to see your body mass index.”);
heightField = new JTextField(10);
weightField = new JTextField(10);
calcButton = new JButton (“Calculate”);
16
// add the components to the content pane
myWindow.add(heightLabel);
myWndow.add(heightField);
myWindow.add(weightLabel);
myWindow.add(weightField);
myWindow.add(calcButton);
// add all the event handling for the click of the button
calcButton.addActionListener( new ActionListener ()
{public void actionPerformed(ActionEvent e)
{calculateActionPerformed(e); }
}
);
myWindow.add(outputLabel);
17
setTitle("THE SUN FITNESS” +
“ CENTER BODY MASS INDEX");
setSize(400,300);
setVisible(true);
}
18
Add the appropriate variable declarations and
calculation statements to the Java application
where they are needed; in this case, to the button
event logic.
19
// write the calculateActionPerformed method that
// gets the data from the text fields, performs the proper
// calculations and displays the results
public void calculateActionPerformed(ActionEvent e)
{ private int height;
private int weight;
private double meters;
private double kilograms;
private double index;
height =Integer.parseInt(heightField.getText());
weight = Integer.parseInt(weightField.getText());
meters = height / 39.36;
kilograms = weight / 2.2;
index = kilograms/ (meters * meters);
outputLabel.setText(“YOUR BODY MASS INDEX”
+ “ IS ” + index + “.”);
}
20
The actionPerformed() Method

When a click event occurs, the
ActionListener’s actionPerformed() method
is triggered
– Input values are retrieved with getText()
– Calculations are performed
– Output is sent to a label with setText()
21
21
// add your main method
public static void main(String a[])
{ BodyMass bmass = new BodyMass();
bmass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} // end of class Body Mass
22
Compiling, Running, and
Documenting the Application
Compile the Body Mass Index
Calculator program
 Execute the program
 Test the program by entering the
sample input data supplied in the
requirements phase at the prompts
 Verify the results
 Print the source code and screen
images for documentation

23
Conclusion
of Class 8
24