[英]Problems displaying GUI components with img background
我已经为我的Java Applet添加了一个背景,我需要一些帮助来理解为什么applet没有正确显示。 为了显示这个背景图像,我使用了下面的代码:
BufferedImage img = null;
try {
URL url = new URL(getCodeBase(), "Backgrounds/Background.png");
img = ImageIO.read(url);
}
catch (Exception e) {
}
然后还把它放在油漆方法中......
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
}
问题是,在绘制背景时,您无法看到GUI组件(如按钮和标签),即使在将其他GUI组件添加到内容窗格之前绘制了背景。 可以显示组件,但必须先突出显示它们或单击它们。
此图显示了加载applet时的applet:
然后这是我在屏幕上的几个地方点击后的applet:
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
}
首先,该方法调用应该是:
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this); // Every Component is an image observer
}
然后,修复破损的油漆链:
public void paint(Graphics g) {
super.paint(g); // Draw the rest of the components!
g.drawImage(img, 0, 0, this); // Every Component is an image observer
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.