Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,

Similar presentations


Presentation on theme: "Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,"— Presentation transcript:

1 Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame, JLabel, JTextField, and JButton work.  Become familiar with the concept of event- driven programming. Graphical User Interface (GUI) Components

2 Java Programming: From Problem Analysis to Program Design, Second Edition2  Discover events and event handlers.  Explore object-oriented design.  Learn how to identify objects, classes, and members of a class. Graphical User Interface (GUI) Components

3 Java Programming: From Problem Analysis to Program Design, Second Edition3 Graphical User Interface (GUI) Components  View inputs and outputs simultaneously.  One graphical window.  Input values in any order.  Change input values in window.  Click buttons to get output.

4 Java Programming: From Problem Analysis to Program Design, Second Edition4 Java GUI Components

5 Java Programming: From Problem Analysis to Program Design, Second Edition5 Graphical User Interface (GUI) Components  GUI components placed in content pane.  GUI components:  Windows  Labels  Text areas  Buttons

6 Java Programming: From Problem Analysis to Program Design, Second Edition6 GUI Components  Added to content pane of window.  Not added to window itself.  Pixel: A picture element.

7 Java Programming: From Problem Analysis to Program Design, Second Edition7 Windows  Can be created using a Frame object.  The class Frame provides various methods to control attributes of a window.  Measured in pixels of height and width.  Attributes associated with windows:  Title  Width  Height

8 Java Programming: From Problem Analysis to Program Design, Second Edition8 class JFrame  GUI window instance created as instance of Frame.  Provides various methods to control window attributes.

9 Java Programming: From Problem Analysis to Program Design, Second Edition9 Methods Provided by the class JFrame

10 Java Programming: From Problem Analysis to Program Design, Second Edition10 Methods Provided by the class JFrame

11 Java Programming: From Problem Analysis to Program Design, Second Edition11 Two Ways to Create a Window  First way:  Declare object of type JFrame.  Instantiate object.  Use various methods to manipulate window.  Second way:  Create class-containing application program by extending definition of class JFrame.  Utilize mechanism of inheritance.

12 Java Programming: From Problem Analysis to Program Design, Second Edition12 Content Pane  Inner area of GUI window (below title bar, inside border).  To access content pane:  Declare reference variable of type Container.  Use method getContentPane of class JFrame.

13 Java Programming: From Problem Analysis to Program Design, Second Edition13 Methods Provided by the class Container

14 Java Programming: From Problem Analysis to Program Design, Second Edition14 class JLabel  Labels: Objects of a particular class type.  class JLabel : Used to create labels.  Label attributes:  Title  Width  Height  To create a label:  Instantiate object of type JLabel.  Modify attributes to control display of labels.

15 Java Programming: From Problem Analysis to Program Design, Second Edition15 Methods Provided by the class JLabel

16 Java Programming: From Problem Analysis to Program Design, Second Edition16 Methods Provided by the class JLabel

17 Java Programming: From Problem Analysis to Program Design, Second Edition17 class JTextField  Text fields: Objects belonging to class JTextField.  To create a text field:  Declare reference variable of type JTextField.  Instantiate object.

18 Java Programming: From Problem Analysis to Program Design, Second Edition18 Methods Provided by the class JTextField

19 Java Programming: From Problem Analysis to Program Design, Second Edition19 class JButton  Provided to create buttons in Java.  To create a button:  Use the same technique that is used to create JLabel and JTextField.

20 Java Programming: From Problem Analysis to Program Design, Second Edition20 Methods Provided by the class JButton

21 Java Programming: From Problem Analysis to Program Design, Second Edition21 Methods Provided by the class JButton

22 Java Programming: From Problem Analysis to Program Design, Second Edition22 Handling an Event  Action event: An event created when JButton is clicked.  Event listener: An object that receives a message when JButton is clicked.  In Java, you must register the listener.

23 Java Programming: From Problem Analysis to Program Design, Second Edition23 Handling an Event  class ActionListener  Handles action event.  Part of package java.awt.Event.  The class ActionListener is a special type of class (interface).  Must contain the actionPerformed method.

24 Java Programming: From Problem Analysis to Program Design, Second Edition24 Rectangle Program: Sample Run

25 Java Programming: From Problem Analysis to Program Design, Second Edition25 Programming Example: Temperature Conversion  Input: Temperature in Fahrenheit or Celsius.  Output: Temperature in Celsius if input is Fahrenheit; temperature in Fahrenheit if input is Celsius.

26 Java Programming: From Problem Analysis to Program Design, Second Edition26 Programming Example: Temperature Conversion Solution: 1.Create the appropriate JLabels, JTextFields, JButtons. 2.Add them to the created content pane. 3.Calculate the appropriate conversions when the buttons are clicked and an event is triggered.

27 Java Programming: From Problem Analysis to Program Design, Second Edition27 Sample Run for TempConversion

28 Java Programming: From Problem Analysis to Program Design, Second Edition28 Object-Oriented Design Simplified methodology: 1.Write down detailed description of problem. 2.Identify all (relevant) nouns and verbs. 3.From list of nouns, select objects. 4.Identify data components of each object. 5.From list of verbs, select operations.

29 Java Programming: From Problem Analysis to Program Design, Second Edition29  Every GUI contains a window.  Various components are added to the content pane of a window.  class Frame is used to create windows.  JLabel is used to label GUI components and display information to user.  JTextFiled is used for input/output.  JButton generates action event.  Action event is sent to action listener.  Action listener must have method called actionPerformed. Summary


Download ppt "Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,"

Similar presentations


Ads by Google