Monday, 11 May 2015

Simple Graphics and GUI Techniques in Java

This section presents an introduction to the graphical user interface capabilities provided in the Java Abstract Windowing Toolkit (AWT). The AWT is actually a package named java.awtincluded with the JDK

Although there are many user interface classes contained in package java.awt, Table 7.1 lists the most commonly used classes with a brief description of each class.

Table 7.1. Commonly used user interface classes in package java.awt.
Class NameDescription
ButtonA standard pushbutton user interface component
CanvasA canvas that is used as a graphics drawing context
CheckboxA standard checkbox user interface component
CheckboxGroupA user interface component used to group checkboxes
CheckboxMenuItemA checkbox that is displayed as a menu item
ChoiceA menu component that displays the currently selected choice
ColorA class used to encapsulate colors in Java
ComponentThe base class of all user interface components
ContainerA base class for all container/grouping components
DialogA standard dialog screen that can be either modal or nonmodal
FileDialogA standard file selection dialog that is OS-dependent
FontA class used to encapsulate fonts in Java
FrameA frame that can contain window objects
GraphicsThe base class used for all graphics and drawing operations
ImageA class used to encapsulate images in Java
LabelA standard label user interface component
ListA standard list box user interface component
MenuA menu component that resides on a menubar
MenuBarThe menubar that contains menus and menu items
MenuItemMenu selection items that trigger program events
PanelA user interface component used to surround other components
ScrollbarA standard scrollbar user interface component
TextAreaA multiline text editing window
TextFieldA single-line text editing window
WindowA window inside a frame that contains multiple objects
By examining this table, you can see that three primary classes of components exist within the AWT: drawing objects, containers, and user interface components. If you have ever done any type of GUI programming before, you are probably familiar with all of the components listed here.

Laying Out Components on the Screen

Before you dive into GUI programming with Java, there is one type of class contained in the AWT (not listed in Table 7.1) that may be new even to experienced programmers. This type of class is known as a layout manager. Layout managers allow components to be added onscreen without specifying exact coordinate locations for the components. This type of class makes working with onscreen components much easier. Because advanced "drag-and-drop" Java Developer's environments are not currently available, Java developers currently must either use layout managers or give specific screen coordinates to locate components onscreen. This will remain true in the future for developers coding on platforms that do not support advanced development environments. Here are some examples of layout managers:
  • FlowLayout-The default layout manager. This class lays out components from left to right, wrapping onto the next row when necessary.
  • GridLayout-Breaks the screen up into a rectangular grid and then places a component within each grid element.
  • BorderLayout-Adds components to the screen using some specified geographic direction (north, south, east, west, and center).
  • CardLayout-Adds components onto a stack of cards, where only one card is visible at a time. Specific methods need to be called to switch cards and show a different set of components.

Although the FlowLayout is the default layout manager used in the applet class, the following example (Listing 7.4) illustrates the use of the BorderLayout class. All this simple applet does at the current time is add a text box, a list, and a push button. Don't worry about the actual functionality now. This will be added when we discuss events in the next section.


Listing 7.4. The contents of EditList1.java.
import java.awt.TextField;

import java.awt.List;
import java.awt.Button;
import java.awt.BorderLayout;
import java.applet.Applet;

public class EditList1 extends java.applet.Applet
{
  TextField theText;
  List      theList;
  Button    theButton;


  public void init()
  {
    setLayout(new BorderLayout()); // sets the layout manager to be a
 BorderLayout

    theText = new TextField();
    theList = new List();
    theButton  = new Button("Add text to list!");

    add("North", theText);
    add("Center", theList);
    add("South", theButton);
  }
}

 This is an exciting step, but this applet would be incomplete without some sort of functionality. We can use Java events to force some work to be done when the user clicks on a button, double-clicks the list, or edits the text. The next section will briefly discuss event handling. At the end of the section, the EditList1 applet will be modified so that when a user clicks the button, whatever text has been typed into the text field gets added to the list.

Handling Events

Event handling is one of the primary features of graphical user interface programming. It is so important that this type of interface is also commonly called an event-driven interface. If you stop to think about it, there are many events going on constantly within a standard user interface. Key presses, button clicks, and even voice activation can cause processing to occur on your computer. (In addition to common user events, the operating system itself commonly sends events to applications such as when a window is dragged over another window, or when the system itself is preparing to shut down.) To take advantage of this rich model, it is necessary to be able to "handle" or respond to these events in some way. Of course, Java provides this functionality, but it is up to the programmer to implement it. In the previous example, EditList1.java, the user can click on the button continuously, yet no text will ever be added to the list. This is because the default button-click event for the Button class does nothing except change the look of the button while the user is actually clicking it. It is up to the user to provide some type of functionality.

Note
There are two ways to trap events:
  • Override the applet's action() method and trap the events for all components within the applet. If this is done, the action() method's Event object will need to be examined so that the object that sent the event can be determined.
  • Create a new class for each component on the screen that needs to have its events handled. Then override the individual component's action methods.
Listing 7.5 (EditList2.java) uses the Applet class's action()method to trap the button-click event. When this event has been trapped, the contents of the edit control are added to the list box.

Listing 7.5. The contents of EditList2.java.
import java.awt.TextField;

import java.awt.List;
import java.awt.Button;
import java.awt.BorderLayout;
import java.applet.Applet;
import java.awt.Event;

public class EditList2 extends java.applet.Applet
{
  TextField theText;
  List      theList;
  Button    theButton;


  public void init()
  {
    setLayout(new BorderLayout()); // sets the layout manager to be a
 BorderLayout

    theText = new TextField();
    theList = new List();
    theButton  = new Button("Add text to list!");

    add("North", theText);
    add("Center", theList);
    add("South", theButton);
  }

  public boolean action(Event evt, Object arg)
  {
    if (evt.target instanceof Button)
      theList.addItem(theText.getText()); 

    return true;
  }
}

Running the Example4.htmlfile within a Web browser will show that the event handling implemented above actually works. Figure 7.4 shows Example4.htmlrunning within the Netscape Navigator browser. 

No comments:

Post a Comment