The Abstract Window Toolkit (AWT)


The Abstract Window Toolkit (AWT) is a large package that enables you to build graphical user interfaces. Some of the components that can be used are buttons, check boxes, choices, labels, lists, scrollbars, text areas, and text fields. Dialog boxes can be created to prompt a user for information. Layout managers are available to arrange the elements in a window.

A complete discussion of the AWT components is beyond the scope of this book. However, the following simple applet demonstrates how to create a user interface that displays three buttons:

 import java.applet.*; import java.awt.*; /*   <applet code="ButtonApplet" width=400 height=60>   </applet> */ public class ButtonApplet extends Applet {   public void init(){     Button b1 = new Button("Yes");     add(b1);     Button b2 = new Button("No");     add (b2);     Button b3 = new Button("Undecided");     add (b3);   } }

You can run this applet with appletviewer ButtonApplet.java. The output will appear as shown in Figure 25–2.

image from book
Figure 25–2: A simple use of AWT components

In the second edition of Java, a package called javax.swing was added to the platform. This package, referred to as Swing, is an advanced graphical user interface built on top of the AWT. Swing provides a more customizable look and feel for Java programs. If you plan to create extensive graphical user interfaces in Java, you should consider further investigation into Swing. The section “How to Find Out More” at the end of this chapter includes sources for learning about Swing.

Event Handling

Events are generated when a user interacts with the AWT components in a graphical user interface. For example, an event is generated when a button is pressed, the mouse is clicked, a scrollbar is manipulated, a menu item is selected, or a key is pressed.

A source generates an event and sends it to one or more listeners. This is known as the delegation event model. The source delegates the handling of that event to the listeners.

A source must implement methods that allow listeners to register and unregister for events. A listener must implement methods from a specific interface in order to receive notifications about this type of event. The java.awt.event package defines classes for the different types of AWT events. It also declares listener interfaces for these events.

For example, a button generates a java.awt.event.ActionEvent object each time it is pressed. Listeners implement the java.awt.event.ActionListener interface to receive these notifications. This interface declares one method whose signature is shown here:

 void actionPerformed(ActionEvent ae)

Here, ae is the ActionEvent object that was generated by the button.

Listeners register and unregister for this type of event notification via the following methods:

 void addActionListener(ActionListener al) void removeActionListener(ActionListener al)

Here, al is the object that implements the ActionListener interface.

The following example illustrates event handling:

 import java.applet.*; import java.awt.*; import java.awt.event.*; /*   <applet code="ButtonEventsApplet" width=400 height=60>   </applet> */ public class ButtonEventsApplet extends Applet implements ActionListener {   Label label;   public void init() {     Button b1 = new Button("Yes");     b1.addActionListener(this);     add(b1);     Button b2 = new Button("No");     b2.addActionListener(this);     add (b2);     Button b3 = new Button("Undecided");     b3.addActionListener(this);     add (b3);     label = new Label(" ");     add(label);   }   public void actionPerformed(ActionEvent ae){     label.setText(ae.getActionCommand());   } }

The ButtonEventsApplet class implements ActionListener. The init() method creates three buttons and adds these to the applet. In addition, the applet itself is registered to receive action events generated by each of these buttons. A label is also added to the applet. This is used to display a string each time a button is pressed.

The actionPerformed() method is invoked when a button is pressed. The getActionCommand() method returns the command string associated with this action event. That string is the label on the button. The setText() method is invoked to display this string in the label.




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net