简体   繁体   English

为什么这个小程序不能工作/显示图像?

[英]why doesn't this applet work/display the image?

I am new to Applets, and am trying to get comfortable with some basics - like how to display a jpg image in one.我是 Applets 的新手,并且正在尝试熟悉一些基础知识——比如如何在一张图片中显示 jpg 图像。 I've read what I think are the relevant parts of the Java Tutorials on Applets, and I don't understand why this doesn't work, all I get is a blank applet area.我已经阅读了我认为 Java 小程序教程的相关部分,我不明白为什么这不起作用,我得到的只是一个空白的小程序区域。 "/cards/as.jpg" - that file exists in subdirectory of the directory the applet is running from. “/cards/as.jpg” - 该文件存在于运行小程序的目录的子目录中。 Can anyone see what I'm doing wrong?谁能看到我做错了什么? I'm at a loss.我不知所措。

import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestApplet extends JApplet {
    public void init() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    createGUI();
                }
            });
        }
        catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }

    private void createGUI() {
        JPanel newContentPane = new JPanel();

        ImageComponent card = new ImageComponent();
        card.setImage(getCodeBase() + "/cards/as.jpg");

        newContentPane.add(card);        

        newContentPane.setOpaque(true);
        setContentPane(newContentPane);
    }
}


import java.awt.*;
import javax.swing.JComponent;
import javax.imageio.*;
import java.net.*;
import java.io.*;

/**
   A component that draws an Image.
*/
public class ImageComponent extends JComponent {  
    private Image image;
    private String url;

    public ImageComponent() {
        image = null;
        url = null;
    }

    public void setImage(String urlCardName) {
        url = urlCardName;
        if (url != null) {
            try {
                image = ImageIO.read(new URL(url));
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        else {
            image = null;
        }
        this.repaint();
    }

    public void paintComponent(Graphics g) { 
        if (image == null) return;
        // draw the images in the upper-left corner of the component
        g.drawImage(image, 0, 0, null);
    }
}

I think it's your layouts.我认为这是你的布局。 Normally a contentPane uses BorderLayout by default, but when you use your own JPanel as contentPane, you're using the JPanel default layout which is FlowLayout, and components added to a container using FlowLayout won't expand to fill the container as they would with BorderLayout.通常 contentPane 默认使用 BorderLayout,但是当您使用自己的 JPanel 作为 contentPane 时,您使用的是 JPanel 默认布局,即 FlowLayout,并且使用 FlowLayout 添加到容器的组件不会像使用那样扩展以填充容器边框布局。 Check to see how large your ImageComponent is when rendered -- I'll bet you it's very small, 0 size even.检查您的 ImageComponent 在渲染时有多大——我敢打赌它非常小,甚至是 0 大小。

A solution is to use the contentPane that's already present in the applet with its default BorderLayout, or else if you must use your own JPanel, give it a BorderLayout and add the new ImageComponent BorderLayout.CENTER.一种解决方案是使用 applet 中已经存在的 contentPane 及其默认 BorderLayout,或者如果您必须使用自己的 JPanel,则为其提供 BorderLayout 并添加新的 ImageComponent BorderLayout.CENTER。 For example:例如:

    JPanel newContentPane = new JPanel();

    newContentPane.setLayout(new BorderLayout()); // *** added this

    ImageComponent card = new ImageComponent();
    card.setImage(getCodeBase() + "/cards/as.jpg");

    newContentPane.add(card, BorderLayout.CENTER);  // *** changed

Check to see how large your ImageComponent is when rendered -- I'll bet you it's very small, 0 size even.检查您的 ImageComponent 在渲染时有多大——我敢打赌它非常小,甚至是 0 大小。

This is because you never set a preferred size for you component.这是因为您从未为组件设置首选大小。

The real question is why are you drawing the image yourself.真正的问题是您为什么要自己绘制图像。 There is no need to create a custom component.无需创建自定义组件。 Just use a JLabel with a Icon and this would not be a problem.只需使用带有图标的 JLabel,这不会有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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