How to create and select a font in java.awt?

First you have to create a Font object. To create a Font object, you have to use the following constructor:

Font(String fname, int fstyle, int ptsize)

where, fname is the name of font. It can be any one of Serif, Sans Serif, Dialog, DialogInput, Symbol and Monospaced. In java.awt Dialog is the default font. It is used for dialog box.

fstyle is for style of fonts which could be Bold, Italic or Plain. You can use this by constants: Font.BOLD, Font.ITALIC and Font.PLAIN. Two styles can also be combined together: Font.BOLD | Font.ITALIC.

To select the created font, setFont( ) method is used:

void setFont(Font fontob)

where, fontOb is font object having the created font.

Leave a Reply