How to change JLabel font style and size in Java

Hello folks! In this tutorial, we will discuss how to change the JLabel font style and font size in Java. I will discuss thoroughly each of them. Firstly, let us discuss changing of JLabel font style in Java.

How to change JLabel font style in Java

We import the Java Swing and Java AWT libraries into our code and use them.

  • Java Swing library is used for creating lightweight windows and it provides various powerful components such as JFrame, JLabel, etc.
  • Java AWT Library contains all the necessary classes needed to develop a GUI (Graphical User Interface).

Let us walk through the code and understand it better.

Firstly, We are creating a JFrame with the title “JLabel Font Style Example”. The setBounds() function is used to specify the bounds of the frame for easier visualization. The first two parameters are the (x,y) i.e., the location of the frame, and the other two parameters indicate the height and width of the frame respectively.

Now, we create a container to add our objects to the frame. We create a JLabel object with the title “Welcome!” and name it obj1. We use Object.setFont() function to set the Font Style of our text. For, the first object, the font style is set to “Arial”.

Then, we create another JLabel object (obj2) the same as before but with a font style as “Times New Roman” and add both objects to the container frame and display it.

Let’s see the code:

//Necessary Imports
import java.awt.*; 
import javax.swing.*;

public class FontSyle extends JFrame {
    public static void main(String[] args) {

        JFrame frame = new JFrame("JLabel Font Style Example"); // Creating a new JFrame with a title
        frame.setLayout(null); // To terminate the default flow layout
        frame.setVisible(true); // To display the frame
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // To Terminate JFrame and the program on close
        frame.setBounds(100, 200, 400, 400); // To set the bounds of the frame.

        Container c = frame.getContentPane(); //To get content pane layer and add objects in the frame.
       
        //Creating First JLabel Object
        JLabel obj1 = new JLabel("Welcome!"); //Creating a JLabel with title as Welcome 
        obj1.setBounds(100, 100, 300, 30); //Setting the bounds of the label.
        obj1.setFont(new Font("Arial", Font.PLAIN, 30)); //Creating an Arial Font Style with size 30 

        //Creating Second Jlabel Object with different font
        JLabel obj2 = new JLabel("Welcome!"); //Creating same JLabel title as before.
        obj2.setBounds(100, 200, 300, 30); //Setting the same bounds as before.
        obj2.setFont(new Font("Times New Roman", Font.PLAIN, 30)); //Creating an Times New Roman Font Style with size 30

        //Adding the objects in the container frame
        c.add(obj1);
        c.add(obj2);
    }
}

Output:

How to change JLabel font style in Java

Analysis:

From, the above output we can see that the first text is in “Arial” font and the second text in “Times New Roman” font with the same font size as 30. Thus, in this way, we can change the JLabel font style in Java.

How to change JLabel font size in Java

We use various parameters of the Font() function to change the font size of JLabel. The Font() function has three parameters as Font Style, Font Weight, and Font Size. We change the Font Size parameter to change the size of the JLabel Font.

The use of the Font() function is shown below:

Object.setFont(new Font("Font-Style", Font-Weight, Font Size));

We use the same code as above keeping font styles the same while changing only the font size. We create a JLabel object (obj1) and assign a font size of 16 and another JLabel object (obj2) with a font size of 30.

Let’s see the code:

//Necessary Imports
import java.awt.*; 
import javax.swing.*;

public class FontSize extends JFrame {
    public static void main(String[] args) {

        JFrame frame = new JFrame("JLabel Font Size Example"); // Creating a new JFrame with a title
        frame.setLayout(null); // To terminate the default flow layout
        frame.setVisible(true); // To display the frame
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // To Terminate JFrame and program on close
        frame.setBounds(100, 200, 400, 400); // To set the bounds of the frame.

        Container c = frame.getContentPane(); //To get content pane layer and add objects in the frame.
       
        //Creating First JLabel Object
        JLabel obj1 = new JLabel("Welcome!"); //Creating a JLabel with title as Welcome 
        obj1.setBounds(100, 100, 300, 30); //Setting the bounds of the label.
        obj1.setFont(new Font("Arial", Font.PLAIN, 16)); //Creating an Arial Font Style with size 16 

        //Creating Second Jlabel Object with different font
        JLabel obj2 = new JLabel("Welcome!"); //Creating same JLabel title as before.
        obj2.setBounds(100, 200, 300, 30); //Setting the same bounds as before.
        obj2.setFont(new Font("Arial", Font.PLAIN, 30)); //Creating an Arial Font Style but with size 30

        //Adding the objects in the container frame
        c.add(obj1);
        c.add(obj2);
    }
}

Output:

How to change JLabel font size in Java

Analysis:

From, the above output we can see that the first text is in “Arial” font style with a size of 16 and the second text with the same font style but with a font size of 30. Thus, in this way, we can change the JLabel font size in Java.

Thanks for reading. I hope you found this post useful!

Also read: Real time Currency conversion Java GUI application using fixer io API

Leave a Reply

Your email address will not be published.