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 JOptionPane 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
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. 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 getContentPane 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
class JTextField Used to create and modify JTextField objects belonging to class JTextField 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 JTextField 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 JTextField