2011-02-01 21 views
1

我想在我的JPanel上有图像,在我的JPanel上也有组件,例如JSlider和JRadioButton。正如你所看到的,我从JPanel派生了一个类并重写了方法paintComponent。这是在JPanel上显示图像的好方法。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); 
} 

但是我有一些问题。当我在ImagePanel上添加JSlider,JRadioButton或其他JPanel组件时,这个组件的背景保持为默认值,而不是背景图片。我不知道如何设置这个图像作为这个组件的背景。 请指导我。

问候

回答

1

您必须将其他组件的不透明属性设置为false。

jRadioButton.setOpaque(false); 

例如:

ImagePanel with RadioButton on it.

+0

非常感谢你.. damet garm rafigh !!! – sajad 2011-02-01 09:28:48

0

setOpaque(false)所有其他组件的帮助?

+0

是的..谢谢 – sajad 2011-02-01 15:06:54

1
jRadioButton.setOpaque(false); 

将工作的许多外观和感觉,但如果你想它与Nimbus的工作,你也应该设置背景色为透明:

jRadioButton.setBackground(new Color(0,0,0,0)); 

查看this question了解更多详情。