C H A P T E R 7
111
Writing a User Interface
Most of the people who want to develop software get into it to write programs that have rich user
interfaces of some sort. That rich user interface might be through a set of windows, such as programs
like Word or many games use, or through a web browser. However, a substantial community of
developers largely develops programs that have no user interface. They write programs such as device
drivers (the software that lets something like a mouse work with a computer), services (programs that
provide information to other programs outside of the user interface), and database engines (which
usually manage data in such a way that many programs can access that data). So there's plenty of work
to do outside of user interfaces. Still, though, the visible part of the job attracts a lot of people to the
profession, and there's nothing wrong with that.
This chapter shows you how to develop a user interface with the most commonly used of Java's
built-in user-interface toolkits: Java Swing. We start with some simple basics and we end with an
implementation of a Minesweeper game. After all, why not have a little fun when we're done? (And I bet
a lot of the folks who buy this book would like to write a game. That works for me; I like games, too.)
Java Swing: The Basics
Java Swing is a large toolkit that includes support for all the things that you generally (and some things
you don't generally) see in a non-web user interface: windows, buttons (with text, images, both, or
neither), option lists, menus, labels, text boxes, text areas, checkboxes, droplists, drawing areas, file-
selection dialogs, file save dialogs, other dialogs, and so on. In short, Swing offers all the tools you need
to write almost any program. Some specialized programs might require interface objects that Swing
doesn't have, but other toolkits probably don't have them, either. Also, Swing does include the ability to
make new kinds of interface objects, though that's beyond the scope of this book.
Swing uses the Java Foundation Classes (JFC). The JFC is another toolkit that supports the creation
of applications that can run on many different operating systems (such as Windows, Unix, and MacOS).
Swing is one of several toolkits supported by JFC. Others include Java2D and the Abstract Window
Toolkit (AWT). We use parts of the AWT as we work on our Swing application. For example, Java's Color
class is part of the AWT.
Some people dislike Swing, claiming that it's overly complex and slow. Those criticisms fall on any
UI toolkit, though. Swing is verbose but not more verbose than other UI toolkits that I've used. When
you need to specify a lot of properties (size, font, color, placement on the screen, conditional presence,
and so on), it takes a number of lines of code for each object to specify all that. That's the nature of the
job, regardless of toolkit. As for speed, Swing can perform as well as other UI toolkits, though you have to
know a great deal about the toolkit to get the most from it. Again, that's also true of other UI toolkits. The
Java 7 for Absolute Beginners
© Jay Bryant 2012
J. Bryant,