gpt4 book ai didi

java - 使用图像背景显示 GUI 组件时出现问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:44:11 27 4
gpt4 key购买 nike

我已经为我的 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 之前绘制的。可以让组件出现,但您必须先突出显示它们或单击它们。

这张图片显示了小程序加载时的小程序:

enter image description here

然后这是我在屏幕上的几个地方点击之后的小程序:

enter image description here

最佳答案

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com