Java 7 for Absolute Beginners

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

frame.getContentPane().add(controlPanel);
frame.getContentPane().add(minePanel);
}


You might ask, “Panels? What panels?” Well, it happens that the MineSweeper program uses two
JPanel objects. One panel holds the score panel and the other holds the minefield. Remember the
BoxLayout object with the vertical axis? It dictates that one panel is over the other. In this case, the panel
that shows the scores (called controlPanel) is above the minefield (called minePanel). Again, let's go
through it one line at a time.
The first line creates a Border object that has 5 pixels of padding on all four sides. We use that Border
object to keep our labels from bumping into the other things in the window. The second line creates a
new JPanel object called controlPanel, which holds our labels. So why not create the panel that holds
the minefield here? Because we need it elsewhere, so we have to create it in the class variables at the top
of the class. The third line sets the padding for the minesFoundLabel object, which tells the player how
many mines they've marked so far. (Of course, the player might be wrong; that's part of the fun.) The
fourth line sets the alignment of the minesFoundLabel to the left, which makes it appear on the left side of
the panel. The fifth line sets the alignment of the minesRemainingLabel, which tells the player how many
mines have yet to be found, such that the label is on the right side of the panel. The sixth line uses our
border definition to add a border to the minesRemainingLabel. The seventh line sets the text of the
minesRemainingLabel. So why didn't we set the value of the minesFoundLabel? Because we don't need to
concatenate two strings together to set that one and we can't be sure how many mines the player has
chosen to have in the game. The next two lines add our labels to the controlPanel object. Then, we
create a GridLayout object, which we use to control the placement of the two labels. In this case, we
create a small grid with just one row and two cells (one for each label). Finally, we add the two panels to
the JFrame object. At this point, all the controls that appear in the window have been defined and their
relationships to one another have also been defined.


■ Note Creating a component, setting its attributes, and adding the component to a container is the normal


idiom for setting up user interfaces in Swing (and other user interface frameworks as well). You'll do a lot of that if


you write many applications that have user interfaces. Remember that a container is also a component, so you'll


often add one component to another component and then add that set of components to yet another component.


You can see that here in the addAndArrangePanels method when we add labels to a JPanel object and then add


the JPanel object to the JFrame object.


Now that we have the components in the window defined, let's add a menu, so that a player can
start a new game, set the size of the game, and exit the game. We do all that in the AddMenu method,
which appears in Listing 7-10.


Listing 7-10. MineSweeper's addMenu method


private void addMenu(JFrame frame) {
JMenu file = new JMenu("File");
file.setMnemonic('F');
JMenuItem newItem = new JMenuItem("New Game");
newItem.setMnemonic('n');

Free download pdf