260 likes | 268 Views
Chapter 9: Visual Programming Basics. Object-Oriented Program Development Using Java: A Class-Centered Approach. Objectives. Event-Based Programming Creating a Swing-Based Window Adding a Window Closing Event Handler Adding a Button Component Common Programming Errors.
E N D
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach
Objectives • Event-Based Programming • Creating a Swing-Based Window • Adding a Window Closing Event Handler • Adding a Button Component • Common Programming Errors
Event-Based Programming • Event-based programs provide fully functioning GUIs • An event is initiated by a user action • A program must: • Correctly assess which specific event has occurred • Provide the appropriate code to perform an action based on the identified event
Event-Based Programming (continued) • Actions that trigger events include: • Placing the mouse pointer over a button and clicking the left mouse button • Using the TAB key until the desired button is highlighted with a dotted line then pushing the Enter key • Pressing an accelerator key (shortcut key) • The sequence of events in a program are controlled by the user
Event-Based Programming (continued) • Programmer provides: • Code to create GUI • Code to appropriately process events • Java provides a set of objects for coding GUIs: • AWT (abstract window toolkit) • Older GUI components • Swing • Newer GUI components
Swing Components Examples • Top-Level Container – JFrame, JApplet, JWindow • Intermediate Container – JPanel, JInternalFrame • Atomic – JButton, JCheckBox, JTextField
The Event-Based Model • Operating system: • Has total control of computer • Never relinquishes control to any executing programs • Most executing programs spend the majority of their time in a sleep type of mode • When an event occurs: • The operating system passes event information to the appropriate application • Permits the application to take action
Containment Hierarchy • Hierarchy of component placement • Consists of one and only one top-level container • Any number of other intermediate containers • And/or atomic components • JFrame is most commonly used as a top-level container • Heavyweight components (earlier Java term = top-level containers) are responsible for interfacing with the operating system
Containment Hierarchy (continued) • A content pane (next level of hierarchy) is an internal component provided by each top-level container • All of the visible components displayed by a GUI must be placed on a content pane • Menu bar: • Can be optionally added to a top-level container • Placed outside of a content pane
Containment Hierarchy (continued) • Layout manager: • Defines how components are positioned and sized within a container’s content pane • Default placement can always be changed • By explicitly specifying another layout manager • Lightweight components: • Intermediate containers and atomic components • Do not interface with the operating system
JFrame automatically provides a root pane, which in turn contains the content pane. Generally, not concerned with root pane.
Layout managers Each top and intermediate-level containers has a default layout manager that defines the components position and size withing the container’s content pane. You can always change the default layout by specifying another layout manager (chapter 10). The six layout managers currently available are:
Lightweight Components • intermediate containers and atomic components • do not interface directly with the operating system (hence lightweight)
Creating a Swing-Based Window • Two predominant approaches: • Construct a GUI as a separate class using Swing components • Construct a GUI object using Swing components from within the main() method
Creating a Swing-Based Window (continued) • Create JFrame: • JFrame mainFrame = new JFrame("First GUI Window"); • Setting size: • syntax: objectReferenceName.setSize(width, height) • mainFrame.setSize(300,150);
Could also use a single statement jFrame mainFrame = new JFrame(“First Gui Window”);
Creating a Swing-Based Window (continued) • Display JFrame: • Use show() • Or setVisible(true)
import javax.swing.*;public class FirstWindow extends JFrame{ private JFrame mainFrame; public FirstWindow() // a constructor { mainFrame = new JFrame("First GUI Window"); mainFrame.setSize(300,150); mainFrame.show(); } public static void main(String[]args){ new FirstWindow();}} // end of class Provides access to all public and protected methods in the JFrame class
Look and Feel • Refers to: • How a GUI appears on screen • How a user interacts with it • Swing package: • Supports four look and feel types • If no look and feel is specified, the default Java look and feel is used
To implement a specific look and feel, this code must be contained in a try and catch block.