2011-02-01 34 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瞭解更多詳情。