Chapter 6 Graphical User Interface GUI and ObjectOriented
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Chapter Objectives Learn about classes used to build simple GUIs Ø Learn to use GUI components Frame, Label, Textile, and JButton Ø Become familiar with the concept of event-driven programming, events and event handlers Ø Explore object-oriented design using GUI as an one example Ø Learn how to identify objects, classes, and members of a class Ø
Graphical User Interface (GUI) Components We have already met JOption. Pane from the swing library. It allowed us to use single variable text input boxes and information boxes Ø Now we will look at the basic components used to build User interfaces which allow us to: Ø view inputs and outputs simultaneously in a single window ] To Input values of inputs in any order ] To change input values in window ] To click on buttons to initiate an action to produce an output ]
Java GUI Components
Graphical User Interface (GUI) Components Ø GUI components Windows ] Labels ] Text areas ] Buttons ] A Window has a title, a content pane, additional GUI components can be placed on the content pane Ø Labels indicate what the contents of a text box is, what the purpose of a button is, etc. Ø A text area can be used to display and/or input data Ø Pushing a button is associated with an action or event Ø
GUI Creation Ø First create a Window with a label indicating the purpose of the GUI. You can specify the size of the window in pixels. Ø Access the content pane of the window and choose the layout of the components Ø Add the desired GUI components (labels and text areas) Ø Add any buttons and handle the events associated with those buttons
Creating a Window Ø Create a Window by declaring an instance of a JFrame object, by using a constructor. Ø The class JFrame provides various methods to control attributes of a window Ø Attributes associated with window objects ] Title ] Width (measured in pixels) ] Height (measured in pixels)
Methods Provided by the class Frame
Methods Provided by the class Frame
Two Ways to Create a Window Ø First way ] Declare object of type JFrame ] Instantiate object ] Use various methods to manipulate window Ø Second way ] Create subclass of JFrame containing application program by extending definition of class JFrame is a superclass of the new subclass ] Utilizes mechanism of inheritance
Content Pane Ø Inner area of GUI window (below title bar, inside border) Ø To access content pane: ] Declare reference variable of type Container ] Use method get. Content. Pane of class JFrame
Methods Provided by the class Container
class JLabel Ø Class to create and modify JLabel objects (labels) A label may be aligned left, center or right ] A label may include text and/or an icon ] Ø To create a label ] Declare and Instantiate object of type JLabel May modify attributes to control display of labels Ø To add a label to a container (display the label in the content pane) use the method add from the container class Ø
Methods Provided by the class JLabel
Methods Provided by the class JLabel
class JText. Field Ø Used to create and modify JText. Field objects belonging to class JText. Field Text field may accept input or display values ] May specify length in characters of text field ] Ø To create text field Declare reference variable of type JText. Field ] Instantiate object ] Ø To add a text field to a container (display the label in the content pane) use the method add from the container class
Methods Provided by the class JText. Field
- Slides: 17