Pages

Set JLabel background color

Source code below will show you, how to set JLabel background color base on RGB. You can get RGB value for your color using "Color picker" at above.

************************************************************************
COMPLETE SOURCE CODE FOR : SetJLabelBackgroundColor.java
************************************************************************


import javax.swing.JLabel;
import javax.swing.JFrame;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;

public class SetJLabelBackgroundColor extends JLabel
{
//Create JFrame with title ( Set JLabel background color )
JFrame frame=new JFrame("Set JLabel background color");

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=0
//B=0
Color color=new Color(255,0,0);

public SetJLabelBackgroundColor()
{
//Set text for JLabel
setText("Set JLabel Background Color");

//Set JFrame layout to FlowLayout
frame.setLayout(new FlowLayout());

//Add JLabel into JFrame
frame.add(this);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(500,300);

//Make JFrame visible
frame.setVisible(true);
}

//Fill background color to JLabel
public void paint(Graphics g)
{
g.setColor(color);
g.fillRect(0,0,getWidth(),getHeight());
super.paint(g);
}

public static void main(String[]args)
{
SetJLabelBackgroundColor sjlbc=new SetJLabelBackgroundColor();
}
}


************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************
Related Posts Plugin for WordPress, Blogger...