Java 7 for Absolute Beginners

(nextflipdebug5) #1
CHAPTER 7 ■ WRITING A USER INTERFACE

Getting big, isn't it? Actually, no. It's a much bigger program than most of what we've done so far,
but it's still not as big as many real programs. Managing complexity is a big part of software
development. As mentioned previously, good design and good coding practices let you manage the
complexity of your code and still produce robust programs. I hammer on this point, by the way, because
it is crucial to good software development.
In this case, we’ve managed the additional complexity by adding an arrangeComponents method.
That method lets you associate all the components with a JPanel and then add the panel to the frame.
Without a panel, the frame would have no place to put the components, so JFrame and JPanel often go
hand in hand. As usual, other ways exist, but a JFrame object with one or more JPanel objects is common
in Swing applications.
In a more complex program, multiple methods (and probably classes) would handle all the
components. In larger Swing programs, it's common practice to have each window be defined by its own
class. In such systems, another class (or set of classes), called a controller, determines which window (or
windows) should be visible and which one should have focus. Our little program isn't that complex,
though, so one class will do.
We also had to implement the MouseListener interface, so that the program can tell when someone
clicks on the button. The MouseListener interface requires five methods, but we use only one of them.
Other programs might need to distinguish between when a mouse button is pressed and when it is
released (for enabling behavior such as drag and drop or drawing a box in a drawing program), but our
little program just needs to know that the user clicked the button. Note the line where a mouse listener is
added to the button. Each component that you want to handle an event has to have an event listener
added to it. When you have multiple objects listening for events, you have to figure out which object the
user chooses and make your program respond accordingly. The getSource method lets us do that. You’ll
see a great deal more of that kind of decision making, both with menus and with buttons, in the next
example.


■ Tip If you need to detect whether a mouse button has been clicked, use the mouseReleased method instead


of the mouseClicked method. In many JVMs, mouseClicked doesn't work well (in particular, it misses some


clicks, which is frustrating for the user), whereas mouseReleased is more reliable. This happens because mouse


motion interrupts the mouse listener (Java has a separate listener interface for monitoring mouse motion). So, if


you twitch just a little bit while clicking the button, you lose the mouse click.


A Larger Swing Application


As it happens, I like Minesweeper and similar games, so I thought I'd write my own Minesweeper game
to serve as a larger, more real example of a Swing application. In addition to being one of my favorite
games, MineSweeper makes a good sample because it uses a mouse listener and an action listener. It's
also the kind of application that benefits from having both a program class and a number of additional
classes to handle various bits of functionality.
Before we get started, let's think about the design of such a program. Table 7-1 describes the various
classes that comprise the MineSweeper program.

Free download pdf