How to change text font for JLabel with HTML in Java?


To change text font, you can use the setFont() method of JLabel −

label.setFont(new Font("Verdana", Font.PLAIN, 12));

The following is an example to change text font for JLabel with HTML −

Example

import java.awt.Font;
import javax.swing.*;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Label Example");
      JLabel label;
      label = new JLabel("<html><font size=+1>" + "<center>ABC</html>");
      label.setBounds(50, 50, 100, 30);
      label.setFont(new Font("Verdana", Font.PLAIN, 12));
      frame.add(label);
      frame.setSize(500,300);
      frame.setLayout(null);
      frame.setVisible(true);
   }
}

Output

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

613 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements