我把这段代码直接从“Java一体机”中的Dummies ... ....为什么不工作? [英] I took this code straight out of 'Java all in one for Dummies' ....why doesn't it work?

查看:118
本文介绍了我把这段代码直接从“Java一体机”中的Dummies ... ....为什么不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


@SuppressWarnings("serial")
public class Picture extends JFrame{

    public static void main(String[] args){

        new Picture();

    }

    public Picture(){

        this.setTitle("Picture");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new JPanel();
        ImageIcon pic = new ImageIcon("TestImage.jpg");
        p.add(new JLabel(pic));
        this.add(p);
        this.pack();
        this.setVisible(true);
        System.out.println(pic);
    }

}

这段代码只是显示一个折叠的框架。我会期望pack();方法调用会使我的.jpg图像周围的帧大小正确吗?

This code as it is just displays a collapsed frame. I would expect that the pack(); method call would size the frame around my .jpg image, correct?

我可以听到第一个问题,是的,图像与我的项目包文件夹在同一个文件夹中。

I can already hear the first question, and yes the image is in the same folder as my project package folder.

我正在使用Eclipse IDE。

I am using the Eclipse IDE.

我还添加了println();声明,以查看图像引用返回,并返回TestImage.jpg。
我以为会在内存中返回图像的地址。

I also added the println(); statement to see what the Image reference returned and it returns "TestImage.jpg". I thought it would return an address of the image in memory.??

我一直在阅读论坛,我找不到任何东西这有助于我在那里没有复制和使用别人的更复杂的代码。
我想尽可能的简单,以便我不要混淆自己。

I have been reading the forum all morning and I can't really find anything that helps me there without copying and using someone else's more complex code. I am trying to keep this as simple as I can so that I don't confuse myself.

提前感谢任何帮助。

推荐答案


图像与我的项目包文件夹在同一个文件夹中

the image is in the same folder as my project package folder

就是这样。

正如你所写,你的程序在当前工作目录,而不是包层次结构。

As written, your program looks for the image in the current working directory, not the package hierarchy.

构造函数采用 String 的Javadoc,它读取图像从指定的文件名,根据需要。但是,当您指定相对路径时,这意味着相对于应用程序正在运行的工作目录进行读取。

From the Javadoc for the constructor taking String, it reads the image from the specified filename, as desired. However, when you specify a relative path, that means to read relative to the working directory that the application is running in.

要解决此问题,您有两个选项:

To work around this, you have 2 options:


  • 指定相对于IDE运行程序的工作目录的映像文件名。我相信Eclipse会在项目根目录中运行应用程序目录,源包层次结构根植于 src 。在这种情况下,如果您指定 src / TestImage.jpg ,它将会起作用。缺点是,如果您从不同的目录运行程序,则必须随着它移动图像文件。这对于分发/打包来说是不方便的,因为您不能只是放下JAR文件并运行它。

  • Specify the image filename relative to the working directory your IDE runs your program in. I believe Eclipse runs applications in the project root directory, and the source package hierarchy is rooted at src. In this case it'll work if you specify src/TestImage.jpg. The disadvantage is that if you ever run your program from a different directory, you'll have to move the image file along with it. This is inconvenient for distribution/packaging, because you can't just drop the JAR file and have it run.

使用Java的资源加载器加载映像文件从包层次结构。要这样做,首先使用

Use Java's resource loader to load the image file from the package hierarchy. To do this, first use

getClass().getResource("TestImage.jpg")

获取图像的URL(相对于包根)。请参阅 ImageIcon 具有接受URL的构造函数,以便从中读取图像。所以你应该使用

to get a URL for the image (relative to the package root). See that ImageIcon has a constructor that accepts a URL to read the image from. So you should read the image using

new ImageIcon(getClass().getResource("TestImage.jpg"))

相对于他的包层次结构的优点是程序可以从任何位置运行,图像可以在单个JAR文件中与您的应用程序捆绑在一起。

The advantage of being relative to he package hierarchy is that the program can be run from any location, and the image can be bundled with your app in a single JAR file.

Aside:最佳做法是创建一个可以将代码和资源放在一起的软件包(而不仅仅是将它们放在包根)。在这种情况下,通过com / example / someapp / TestImage.jpg

Aside: it's best practice to create a package in which you place both code and resources (rather than just placing them in the package root). In that case, pass "com/example/someapp/TestImage.jpg" instead.

这篇关于我把这段代码直接从“Java一体机”中的Dummies ... ....为什么不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆