Java Programming

Java Font Class with programming example

Java Font class:

As described in previous article, the Component class also provides the method setFont is available, which allows the user to setFont.

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, 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 an 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.

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

our program example

package testing;



 * @author Fawad khan


import java.awt. *;

import javax.swing. *;

public class Testing extends JFrame {

Container c; // container of this frame  

JLabel TextLabel; // Label that should appear in the frame

public Testing() {// constructor

 c = getContentPane(); // determine container

 c.setLayout (new FlowLayout ()); // set layout

 // Create the label object with the transfer of the label text

TextLabel=new JLabel("Testing java font class");

 TextLabel.setFont(new Font("Monospaced",Font.BOLD+Font.ITALIC,40));


// Add the label to the frame

 c.add (TextLabel);


 public static void main (String [] args) {

 Testing window = new Testing();

 window.setTitle ("Java Font Class");

 window.setSize (700,200);

 window.setVisible (true);

 window.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);



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

Java font class

Programming Explanation:

First of all we import the required java library for GUI designing

import java.awt. *;

import javax.swing. *;

Then we define our public class and extended JFrame extend Jframe means that add all the GUI component to the extended class. here our class is Testing and which based on JFrame.

public class Testing extends JFrame

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

Container c;

Then we define the JFrame label method which is JLabel and its object TextLabel

JLabel TextLabel;

Then we create the constructor the Testing class

public Testing() {

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

c = getContentPane();

Then we set layout flowLayout of the container

c.setLayout (new FlowLayout ());

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

TextLabel=new JLabel(“Testing java font class”);

Then we set the font style and size

TextLabel.setFont(new Font(“Monospaced”,Font.BOLD+Font.ITALIC,40));

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


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

c.add (TextLabel);


public static void main (String [] args) {

Then I create the object of the class which is window

Testing window = new Testing();

Then I set the title of the frame using setTitle method

window.setTitle (“Java Font Class”);

Then I set the size of the frame using setSize method

window.setSize (700,200);

Then I set the window visibility true

window.setVisible (true);

window.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);



Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

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

Back to top button