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

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

Jan 03, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.

Chapter 9: Visual Programming Basics

Object-Oriented Program Development Using Java:

A Class-Centered Approach

Page 2: 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

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

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

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

4

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

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

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

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

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

7

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

8

Swing Components

Examples Top-Level Container – JFrame, JApplet,

JWindow Intermediate Container – JPanel,

JInternalFrame Atomic – JButton, JCheckBox, JTextField

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

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

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

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

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

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

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

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

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

13

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

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

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:

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

15

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

16

Lightweight Components

intermediate containers and atomic components

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

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

17

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

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

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

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

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

20

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

21

Could also use a single statement

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

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

22

Creating a Swing-Based Window (continued)

Display JFrame:

Use show()

Or setVisible(true)

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

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

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

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

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

25

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

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

26