+ All Categories
Home > Documents > Unit 111 Java GUI Components and Events Learning Outcomes oDistinguish between GUI components and...

Unit 111 Java GUI Components and Events Learning Outcomes oDistinguish between GUI components and...

Date post: 21-Dec-2015
Category:
View: 234 times
Download: 2 times
Share this document with a friend
Popular Tags:
14
Unit 11 1 Java GUI Components and Events Learning Outcomes o Distinguish between GUI components and containers. o Identify and distinguish top-level containers from other containers. o Write simple programs to add components into containers. o Explain what Events are, and distinguish between Event sources, Event classes and Event listeners. GUI Components and Containers Swing's Top-level Containers Adding Components to Containers Self-check Exercise 1 GUI Events GUI Events Classes Self-check Exercise 2 Exercises
Transcript
Page 1: Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.

Unit 11 1

Java GUI Components and Events

Learning Outcomeso Distinguish between GUI components and containers.

o Identify and distinguish top-level containers from other containers.

o Write simple programs to add components into containers.

o Explain what Events are, and distinguish between Event sources, Event classes and Event listeners.

GUI Components and Containers Swing's Top-level Containers Adding Components to Containers Self-check Exercise 1 GUI Events GUI Events Classes Self-check Exercise 2 Exercises

Page 2: Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.

Unit 11 2

Introduction to Components

• A component is an object having a graphical representation that can be displayed on the screen.

• Example of components in a typical GUI include:

• buttons, text boxes, lables and pop-up menus

Page 3: Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.

Unit 11 3

Introduction to Containers

A container is a special component that can hold other components.

• Example of containers in typical GUI applications include:o panels, windows, applets, frames

• Functionality of most GUI

components derive from the

Component and

Container classes.

Page 4: Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.

Unit 11 4

Swing’s Top-level Containers

• JApplet, JWindow, JFrame and JDialog are the top-level Swing

containers.

• Strictly speaking, JWindow and JFrame are the top-level containers.

• However, use top-level classes to refer to JApplet, JWindow, JFrame

and JDialog.

• A top-level container can be displayed without being added to another

container.

• Top-level containers are the only heavyweight Swing components.

• If you create an AWT window and add 3 buttons to it, AWT will

create four peers for you.

• Top-level containers do not behave exactly like other containers

Page 5: Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.

Unit 11 5

Anatomy of Top-level Containers

• Each top-level container has only one component, JRootPane.

• The root pane consists of three other containers: the layered, content and glass panes:

Page 6: Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.

Unit 11 6

Anatomy of Top-level Containers (cont’d)

• The root pane has a glass pane on top and a layered pane underneath.

• The glass pane component is

always painted last and appears

on top of the content pane and

menu bar.

• The layered pane consists of

a menu bar and a content pane.

• A content pane is a Container that covers the visible area

of a container.

• A content pane is automatically created for a newly created container.

• Components must be added to the content pane of a container.

• The content pane can be retrieved using the getContentPane method.

Page 7: Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.

Unit 11 7

Example 1: Creating Windows & Frames

1 import java.awt.*; import javax.swing.*; 2 public class TopLevelWindows{ 3 public static void main(String [] args){ 4 JFrame frame = new JFrame("My JFrame"); 5 frame.setLocation(100,100); 6 frame.setSize(300,300); 7 Container fcp = frame.getContentPane(); 8 JButton fb = new JButton("Draggable, Resizable Frame"); 9 fcp.add(fb);1011 JWindow window = new JWindow();12 window.setLocation(500,100);13 window.setSize(300,300);14 Container wcp = window.getContentPane();15 JButton wb = new JButton("Unmovable, No Frills Window");16 wcp.add(wb);1718 frame.setVisible(true);19 window.setVisible(true);20 }21 }

Page 8: Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.

Unit 11 8

Example 2: Adding Components to Containers

1 import java.awt.*; 2 import javax.swing.*; 3 public class AddingComponents extends JFrame{ 4 5 JButton button = new JButton("Press Me"); 6 JLabel label = new JLabel( "Running Total:"); 7 JTextField textField = new JTextField(10); 8 Container cp = getContentPane(); 910 public AddingComponents() {11 super("A Container With Components");12 setSize(300,100);13 cp.setLayout(new FlowLayout());14 cp.add(label);15 cp.add(textField);16 cp.add (button);17 setVisible(true);18 }19 public static void main(String args []) {20 new AddingComponents();21 }22 }

Page 9: Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.

Unit 11 9

Introduction to GUI Events

• We will now discuss how components and containers communicate.

• When a user interacts with a GUI component, events are triggered.

• An event is an action triggered by the user or by some other means.

• When an event is triggered an event object is created and delivered

to the event receivers.

• Execution of these kinds of programs is driven by users’ activation

of events.

• Event classes, event sources and event listeners are three groups of

Java classes crucial for event-driven programming.

Page 10: Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.

Unit 11 10

Events Classes Hierarchy• Event classes represent events and contain methods for getting information

on the events.

• Here is the class hierarchyfor events classes:

Page 11: Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.

Unit 11 11

Events Source Classes

• An event source is the component that generates an event.

• A source must register listeners who may wish to take some action

when the event is generated.

• When an event occurs the source informs all registered listeners for

this event.

• An event listener can register or unregister with the following

methods:

1. public void addTypeListener(TypeListener t)

2. public void removeTypeListener (TypeListener t)

• Type is the name of the event and t is a reference to the event

listener.

Page 12: Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.

Unit 11 12

Events Listener Classes

• The java.awt.event package contains interfaces and classes for dealing with events.

• Each listener interface is a specification for receiving a particular type of event.

• An event listener class implements the listener interfaces of interest to the class.

• Every event handler should satisfy these two conditions:

1. Implement an interface.

2. Register as event listener.

Page 13: Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.

Unit 11 13

Events: A Pictorial View

Page 14: Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.

Unit 11 14

Exercises

1. The Panel, Window and JComponent class each subclasses Component class.

Write down the similarities and differences between these classes in a tabular form.

2. Write down in tabular form the similarities and differences between top-level

components and other GUI components.

3. Write a Java program to display three frames as follows. The top-left corner of the

second frame should have the same coordinates as the bottom-right corner of the

first frame. Similarly, the top-left corner of the third frame should have the same

coordinates as the bottom-right corner of the second frame.

4. Run the program in Example 2. Try resizing the window and watch how the

components placements change. Modify this program by adding four more buttons.

Remove the setSize method call and replace it with the call to the pack method.

Re-run the program and notice the output.

5. Write short notes on

a. Event classes

b. Event sources

c. Event listeners


Recommended