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.

You can use the setBackground(Color c) and setForeground(Color c) methods<br />

defined in the java.awt.Component class <strong>to</strong> set a <strong>com</strong>ponent’s background and foreground<br />

colors. Here is an example of setting the background and foreground of a but<strong>to</strong>n:<br />

JBut<strong>to</strong>n jbtOK = new JBut<strong>to</strong>n("OK");<br />

jbtOK.setBackground(color);<br />

jbtOK.setForeground(new Color(100, 1, 1));<br />

Alternatively, you can use one of the 13 standard colors (BLACK, BLUE, CYAN, DARK_GRAY,<br />

GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, and YELLOW) defined as<br />

constants in java.awt.Color. The following code, for instance, sets the foreground color of<br />

a but<strong>to</strong>n <strong>to</strong> red:<br />

jbtOK.setForeground(Color.RED);<br />

12.8 The Font Class 461<br />

12.16 How do you create a color? What is wrong about creating a Color using new<br />

Color(400, 200, 300)? Which of two colors is darker, new Color(10, 0, 0)<br />

or new Color(200, 0, 0)?<br />

12.17 How do you create a Color object with a random color?<br />

12.18 How do you set a but<strong>to</strong>n object jbtOK with blue background?<br />

12.8 The Font Class<br />

Each GUI <strong>com</strong>ponent has the font property. Fonts are objects created from the Font class.<br />

You can create a font using the java.awt.Font class and set fonts for the <strong>com</strong>ponents using<br />

the setFont method in the Component class.<br />

The construc<strong>to</strong>r for Font is:<br />

public Font(String name, int style, int size);<br />

You can choose a font name from SansSerif, Serif, Monospaced, Dialog, and<br />

DialogInput, choose a style from Font.PLAIN (0), Font.BOLD (1), Font.ITALIC (2),<br />

and Font.BOLD + Font.ITALIC (3), and specify a font size of any positive integer. For<br />

example, the following statements create two fonts and set one font <strong>to</strong> a but<strong>to</strong>n.<br />

✓Point✓ Check<br />

Key<br />

Point<br />

Font font1 = new Font("SansSerif", Font.BOLD, 16);<br />

Font font2 = new Font("Serif", Font.BOLD + Font.ITALIC, 12);<br />

JBut<strong>to</strong>n jbtOK = new JBut<strong>to</strong>n("OK");<br />

jbtOK.setFont(font1);<br />

Tip<br />

If your system supports other fonts, such as “Times New Roman,” you can use the font <strong>to</strong><br />

create a Font object. To find the fonts available on your system, you need <strong>to</strong> obtain<br />

an instance of java.awt.GraphicsEnvironment using its static method<br />

getLocalGraphicsEnvironment(). GraphicsEnvironment is an abstract class<br />

that describes the graphics environment on a particular system. You can use its<br />

getAllFonts() method <strong>to</strong> obtain all the available fonts on the system and its<br />

getAvailableFontFamilyNames() method <strong>to</strong> obtain the names of all the available<br />

fonts. For example, the following statements print all the available font names in the system:<br />

find available fonts<br />

GraphicsEnvironment e =<br />

GraphicsEnvironment.getLocalGraphicsEnvironment();<br />

String[] fontnames = e.getAvailableFontFamilyNames();<br />

for (int i = 0; i < fontnames.length; i++)<br />

System.out.println(fontnames[i]);

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

Saved successfully!

Ooh no, something went wrong!