Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.

Similar presentations


Presentation on theme: "1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components."— Presentation transcript:

1 1 Class 8

2 2 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

3 3 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

4 4 The Body Mass Index Calculator Create a requirements document

5 5

6 6 Problem Analysis Convert user input to metric measurements Calculate the BMI Display the result

7 7 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

8 8

9 9

10 10

11 11 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??

12 12 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

13 13 import javax.swing.*; import java.awt.*; import java.awt.event.*; // add the class header public class BodyMass extends JFrame { }

14 14 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; }

15 15 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);

16 16 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”);

17 17 // 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);

18 18 setTitle("THE SUN FITNESS” + “ CENTER BODY MASS INDEX"); setSize(400,300); setVisible(true); }

19 19 Add the appropriate variable declarations and calculation statements to the Java application where they are needed; in this case, to the button event logic.

20 20 // 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 + “.”); }

21 21 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()

22 22 // 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

23 23 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

24 24 Conclusion of Class 8


Download ppt "1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components."

Similar presentations


Ads by Google