19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

This was cumbersome. Versions of <strong>Java</strong> since <strong>Java</strong> 5 allow you <strong>to</strong> place <strong>com</strong>ponents in the<br />

content pane by invoking a frame’s add method, as follows:<br />

frame.add(jbtOK);<br />

12.5 Layout Managers 451<br />

This feature is called content-pane delegation. Strictly speaking, a <strong>com</strong>ponent is added <strong>to</strong> the<br />

content pane of a frame. For simplicity, we say that a <strong>com</strong>ponent is added <strong>to</strong> a frame.<br />

In Listing 12.2, an object of JBut<strong>to</strong>n was created using new JBut<strong>to</strong>n("OK"), and this<br />

object was added <strong>to</strong> the content pane of the frame (line 9).<br />

The add(Component <strong>com</strong>p) method defined in the Container class adds an<br />

instance of Component <strong>to</strong> the container. Since JBut<strong>to</strong>n is a subclass of Component, an<br />

instance of JBut<strong>to</strong>n is also an instance of Component. To remove a <strong>com</strong>ponent from a<br />

container, use the remove method. The following statement removes the but<strong>to</strong>n from the<br />

container:<br />

content-pane delegation<br />

container.remove(jbtOK);<br />

When you run the program MyFrameWithComponents, the window will be displayed as in<br />

Figure 12.3b. The but<strong>to</strong>n is always centered in the frame and occupies the entire frame no<br />

matter how you resize it. This is because <strong>com</strong>ponents are put in the frame by the content<br />

pane’s layout manager, and the default layout manager for the content pane places the but<strong>to</strong>n<br />

in the center. In the next section, you will use several different layout managers <strong>to</strong> place <strong>com</strong>ponents<br />

in the desired locations.<br />

12.5 How do you create a frame? How do you set the size for a frame? How do you<br />

add <strong>com</strong>ponents <strong>to</strong> a frame? What would happen if the statements<br />

frame.setSize(400, 300) and frame.setVisible(true) were swapped in<br />

Listing 12.2?<br />

✓Point✓ Check<br />

12.5 Layout Managers<br />

Each container contains a layout manager, which is an object responsible for laying<br />

out the GUI <strong>com</strong>ponents in the container.<br />

In many other window systems, the user-interface <strong>com</strong>ponents are arranged by using hardcoded<br />

pixel measurements. For example, when placing a but<strong>to</strong>n at location (10, 10) in a<br />

window using hard-coded pixel measurements, the user interface might look fine on one system<br />

but be unusable on another. <strong>Java</strong>’s layout managers provide a level of abstraction that<br />

au<strong>to</strong>matically maps your user interface on all window systems.<br />

The <strong>Java</strong> GUI <strong>com</strong>ponents are placed in containers, where they are arranged by the container’s<br />

layout manager. In the preceding program, you did not specify where <strong>to</strong> place the OK<br />

but<strong>to</strong>n in the frame, but <strong>Java</strong> knows where <strong>to</strong> place it, because the layout manager works<br />

behind the scenes <strong>to</strong> place <strong>com</strong>ponents in the correct locations. A layout manager is created<br />

using a layout manager class.<br />

Layout managers are set in containers using the setLayout(aLayoutManager) method.<br />

For example, you can use the following statements <strong>to</strong> create an instance of XLayout and set<br />

it in a container:<br />

Key<br />

Point<br />

layout manager<br />

LayoutManager layoutManager = new XLayout();<br />

container.setLayout(layoutManager);<br />

This section introduces three basic layout managers: FlowLayout, GridLayout, and<br />

BorderLayout.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!