JLabel
text can be formatted using an HTML standard tags
. The example below shows you how we can use and HTML font tag to change the font size, color and style of JLabel
text.
package org.kodejava.swing;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.Container;
import java.awt.FlowLayout;
public class JLabelHTMLStyle extends JFrame {
public JLabelHTMLStyle() {
setTitle("JLabel with HTML Style");
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new JLabelHTMLStyle().setVisible(true));
}
private void initComponents() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(500, 500);
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.
String text = "<html>" +
"<font size='16' color='orange'><strong>Hello World!</strong></font>" +
"</html>";
JLabel label = new JLabel(text);
container.add(label);
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024