Formatting JLabel Text using HTML in Java Swing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import javax.swing.*; import java.awt.*; public class HTMLLabel extends JFrame { public HTMLLabel() { setTitle("JLabel with HTML"); initComponents(); } private void initComponents() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 200); Container container = getContentPane(); container.setLayout(new FlowLayout(FlowLayout.CENTER)); // // Create a JLabel object that display a string formatted using HTML. // 14 font size with red and italic. // JLabel label = new JLabel("<html><font size=\"14\" color=\"ff0000\"><i>Hello World</i></font></html>"); container.add(label); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new HTMLLabel().setVisible(true); } }); } } |