Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 205 Programming II Lecture 5 AWT - I.

Similar presentations


Presentation on theme: "CSC 205 Programming II Lecture 5 AWT - I."— Presentation transcript:

1 CSC 205 Programming II Lecture 5 AWT - I

2 Overview Java’s Abstract Window Toolkit provides classes and other tools for building programs that have a graphical user interface. The term “Abstract” refers to the AWT’s ability to run on multiple platforms. Building a GUI involves creating “abstract” components such as buttons and windows, which are then mapped to “concrete” components for a specific platform.

3 Creating a GUI GUI programming in Java is based on three concepts:
Components. A component is an object that the user can see on the screen and interact with. Containers. A container is a component that can hold other components. Events. An event is an action triggered by the user, such as a key press or mouse click.

4 A GUI Window

5 Hierarchy of Component Classes
The component classes are related to each other through inheritance:

6 Component Properties Some of the properties shared by all components:
Position: The position of the component on the screen. Size: The height and width of the component. Visibility: Whether or not the component is currently visible. Colors: The component’s foreground and background colors.

7 Creating a Component Object
Components are objects, so they’re created by invoking a constructor. A button would be created by using a constructor belonging to the Button class. The most commonly used constructor has one argument (the button’s label): Button b = new Button("Testing"); For a component to be visible, it must be added to a container (typically a frame) by the add method.

8 Event Listener To detect when an event occurs, a special “listener” object can be attached to a component. When the user performs an action that involves the component, a method belonging to the listener object will be called automatically.

9 Frames In Java terminology, a frame is a window with a title and a border. A frame may also have a menu bar. Frames play an important role in the AWT because a GUI program normally displays a frame when it’s executed. The DrawableFrame objects used in previous chapters are examples of frames.

10 Creating a Frame The FrameTest program creates a Frame object and displays it on the screen. This program illustrates three key steps: 1. Using the Frame constructor to create a frame. 2. Setting the size of the frame. 3. Displaying the frame on the screen.

11 Sample Code // Displays a frame on the screen. import java.awt.*;
public class FrameTest { public static void main(String[] args) { Frame f = new Frame("Frame Test"); f.setSize(150, 100); f.setVisible(true); } // WARNING: Frame cannot be closed.

12 Adding Components to a Frame
To add a component to a frame (or any kind of container), the add method is used. add belongs to the Container class, so it’s inherited by Frame and the other container classes. An example of adding a button to a frame: Button b = new Button("Testing"); add(b); These statements would normally go in the constructor for the frame class.

13 Sample Code import java.awt.*; // Driver class
public class ButtonTest { public static void main(String[] args) { Frame f = new ButtonTestFrame("Button Test"); f.setSize(150, 100); f.setVisible(true); } // Frame class class ButtonTestFrame extends Frame { public ButtonTestFrame(String title) { super(title); setLayout(new FlowLayout()); Button b = new Button("Testing"); add(b);

14 Event Listeners Steps involved in handling an event:
1. The user performs an action, causing an event to be triggered (or fired). 2. An object is created that contains information about the event, including an indication of which component was involved. 3. A method that belongs to a listener object is called. The object created in step 2 is passed to the method.

15 Events When an event occurs, an object is created that contains information about the event. This object will belong to one of several different classes, depending on the nature of the event. These classes all belong to the java.awt.event package. Java divides events into two groups: “high-level” events and “low-level” events.

16 Events Class Name Description of Event
ActionEvent A significant action has been performed on a component (a button was pressed, a list item was double-clicked, or the Enter key was pressed in a text field). AdjustmentEvent The state of an adjustable component (such as a scrollbar) has changed. ItemEvent An item has been selected (or deselected) within a checkbox, choice menu, or list. TextEvent The contents of a text area or text field have changed.

17 Interfaces Event-handling requires the use of interfaces.
An interface looks like a class, except that its methods aren’t fully defined. Each method in an interface has a name, a parameter list, and a result type, but no body. One common interface is named ActionListener: public interface ActionListener extends EventListener { public void actionPerformed(ActionEvent evt); } This resembles a class declaration, except that the word class has been replaced by interface, and the actionPerformed method has no body.

18 Interfaces The keyword implements is used to tell the compiler that a class will implement a particular interface. A class that implements the ActionListener interface: class class-name implements ActionListener { public void actionPerformed(ActionEvent evt) { } … // Variables, constructors, and methods, // if desired The class may contain any number of variables, constructors, and methods.

19 Creating Event Listeners
To handle an event, it’s necessary to create an event listener object. This object will be “registered” with a component; when an event occurs that involves the component, one of the listener’s methods will be called. An event listener will be an instance of a “listener class” defined by the programmer.

20 Creating Event Listeners
A listener class must implement one of the interfaces that belong to the java.awt.event package. Listener interfaces for high-level events: Interface Name Required Method ActionListener actionPerformed(ActionEvent evt) AdjustmentListener adjustmentValueChanged(AdjustmentEvent evt) ItemListener itemStateChanged(ItemEvent evt) TextListener textValueChanged(TextEvent evt) Each interface contains a single method. The access modifier for each method is public, and the result type is void.

21 Creating Event Listeners
There’s a similar set of listener interfaces for low-level events. The listener interface for WindowEvent is named WindowListener.

22 Creating Event Listeners
Pressing a button is an action event, so the listener class for a button would need to implement the ActionListener interface. To implement this interface, the class must define a public void method named actionPerformed with a parameter of type ActionEvent. An example of a listener for an action event: class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { }

23 Creating Event Listeners
After writing a listener class, the next step is to create an instance of the class and connect it to a particular component. In the simplest case, a single listener object will be attached to a single component. Suppose that b is a Button object: Button b = new Button("Change Color"); A listener object can be created by using the constructor for the listener class: ButtonListener listener = new ButtonListener();

24 Creating Event Listeners
listener can now be registered as an action listener for the button: b.addActionListener(listener); It’s sometimes possible to save a statement by combining the creation of the listener object with the call of addActionListener: b.addActionListener(new ButtonListener());

25 Creating Event Listeners
Calling addActionListener creates a link between the Button object and its listener: When the user presses the button, the ButtonListener object’s actionPerformed method will be called.

26 // Displays a frame containing a single "Close window"
// button. The frame can be closed by pressing the button. import java.awt.*; import java.awt.event.*; // Driver class public class ButtonTest2 { public static void main(String[] args) { Frame f = new ButtonTestFrame("Button Test"); f.setSize(150, 100); f.setVisible(true); }

27 // Frame class class ButtonTestFrame extends Frame { public ButtonTestFrame(String title) { super(title); setLayout(new FlowLayout()); Button b = new Button("Close window"); add(b); b.addActionListener(new ButtonListener()); } // Listener for button class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { System.exit(0);


Download ppt "CSC 205 Programming II Lecture 5 AWT - I."

Similar presentations


Ads by Google