Java Basic Problems

Java: How to change font of JLabel?

In my last post I have discussed about JTextField Validation process.

Today I want to share my idea about how to change the font of JLabel in java.

Sometime in java programming we may need to change the font of   JLabel. Actually, it depends on the developer and may be for  the outlookings of the software.

So, whatever the reason is, there is a way to change the font  of JLabel in java.

In the given example, I have just tried to give an idea about  how to declare JLabel in java and also how to change the font and  font appearance of the following JLabel.

*** Important Note: Whatever the font you want to apply in your JLabel, it must be installed on  that computer.

Code Example:

import javax.swing.*;

import java.awt.*;

class labelFont extends JFrame

{

JLabel  label1=new JLabel(“Hi,My name is Prakash”,JLabel.CENTER);

JLabel  label2=new JLabel(“I am a Student”,JLabel.CENTER);

JLabel  label3=new JLabel(“I like Programming and I am a beginner”);

labelFont()

{

          label1.setBounds(95,00,140,40);

          label2.setBounds(95,15,140,40);

          label3.setBounds(15,200,270,40);

          Font displayFont=new Font(“Times New Roman”,Font.PLAIN,12);

    label1.setFont(displayFont);

          Font displayFont1=new Font(“Calibri (Body)”,Font.PLAIN,12);

    label2.setFont(displayFont1);

                  setSize(300,300);

                             setTitle(“JLabel Font Change”);

                             setDefaultCloseOperation(EXIT_ON_CLOSE);

           JPanel panel=new JPanel();

            getContentPane().add(panel);

                             panel.setLayout(null);

                             panel.add(label1);

            panel.add(label2);

            panel.add(label3);

}

public static void main(String args[])

{

labelFont myFont=new labelFont();

myFont.setVisible(true);

}

}

OUTPUT:

JLabel font change java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s