Java JPanel映像和组件
我希望在我的JPanel上有图像,在我的JPanel上也有JSlider和JRadioButton等组件。我从JPanel派生了一个类,并重写了方法paintComponent,如您所见。这是在JPanel上创建图像的好方法Java JPanel映像和组件,java,swing,jpanel,jcomponent,imageicon,Java,Swing,Jpanel,Jcomponent,Imageicon,我希望在我的JPanel上有图像,在我的JPanel上也有JSlider和JRadioButton等组件。我从JPanel派生了一个类,并重写了方法paintComponent,如您所见。这是在JPanel上创建图像的好方法 public void paintComponent(Graphics g) { /*create image icon to get image*/ ImageIcon imageicon = new ImageIcon(imageFile); //get
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之类的组件时;此组件的背景保留为默认,而不是背景图片。我不知道如何将此图像设置为此组件的背景。
请引导我
关于是否设置不透明(false)对于所有其他组件帮助?您必须将其他组件的不透明属性设置为false
jRadioButton.setOpaque(false);
例如:
jRadioButton.setOpaque(false);
将适用于多种外观,但如果您希望它与Nimbus配合使用,还应将背景色设置为透明:
jRadioButton.setBackground(new Color(0,0,0,0));
有关更多详细信息,请参阅