0

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....

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by moggz
1

(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);}
}
Votes + Comments
Helpful
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.