In order to set a different font, Java provides the Font class contained in java. awt package. You can create the Font object using the following constructor.
public Font(String name, int style, int size);
Here, the parameter namecorresponds to the logical font name. The logical font is one of the five font families supported by Java runtime environment. These include Serif, SansSerif,Monospaced,Dialog and DialogInput. The parameter style represents the Font shape which can take the values Font.Plain(0), Font.Bold(l), Font.Italic(2) and Font.Bold +Font. Italic (3). The parameter size specify the font size which can be any positive integer. The font size is measured in points. A point in 1/72 of an inch.
Font Useful Interface
Method | Returns | Notes |
Font(String name, int style, int size) |
| Style is either Font.PLAIN, Font.BOLD, or Font.ITALIC |
equals(Object ,obj) | Boolean | Use this to check for a font identical to this one |
getFamily() | String | Platform-specific font family name |
getFont(String name) | Static Font | Looks up a font from a system property or null |
getFont(String name, Font font) | Static Font | Same as above, but return the passed font if the property is not found |
getName() | String | Returns the name of this font |
getSize() | Int | The point size |
getStyle() | Int | Style is either Font.PLAIN, Font.BOLD, or Font.ITALIC |
isBold() | Boolean | A convenience method to avoid if (getStyle()== Font.PLAIN |
isItalic() | Boolean | A convenience method |
isPlain() | Boolean | A convenience method |
import java.awt.*;
class FontExample extends Frame
{
FontExample()
{
setLayout(new FlowLayout());
Font f = new Font("Serif",Font.BOLD,12);
Label lblUserid = new Label("Userid");
lblUserid.setFont(f);
TextField txtUserid = new TextField("Mr Thakur",15);
txtUserid.setFont(f);
Button btnOK = new Button("OK");
add(lblUserid); add(txtUserid);
}
}
class FontJavaExample
{
public static void main(String args[])
{
FontExample frame = new FontExample();
frame.setTitle("Font Usage in Java Example");
frame.setSize(250,150);
frame.setVisible(true);
}
}