- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we implement a JLabel text with different color and font in Java?
JLabel
- A JLabel class can extend JComponent class and an object of JLabel provides text instructions or information on a GUI.
- A JLabel can display a single line of read-only text, an image or both text and an image.
- A JLabel can also display a single line of text with different colors and fonts using <font size='font-size' color='color of the text'> Some Text </font> tag inside a HTML tag.
- A JLabel can explicitly generate a PropertyChangeListener interface.
Example
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MultiColorLabelTest extends JFrame { public MultiColorLabelTest() { setTitle("MultiColorLabel Test"); setLayout(new FlowLayout()); // multi colored with different font size label JLabel label = new JLabel("<html><font size='5' color=blue> Welcome to</font> <font size='6'color=green> Tutorials Point</font></html>"); add(label); setSize(375, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new MultiColorLabelTest(); } }
Output
- Related Articles
- How can we implement a moving text using a JLabel in Java?
- How can we rotate a JLabel text in Java?
- How to change text font for JLabel with HTML in Java?
- How can we implement an editable JLabel in Java?\n
- How to change JLabel font in Java
- How can we implement the HTML text of JButton in Java?
- How can we implement line wrap and word wrap text inside a JTextArea in Java?
- How can we add different font style items to JList in Java?\n
- How can we implement different borders using the BorderFactory in Java?\n
- How can we implement a long text of the JOptionPane message dialog in Java?
- How to set font face, style, size and color for JTextPane text in Java
- How to change JLabel background and foreground color in Java?
- How can we implement a JToggleButton in Java?
- How can we implement a Custom HashSet in Java?
- How can we implement a scrollable JPanel in Java?

Advertisements