JPanelに画像を表示したいのですが、JPanelにJSliderやJRadioButtonなどのコンポーネントも表示したいと思います。ご覧のとおり、JPanelからクラスを派生させ、メソッドpaintComponentをオーバーライドしました。これは、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);
}
しかし、私にはいくつかの問題があります。JSlider、JRadioButton、または別のJPanelなどのコンポーネントをImagePanelに追加すると、このコンポーネントの背景はデフォルトのままであり、背景画像ではありません。この画像をこのコンポーネントの背景として設定する方法がわかりません。案内してください。
よろしく