Academic Java    Java Tutorial  >  Swing  >  JLabel HTML tags

JLabel HTML tags Example

Java JLabel HTML tags EXAMPLE output In this JLabel example a JLabel is added to a JFrame's content pane.

The text of the JLabel begins "<html>" which causes following HTML tags to be observed.

SwingConstants.CENTER as the second parameter of the JLabel constructor causes the text to be centered in the JLabel's area. Other such constants can be used for alternate alignments.
// JLabel HTML Example
import java.awt.*;
import javax.swing.*;

class JLabelHTMLExample {

   public static void main(String[] args) {

      String s = "<html>here's <font color=blue><i>my</i></font>" +
                  "<br><font color=red>HTML</font><br><br>label";

      JLabel label = new JLabel(s, SwingConstants.CENTER);

      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(label);

      frame.setSize(300,300);
      frame.setVisible(true);
   }
}


* JLabel Examples