Java Reference
In-Depth Information
11.10
Summary
In this chapter, we have given an introduction to GUI programming using the Swing and AWT
libraries. We have discussed the three main conceptual areas: creating GUI components, layout,
and event handling.
We have seen that building a GUI usually starts with creating a top-level frame, such as a
JFrame . The frame is then filled with various components that provide information and func-
tionality to the user. Among the components we have encountered are menus, menu items, but-
tons, labels, borders, and others.
Components are arranged on screen with the help of containers and layout managers.
Containers hold collections of components, and each container has a layout manager that takes
care of arranging the components within the container's screen area. Nesting containers, using
combinations of different layout managers, is a common way to achieve the desired combina-
tion of component size and juxtaposition.
Interactive components (those that can react to user input) generate events when they are ac-
tivated by a user. Other objects can become event listeners and be notified of such events by
implementing standard interfaces. When the listener object is notified, it can take appropriate
action to deal with the user event.
We have introduced the concept of anonymous inner classes as a modular, extendable technique
for writing event listeners.
And finally, we have given a pointer to an online reference and tutorial site that may be used to
learn about details not covered in the chapter.
Exercise 11.77 Add a GUI to the world-of-zuul project from Chapter 6. Every room should
have an associated image that is displayed when the player enters the room. There should
be a non-editable text area to display text output. To enter commands, you can choose
between different possibilities: you can leave the input text-based and use a text field (class
JTextField ) to type commands, or you can use buttons for command entry.
Exercise 11.78 Add sounds to the word-of-zuul game. You can associate individual sounds
with rooms, items, or characters.
Exercise 11.79 Design and build a GUI for a text editor. Users should be able to enter text,
edit, scroll, etc. Consider functions for formatting (font faces, style, and size) and a character/
word-count function. You do not need to implement the load and save functions just yet—you
may like to wait with that until you have read the next chapter.
Terms introduced in this chapter
GUI, AWT, Swing, component, layout, event, event handling, event listener,
frame, menu bar, menu, menu item, content pane, modal dialog, anonymous
inner class, final variable
 
 
Search WWH ::




Custom Search