chapter 9: visual programming basics object-oriented program development using java: a...

26
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class- Centered Approach

Upload: alfred-whitehead

Post on 03-Jan-2016

223 views

Category:

Documents


3 download

TRANSCRIPT

Chapter 9: Visual Programming Basics

Object-Oriented Program Development Using Java:

A Class-Centered Approach

2

Objectives

Event-Based Programming

Creating a Swing-Based Window

Adding a Window Closing Event Handler

Adding a Button Component

Common Programming Errors

3

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

4

5

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

6

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

7

8

Swing Components

Examples Top-Level Container – JFrame, JApplet,

JWindow Intermediate Container – JPanel,

JInternalFrame Atomic – JButton, JCheckBox, JTextField

9

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

10

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

11

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

12

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

13

JFrame automatically provides a root pane, which in turn contains the content pane. Generally, not concerned with root pane.

14

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:

15

16

Lightweight Components

intermediate containers and atomic components

do not interface directly with the operating system (hence lightweight)

17

18

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

19

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

20

21

Could also use a single statement

jFrame mainFrame = new JFrame(“First Gui Window”);

22

Creating a Swing-Based Window (continued)

Display JFrame:

Use show()

Or setVisible(true)

23

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

24

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

25

To implement a specific look and feel, this code must be contained in a try and catch block.

26