
306
|
Chapter 8, Rendering
#60 Changing Fonts Throughout Your Application
HACK
Fonts ’R’ Us
To run the code, you specify the name of your desired font on the com-
mand line as the only argument to the class. If the font you want to use has
one or more spaces in its name, you’ll need to enclose the font name in
quotes, as in the example:
cadamson% java ChangeAllFonts "Comic Sans MS"
This produces a GUI whose components all use the 12-point plain Comic
Sans MS font, as seen in Figure 8-5.
You should also note the standard output when running this example, since
the code prints out every
.font key it finds. The output is too long to list
here, but the first few items look like this:
JFrame frame = new JFrame ("Changing default fonts");
frame.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane( ).add(panel);
frame.pack( );
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace( );
}
}
}
Figure 8-5. Changing default fonts for Swing widgets
CheckBox.font ToolBar.font
Tree.font ColorChooser.font
Viewport.font ToggleButton.font
ProgressBar.font Panel.font
RadioButtonMenuItem.font TextArea.font
FormattedTextField.font
Example 8-7. Changing default fonts via UIDefaults (continued)