You can create a font using the java.awt.Font class and set fonts for the components using the setFont method in the Component class.
The constructor for Font is:
public Font(String name, int style, int size);
You can choose a font name from SansSerif , Serif , Monospaced , Dialog , or DialogInput , choose a style from Font.PLAIN (0), Font.BOLD (1), Font.ITALIC (2), and Font.BOLD Font.ITALIC (3), and specify a font size of any positive integer. For example, the following statements create two fonts and set one font to a button:
Font font1 = new Font( "SansSerif" , Font.BOLD, 16 ); Font font2 = new Font( "Serif" , Font.BOLD + Font.ITALIC, 12 ); JButton jbtOK = new JButton( "OK" ); jbtOK.setFont(font1);
Tip (Optional)
If your system supports other fonts, such as "Times New Roman," you can use it to create a Font object. To find the fonts available on your system, you need to create an instance of java.awt.GraphicsEnvironment using its static method getLocalGraphicsEnvironment() . GraphicsEnvironment is an abstract class that describes the graphics environment on a particular system. You can use its getAllFonts() method to obtain all the available fonts on the system, and its getAvailableFontFamilyNames() method to obtain the names of all the available fonts. For example, the following statements print all the available font names in the system: GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] fontnames = e.getAvailableFontFamilyNames(); for ( int i = ; i < fontnames.length; i++) System.out.println(fontnames[i]); |