JPanel image and components
I would like to have image on my JPanels an also have components such a开发者_开发百科s JSlider and JRadioButton on my JPanel. I derived a class from JPanel and overrided method paintComponent as you see. This is a good way to have image on JPanel.
public void paintComponent(Graphics g)
{
/*create image icon to get image*/
ImageIcon imageicon = new ImageIcon(imageFile); //getClass().getResource(imageFile)
Image image = imageicon.getImage();
/*Draw image on the panel*/
super.paintComponent(g);
if (image != null)
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
However I have some problems. When I add components such as JSlider, JRadioButton or another JPanel on my ImagePanel; this component's background remains as default and not background picture. I don't know how to set this image as background of this components. please guide me.
regards
you must set opaque properties of other component to false.
jRadioButton.setOpaque(false);
for example :
jRadioButton.setOpaque(false);
Will work for many look-and-feels, but if you want it to work with Nimbus you should also set the background color to be transparent:
jRadioButton.setBackground(new Color(0,0,0,0));
See this question for more details.
Doesn't setOpaque(false)
for all the other components help?
精彩评论