Java Swing Tutorial - Java JLabel(String text) Constructor








Syntax

JLabel(String text) constructor from JLabel has the following syntax.

public JLabel(String text)

Example

In the following code shows how to use JLabel.JLabel(String text) constructor.

import javax.swing.JFrame;
import javax.swing.JLabel;
/*from  ww w. j  av a 2  s.  c o  m*/
public class Main {

  public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String htmlLabel = "<html><sup>HTML</sup> <sub><em>Label</em></sub><br>"
        + "<font color=\"#FF0080\"><u>Multi-line</u></font>";
    JLabel label = new JLabel(htmlLabel);
    frame.add(label);

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

  }
}