How to Set the Font to a Java String

In the Java programming language, a String object encodes a sequence of Unicode characters. String is one of the object classes with native Java support. In addition to that, Java's standard library includes the Abstract Window Toolkit, a set of classes and methods that allows you to change the appearance of text being displayed within a window. You can set the font that Java.AWT will use to print out a string.

Instructions

    • 1

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

      import java.awt.*;

    • 2

      Encode the String you want to display within a JLabel object, as in the following sample code:

      String theString = "All the King's men";

      JLabel theLabel = new JLabel(theString);

    • 3

      Set the font for the JLabel object, like the following sample code:

      java.awt.Font theFont = new java.awt.Font("Helvetica", Font.PLAIN, 10);

      theLabel.setFont(theFont);

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

Related Searches:

References

Comments

Related Ads

Featured