为什么我的Icon在JFrame中没有改变?

时间:2014-08-10 01:47:04

标签: java swing jframe

这是我写的java中的代码。附上了我的笔记本电脑的屏幕截图,其中显示了左侧角上仍然存在java图标的输出。

import javax.swing.*;![enter image description here][1]
import java.awt.*;

public class Frame
{
    public static void createWindow()
    {
        JFrame frame = new JFrame("Warning");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel textLabel = new JLabel ("Congratulation!! Installation Complete.",      SwingConstants.CENTER);
        textLabel.setPreferredSize(new Dimension(420,140));
        frame.getContentPane().add(textLabel, BorderLayout.CENTER);
ImageIcon img = new ImageIcon("D:\\Icons\\icon.ico");
        frame.setIconImage(img.getImage());


        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    }

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

}

2 个答案:

答案 0 :(得分:1)

..Icons\\icon.ico

有问题的JVM是否支持 .ico文件?建议坚持使用PNG,GIF和JPEG。以下是在Windows 7计算机上使用Java 1.8支持的文件类型的 1 列表。

Reader  jpg
Reader  bmp
Reader  gif
Reader  png
Reader  jpeg
Reader  wbmp
Writer  jpg
Writer  bmp
Writer  gif
Writer  png
Writer  wbmp
Writer  jpeg

没有列出.ico ..

  1. this answer中看到的MediaTypes代码获取的信息。

答案 1 :(得分:0)

如果链接不能正常使用,请尝试使用以下内容。它对我有用:

使用 setIconImage(...)。

import java.awt.Image;
import javax.swing.*;
import javax.imageio.ImageIO;

import java.net.URL;
import java.util.*;
//upto here just importing. Don't worry, let the eclipse or your editor do that for you
class FrameIcons {

    public static void main(String[] args) throws Exception {
        URL url16 = new URL("image.png");//Making two objects of URL class
        URL url32 = new URL("images.png");//second object of URL

        final List<Image> icons = new ArrayList<Image>();//this is just an arraylist of `icon. For now on if you don't know about list, think it as array where you can put element using .add() method`
        icons.add(ImageIO.read(url16));//adding to list
        icons.add(ImageIO.read(url32));//adding to list

        SwingUtilities.invokeLater( new Runnable() {//this is just for running a `runnable`
            public void run() {
                JFrame f = new JFrame("Frame Icons");//setting JFrame
                f.setIconImages(icons);//setting icon images
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//you must know `everything from here`
                f.setLocationByPlatform(true);
                f.setSize(200,100);
                f.setVisible(true);
            }
        });
    }
}