I added a bachground color to a jpanel using

panel.setBackGround(Color.GRAY);

I want to add some color like "light blue." which do not have in the color list.

how can I do.

(2) how to add backgroud picture to jpanel????
(tried using setBackGround but failed!!!)


thankx in advance....

(1)You can make you own Color Using Color Class Constructor with RGB Values. Search for Exact Color value you want
You can use new Color(100,149,237); or adjust these values as per your requirement

(2) You can use paint method to paint your image
maybe this can help

import java.awt.*;
import javax.swing.*;
class loginScreen extends JFrame
{
  public loginScreen()
  {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    ImagePanel panel = new ImagePanel("test.gif");
    panel.add(new JButton("OK"));
    getContentPane().add(panel);
    pack();
    setLocationRelativeTo(null);
  }
  class ImagePanel extends JPanel
  {
    Image img;
    public ImagePanel(String file)
    {
      setPreferredSize(new Dimension(600, 400));
      try
      {
        img = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource(file), file));
      }
      catch(Exception e){}//do nothing
    }
    public void paintComponent(Graphics g)
    {
      super.paintComponent(g);
      if(img != null) g.drawImage(img,0,0,getWidth(),getHeight(),this);
    }
  }
  public static void main(String[] args){new loginScreen().setVisible(true);}
}
commented: Helpful +25

You can use a JColorChooser to click the exact colour in the HSB tab or you can get the exact value in the RGB tab then specify the values of red,green and blue in your new colour as below;
Color lightblue = new Color(red,green,blue);
panel.setBackground(lightblue);

For the background you can try using look and feel coz it always works for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.