简体   繁体   English

我该如何更改程序图标

[英]how can I change the programs icon

I tried looking up multiple tutorials on how to change the programs icon in java but none of them seem to work. 我尝试查找有关如何更改java中的程序图标的多个教程,但它们似乎都不起作用。 I was also wondering what kind of image can it be .ico or .png eg and what size it had to be 我也想知道它是什么样的图像.ico或.png例如它的大小

I assume you're using a JFrame , then you want setIconImage(java.awt.Image) : 我假设您正在使用JFrame ,那么您需要setIconImage(java.awt.Image)

File/InputStream/URL x = ...
Image icon = ImageIO.read(x);
frame.setIconImage(icon);

The image file format is irrelevant as long as ImageIO can read it (JPEG, GIF, PNG, TIFF, even BMP if really really necessary). 只要ImageIO可以读取它(JPEG,GIF,PNG,TIFF,甚至BMP,如果真的非常必要),图像文件格式就无关紧要了。

An example: 一个例子:

import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Main {

    public static void main(final String[] args) throws Exception {
        JFrame frame = new JFrame("Custom Icon");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);

        // something off the Google Images front page
        final URL url = new URL("http://t3.gstatic.com/images?q=tbn:ANd9GcQbcDkaRcrbsYFUcE6Q7n56_LJr-r4mDqYTOTtPKG9J0MzZcV6V");
        frame.setIconImage(ImageIO.read(url));

        frame.setVisible(true);
    }

}

Edit: How to get a URL to your icon 编辑:如何获取图标的URL

You should place your image files (icons, etc) along with your source code, maybe into its own sub folder in your src folder and access them as "resource". 您应该将图像文件(图标等)与源代码一起放入src文件夹中的子文件夹中,并将其作为“资源”访问。 Check out How to use Icons from the Java Tutorial for how to get a URL object to a resource. 查看如何使用 Java教程中的图标来获取如何将URL对象获取到资源。

I think @Philipp made it quite clear, that x has to be a File , InputStream or URL , not a String as it seems you tried. 我认为@Philipp非常清楚, x必须是FileInputStreamURL ,而不是你想要的String

Now, you also need to have a Frame or JFrame to have an icon! 现在,您还需要一个FrameJFrame来拥有一个图标!

I presume this is in your main method. 我认为这是你的main方法。 To create a frame try 要创建一个框架尝试

JFrame frame=new JFrame();
frame.setVisible(true);

followed by @Philipp's code. 然后是@ Philipp的代码。

-- -

It just occured to me you might mean the icon of the program shortcut in your operating system. 它只是发生在我身上你可能意味着操作系统中程序快捷方式的图标。 This isn't done in Java but in your operating system. 这不是在Java中完成的,而是在您的操作系统中完成的。 unless you use Java Web start 除非你使用Java Web start

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

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