Java 7 for Absolute Beginners

(nextflipdebug5) #1

CHAPTER 7 ■ WRITING A USER INTERFACE


That listing shows why some people complain that Swing is verbose. We added a lot of code just to
add a menu with a single item. We can manage that problem, though, by using good software design and
coding practices. Feel free to write helper classes (for example, the ActionListener can be its own class;
here's a place to apply the design principles we covered in Chapter 6, “Object-Oriented Programming”),
and you should always be careful to make each method serve a well-defined purpose. As your programs
get larger, good design and good coding become ever more important. Indeed, good design is a
developer's primary skill (though we often keep the designs simple in this book to make easier-to-read
examples).
So, let's look at what we had to add to get a menu. We encapsulated the menu code in a separate
method (a good practice) called addMenu. As you can see, it first creates a menu called “File,” then it
creates a menu item called “Exit,” and finally, it creates a menu bar to hold the menu item and attaches
that menu bar to the JFrame object. If we created another menu object and added it to the menu bar,
we'd have another top-level menu object. For example, the word processor I'm using to write this book
has a File menu, an Edit menu, a Format menu, and so on. Each top-level menu is visible in the menu
bar and contains one or more menu items. .The createAndShowGUI method (which is the controlling
method in the program) then calls the addMenu method.
The other big addition that we need to make a menu work is an ActionListener. In this case, we
made the program class itself the ActionListener by implementing the ActionListener interface. More
complex programs often have a separate class (or several classes) serve as listeners. A listener, as its
name implies, monitors (listens to) the events in the program and lets you specify that something should
be done when a certain event happens. In this case, we want the program to stop when someone
chooses Exit from the File menu, so we check for that event and exit when it happens. Java offers a
number of listeners, including mouse, keyboard, and many other listeners, all of which extend the
EventListener interface. We'll add another listener in the next iteration of our SwingDemo program.
Let's add a few other commonly used components: a button, a label, a text area, and a separate child
window called a dialog. In software development, a dialog (or dialog box) is a separate window that
serves a simple, well-defined purpose, such as displaying information or letting the user choose a file. All
those little windows that pop up to ask whether you want to do something in the Windows operating
system are probably the best known (and least loved) dialog boxes. Listing 7-3 shows the expanded code
for all these new components.
Figure 7-3 shows what our modified program looks like when we're done.

Figure 7-3. SwingDemo with more features
Free download pdf