Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Nancy Den

To change the JLabel foreground and background color, use the following methods:

JLabel label;
label.setForeground(new Color(120, 90, 40));
label.setBackground(new Color(100, 20, 70));

The following is an example to change JLabel background and foreground color:

Example

import java.awt.Color;
import java.awt.Font;
import javax.swing.*;
import javax.swing.border.Border;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Demo");
      JLabel label;
      label = new JLabel("This is demo label!",JLabel.RIGHT);
      label.setVerticalAlignment(JLabel.TOP);
      label.setFont(new Font("Verdana", Font.PLAIN, 15));
      label.setForeground(new Color(120, 90, 40));
      label.setBackground(new Color(100, 20, 70));
      Border border = BorderFactory.createLineBorder(Color.ORANGE);
      label.setBorder(border);
      frame.add(label);
      frame.setSize(600,300);
      frame.setVisible(true);
   }
}

Output

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.