Page 735 - Introduction to Programming with Java: A Problem Solving Approach
P. 735
17.4 BorderLayout Manager 701 add(new JButton("Central African Republic"), BorderLayout.CENTER);
add(new JButton("Central African Republic"));
Which statement is better? We prefer the first statement because it makes the code easier to understand. More formally, we say that the first statement is self-documenting.
With a FlowLayout container, you can add as many components as you like. With a BorderLayout container, you can add only five components total, one for each of the five regions. If you add a component to a region that already has a component, then the new component overlays the old component. Thus, in ex- ecuting the following lines, the Somalia button overlays the Djibouti button:
add(new JButton("Djibouti"), BorderLayout.EAST);
add(new JButton("Somalia ", BorderLayout.EAST));
If you need to add more than one component to a region, it’s easy to make the mistake of calling add twice for the same region. After all, there’s no compile-time error to warn you of your misdeed. But what you re- ally need to do is add a JPanel component. We’ll discuss the JPanel component later in the chapter. It allows you to store multiple components in a place where only one component is allowed.
AfricanCountries Program with Buttons
Let’s put this BorderLayout material into practice by using it within a complete program. In our AfricanCountries program, we add African-country buttons to the five regions of a BorderLayout win- dow. See the program’s output window at the bottom of Figure 17.5. The five rectangles you see are the five regions, but they’re also the five buttons. The buttons are the same size as the regions because, with a BorderLayout manageAr, cpomapogneonts aPutoDmaFticallEy enxphanad tno ficll etherir entire region. Note how the outer four regions’ sizes conform nicely to their contents. In other words, the west region is wide enough to show “Western Sahara,” the south region is tall enough to show “South Africa,” and so on. In contrast, note how the center region is unable to display its full “Central African Republic” content. This is because the outer regions control the dividing lines. The center region gets whatever room is left over.
Skim through the AfricanCountries program listing in Figure 17.5. Most of the code is straightforward. But this statement is rather quirky:
add(new JButton("<html>South<br>Africa</html>"), BorderLayout.SOUTH);
Let’s review briefly what those angled-bracket commands are that you see—<html>, <br>, and </html>. As you may recall from the HTMLGenerator program in Chapter 15, the angled bracket elements are called tags. The <html> tag indicates the start of an HTML file, the <br> tag indicates a line break (i.e., a new line), and the </html> tag indicates the end of an HTML file. Normally, you insert tags into an HTML file. But here we’re inserting them into component text in order to produce a new line. When used in JLabel and JButton text, the <html> and </html> tags tell the Java compiler that the enclosed text (the text between the <html> and </html> tags) should be interpreted as HTML text. And the <br> tag tells the Java compiler to insert a newline character in the text.1
We’d like to mention one additional item in the AfricanCountries program. The setLayout method call can be omitted. As we said previously, the BorderLayout is the default layout manager for JFrame windows. Therefore, if you omit the setLayout method call, the program works just fine. But we prefer to keep the setLayout call because it makes the program easier to understand.
1 It may have occurred to you to insert the newline character, \n, into the component’s text. Unfortunately, that doesn’t work for JButton and Jlabel components. However, it does work for the JtextArea component. We’ll describe the JtextArea com- ponent later in the chapter.