Java Font Class: How to set the Font Family, Style, and Size

Java Font class:

Java Font Class:- As described in the previous article, the Component class also provides the method setFont is available, which allows the user in a component Set the font. This method works with an object of the, one font by the name of the font family, the font style, and the Specifies the font size. As a constructor,

public Font (String name, int style, int size)

creates a font object according to the specified values ​​for font Family, style, and size.

As you can see, the font family is indicated by a character string. In principle, A common font name can be used here when creating an object be, however, it is not guaranteed that every font on every computer is available. In Java systems, however, the font families are Monospaced (a non-proportional font such as B. Courier), Sans Serif (a font without serifs such as B. Arial or Helvetica), and Serif (a font with serifs such as Roman) in any case available. These are then usually actually assigned to the existing font of the executing system.

The font style must be specified by an integer value, where one of the constants BOLD (for bold font), ITALIC (for italic font), and PLAIN (for normal script) can use the as final class variables are agreed in the Java Font class. Should the font be bold and italic appear, the two constants can be added and used as a font style Font.BOLD + Font.ITALIC can be used. The font size is in points (pt) specify.

program example: How to use Java Font Class to set the text style, font family, and size using java NetBeans:

with a bold and italic monospaced font in 40-point Labeled size.

Java font class

Java font class Programming Explanation:

First of all, I import the required java library for GUI designing

then I define our public class and extended JFrame extend Jframe means that add all the GUI components to the extended class. here our class is Testing and which is based on JFrame.

Then I define the method of the JFrame class which is Container and its object c

then I define the JFrame label method which is JLabel and its object TextLabel

Then I create the constructor for the Testing class

Then i assign the getContentPane() method to the Container object

then I set the layout flowLayout of the container

then i set the text message using Jlabel object and Jlabel class

Then I set the font style and size

Then i set the font text color for that I used the setForeground method of  Jlabel

Then I Add the label to the frame using container object c and JFrame method add


then I create the object of the class which is the window

Then I set the title of the frame using the setTitle method

Then I set the size of the frame using setSize method

Then I set the window visibility to true



Related Articles

One Comment

  1. This article is a masterpiece of journalistic excellence. The author’s ability to weave together complex issues with ease and present them in a clear, concise, and engaging manner is truly remarkable. What I appreciate most about this article is its commitment to presenting balanced and unbiased perspectives on a range of issues, which is increasingly rare in today’s media landscape. The author’s attention to detail and their thorough research is evident throughout the article, making it a valuable resource for anyone looking to deepen their understanding of important social and political issues.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button