19.09.2015 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

462 Chapter 12 GUI Basics<br />

VideoNote<br />

✓Point✓ Check<br />

Key<br />

Point<br />

Use Swing <strong>com</strong>mon properties<br />

Component<br />

Container<br />

JComponent<br />

<strong>to</strong>ol tip<br />

12.19 How do you create a Font object with font name Courier, size 20, and style bold?<br />

12.20 How do you find all available fonts on your system?<br />

12.9 Common Features of Swing GUI Components<br />

GUI <strong>com</strong>ponents have <strong>com</strong>mon features. They are defined in the superclasses<br />

Component, Container, and JComponent.<br />

So far in this chapter you have used several GUI <strong>com</strong>ponents (e.g., JFrame, Container,<br />

JPanel, JBut<strong>to</strong>n, JLabel, and JTextField). Many more GUI <strong>com</strong>ponents will be<br />

introduced in this book. It is important <strong>to</strong> understand the <strong>com</strong>mon features of Swing GUI<br />

<strong>com</strong>ponents. The Component class is the root for all GUI <strong>com</strong>ponents and containers. All<br />

Swing GUI <strong>com</strong>ponents (except JFrame, JApplet, and JDialog) are subclasses of<br />

JComponent, as shown in Figure 12.1. Figure 12.11 lists some frequently used methods in<br />

Component, Container, and JComponent for manipulating properties such as font,<br />

color, mouse cursor, size, <strong>to</strong>ol tip text, and border.<br />

A <strong>to</strong>ol tip is text displayed on a <strong>com</strong>ponent when you move the mouse on<strong>to</strong> the <strong>com</strong>ponent.<br />

It is often used <strong>to</strong> describe the function of a <strong>com</strong>ponent.<br />

java.awt.Component<br />

-font: java.awt.Font<br />

-background: java.awt.Color<br />

-foreground: java.awt.Color<br />

-preferredSize: java.awt.Dimension<br />

-visible: boolean<br />

-cursor: java.awt.Cursor<br />

+getWidth(): int<br />

+getHeight(): int<br />

+getX(): int<br />

+getY(): int<br />

The get and set methods for these data<br />

fields are provided in the class, but<br />

omitted in the UML diagram for brevity.<br />

The font of this <strong>com</strong>ponent.<br />

The background color of this <strong>com</strong>ponent.<br />

The foreground color of this <strong>com</strong>ponent.<br />

The preferred size of this <strong>com</strong>ponent.<br />

Indicates whether this <strong>com</strong>ponent is visible.<br />

The mouse cursor shape.<br />

Returns the width of this <strong>com</strong>ponent.<br />

Returns the height of this <strong>com</strong>ponent.<br />

getX() and getY() return the coordinate of the<br />

<strong>com</strong>ponent’s upper-left corner within its parent <strong>com</strong>ponent.<br />

java.awt.Container<br />

+add(<strong>com</strong>p: Component): Component<br />

+add(<strong>com</strong>p: Component, index: int): Component<br />

+remove(<strong>com</strong>p: Component): void<br />

+getLayout(): LayoutManager<br />

+setLayout(l: LayoutManager): void<br />

Adds a <strong>com</strong>ponent <strong>to</strong> the container.<br />

Adds a <strong>com</strong>ponent <strong>to</strong> the container with the specified index.<br />

Removes the <strong>com</strong>ponent from the container.<br />

Returns the layout manager for this container.<br />

Sets the layout manager for this container.<br />

-<strong>to</strong>olTipText: String<br />

javax.swing.JComponent<br />

-border: javax.swing.border.Border<br />

The get and set methods for these data<br />

fields are provided in the class, but<br />

omitted in the UML diagram for brevity.<br />

The <strong>to</strong>ol tip text for this <strong>com</strong>ponent. Tool tip text is displayed when<br />

the mouse points on the <strong>com</strong>ponent without clicking.<br />

The border for this <strong>com</strong>ponent.<br />

FIGURE 12.11<br />

JComponent.<br />

All the Swing GUI <strong>com</strong>ponents inherit the public methods from Component, Container, and

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

Saved successfully!

Ooh no, something went wrong!