ImageIcon方法不适用于所有图像 [英] ImageIcon method not working for all images

查看:101
本文介绍了ImageIcon方法不适用于所有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取一个jpeg显示在JFrame上,并且我注意到有些图像文件可以工作,而另一些则不能.我正在使用Eclipse,我的所有图像都在项目的根目录中.当我运行调试器并击中断点时,它将在不会显示的图像上将图像高度和图像宽度报告为-1.它会在显示的图像上报告正确的值.

I'm trying to get a jpeg to display on a JFrame and I'm noticing that some image files work while others do not. I'm using Eclipse and all of my images are in the project's root dir. When I run the Debugger and it hits my breakpoint it reports the imageheight and imagewidth as -1 on the images that will not display. It reports the correct values on the images that do display.

起初,我认为这与图像大小有关,但是在使用mspaint中的分辨率播放后,我意识到情况并非如此.

at first I thought it had something to do with the image sizes but after playing with resolutions in mspaint I've realized that this is not the case.

到目前为止,这是我的代码:

Here is my code so far:

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.*;
import java.io.File;

public class icon {

    public static void main(String[] args) {
        constructVNCForm();
    }

public static void constructVNCForm() {
        //Vars for GUI and essential layout code
        final JFrame frame = new JFrame("This should be on top");
        frame.setSize(800, 640);
        Container content = frame.getContentPane();
        frame.setVisible(true);

        //image code here:
        ImageIcon image = new ImageIcon("trial4.jpg");
        //ABOVE WORKS

        JLabel imageLabel = new JLabel(image);
        FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
        //add the image to the frame
        content.add(imageLabel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        content.setLayout(flow);
    }
}

你们中的任何人对ImageIcons都有任何疑问吗?我所有的jpeg尺寸都在300x300以下.我是Java的新手,所以如果您对我的代码有任何建议,请提出建议.

Have any of you had any issues with ImageIcons? all of my jpegs are various sizes under 300x300. I'm kind of new to Java so if you have any suggestions on my code please advise.

推荐答案

在测试了几种不同大小的图像后,以下工作正常.通常setVisible(true)方法应在结尾处调用.

Tested with few various size of images, the following works fine. Usually setVisible(true) method should be called at the end.

public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("This should be on top");
                frame.setSize(800, 640);
                ImageIcon image = new ImageIcon("someImage.jpg");
                JLabel imageLabel = new JLabel(image);
                FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
                frame.getContentPane().add(imageLabel);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(flow);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

这篇关于ImageIcon方法不适用于所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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