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

Updated on: 2019-07-30T22:30:26+05:30

923 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements