Set foreground color for JLabel in Java
Description
The following code shows how to set foreground color for JLabel.
Example
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
/*from w ww .ja va 2 s . co m*/
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main extends JFrame {
public static void main(String[] args) {
String lyrics = "<html>Line<br>line<br>line</html>";
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout(10, 10));
JLabel label = new JLabel(lyrics);
label.setForeground(new Color(50, 50, 25));
panel.add(label, BorderLayout.CENTER);
JFrame f = new JFrame();
f.add(panel);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »