我使用 ImageIO.read
获取图像 (BackgroundImage
) 并像这样绘制背景图像:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background, 0, 0, backDim.width, backDim.height, null);
}
然后我在那个 JPanel
上添加了一些组件(插入其中的面板的背景颜色是 new Color(0,0,0,0)
(透明)。
启动时背景图片正确显示;但是,当我将它拖到屏幕的底部边缘时,其中一半被删除了。当我将它拖到屏幕的左边缘或右边缘时,它会被重新绘制。
啊,当我删除 Box.createRigidArea()
间隙时,只有标题的背景才能正确显示。其他空间只是 JPanel 的默认颜色(浅灰色)。
编辑:我添加了一个组件监听器,以便在每次移动窗口时绘制屏幕,但它会频繁调用重绘,因此计算机可能会过载。还有其他解决方案吗?
编辑:问题是当我移动窗口时,部分(或大部分)绘制图像被删除(其中包含带有背景图像的嵌套 JPanel
)到屏幕底部并将其拖回。但是,不会调用 repaint()
。
最终编辑:已解决。这是因为我没有调用 setOpaque(false);
最佳答案
background color of the panels inserted into it is new Color(0,0,0,0) (Transparent)
JPanel 的不透明度默认为真。如果设置透明背景色,则需要将不透明度设置为 false - 否则会出现绘画伪像(如您所见)
panel.setBackground(transparentColor);
panel.setOpaque(false);
关于java - 嵌套的 JPanel 的背景图像未正确重新绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17448973/