How to Change Text Fonts in Java

Code written in the Java programming language can interact with input and output devices in a variety of ways. In particular, Java's Abstract Window Toolkit (a built-in class library) includes classes and methods to change the appearance of text being displayed within a window. It's not hard to change the font of the text printed out by your Java code.

Instructions

    • 1

      Include the following code at the beginning of your Java program:

      import java.awt.*;

    • 2

      Create a JLabel object to contain the text you want to display, as in the following sample code:

      JLabel myLabel = new JLabel("text to be displayed");

      Replace "text to be displayed" by whatever text you want your Java code to display.

    • 3

      Change the font for the JLabel object to whatever font you want to use, as in the following sample code:

      java.awt.Font myFont = new java.awt.Font("Times", Font.BOLD, 12);

      myLabel.setFont(myFont);

      Replace "Times" by the name of the font you want to use, "Font.BOLD" for the type of text as predefined with the Font class (e.g., "PLAIN" corresponds to plain text), and "12" by the size in points.

Related Searches:

References

Comments

Related Ads

Featured