简体   繁体   English

使用img background显示GUI组件时出现问题

[英]Problems displaying GUI components with img background

I've added a background to my Java Applet, I need some help understanding why the applet isn't displaying properly. 我已经为我的Java Applet添加了一个背景,我需要一些帮助来理解为什么applet没有正确显示。 To display this background image I've used the code seen below: 为了显示这个背景图像,我使用了下面的代码:

BufferedImage img = null;

try {
            URL url = new URL(getCodeBase(), "Backgrounds/Background.png");
            img = ImageIO.read(url);
        }
        catch (Exception e) {

        }

then also put this in the paint method... 然后还把它放在油漆方法中......

public void paint(Graphics g) {
    g.drawImage(img, 0, 0, null);
}

The problem is that you cannot see the GUI components such as buttons and labels when the background is painted, even though the background is painted before the other GUI components are added to the content pane. 问题是,在绘制背景时,您无法看到GUI组件(如按钮和标签),即使在将其他GUI组件添加到内容窗格之前绘制了背景。 It is possible to get the components to appear but you have to highlight them or click on them first. 可以显示组件,但必须先突出显示它们或单击它们。

This picture shows the applet when the applet is loaded: 此图显示了加载applet时的applet:

在此输入图像描述

Then this is the applet after I have clicked in a few places on screen: 然后这是我在屏幕上的几个地方点击后的applet:

在此输入图像描述

public void paint(Graphics g) {
    g.drawImage(img, 0, 0, null);
}

Firstly, that method call should be: 首先,该方法调用应该是:

public void paint(Graphics g) {
    g.drawImage(img, 0, 0, this); // Every Component is an image observer
}

Then, to fix the broken paint chain: 然后,修复破损的油漆链:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM