我已经为我的 Java Applet 添加了背景,我需要一些帮助来理解为什么 applet 不能正确显示。为了显示这个背景图片,我使用了下面的代码:
BufferedImage img = null;
try {
URL url = new URL(getCodeBase(), "Backgrounds/Background.png");
img = ImageIO.read(url);
}
catch (Exception e) {
}
然后也把它放在 paint 方法中......
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
}
问题是在绘制背景时您看不到按钮和标签等 GUI 组件,即使背景是在将其他 GUI 组件添加到内容 Pane 之前绘制的。可以让组件出现,但您必须先突出显示它们或单击它们。
这张图片显示了小程序加载时的小程序:
然后这是我在屏幕上的几个地方点击之后的小程序:
最佳答案
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
}
关于java - 使用图像背景显示 GUI 组件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29659280/