我的图像就像一个中间透明的框架。我想将其作为 JPanel 的背景图像。我已经这样做了,但是对于图像的透明部分,白色即将到来。我想删除这个白色,以便这个 Jpanel 下面的组件变得可见。
我的自定义 JPanel 代码是
public class JPanelWithBackground extends JPanel {
private static final long serialVersionUID = 1L;
Image imageOrg = null;
Image image = null;
{
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
int w = JPanelWithBackground.this.getWidth();
int h = JPanelWithBackground.this.getHeight();
image = w>0&&h>0?imageOrg.getScaledInstance(w,h,
java.awt.Image.SCALE_SMOOTH):imageOrg;
JPanelWithBackground.this.repaint();
});
}
public JPanelWithBackground(Image image2) {
imageOrg=image2;
image=image2;
setOpaque(false);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image!=null)
g.drawImage(image, 0, 0, null);
}
}