简体   繁体   English

在JFrame中设置图标

[英]Set icon in JFrame

I want to change the icon of the project instead of java icon. 我想更改项目的图标而不是Java图标。 When the program icon is being displayed in status bar, it should be displaying the customized icon instead of default java icon. 当程序图标显示在状态栏中时,它应该显示自定义图标,而不是默认的Java图标。

I am using the following code. 我正在使用以下代码。 Please suggest me what's wrong in this code. 请建议我这段代码有什么问题。

class newframe extends JFrame 
{

    Container cp;
    newframe()
    {
        cp=this.getContentPane();
        cp.setLayout(null);
    }

    public static void main(String args[])
    {
        newframe frm= new newframe(); 

        frm.setbounds(0,0,1000,800);
        frm.setVisible(true);
        ImageIcon im1= new ImageIcon("path upto image");
        frm.setIconImage(im1.getImage());
    }
}
..new ImageIcon("path upto image"); 

框架图标通常是嵌入式资源,因此必须通过URL而不是File (代表路径的String )访问。

There are a couple of things that would be keeping it from compiling. 有几件事会使它无法编译。 First: 第一:

frm.setbounds(0,0,1000,800);

Your "setbounds" should have a capital B. Typically, functions will be cased such that the first letter of the first word is lowercased, and subsequent words are upper-cased. 您的“设定界限”应以大写字母B开头。通常,将对函数进行大小写转换,以使第一个单词的第一个字母小写,随后的单词大写。 See this link for the doc on setBounds: setBounds 有关setBounds的文档,请参见此链接: setBounds

There's a second issue in your ImageIcon path. 您的ImageIcon路径中还有第二个问题。 Its hard to say if that came right from your code or if you removed the path for the sake of the example, but Andrew Thompson has addressed that adequately. 很难说这是源于您的代码,还是为了示例而删除了路径,但是Andrew Thompson已充分解决了这一问题。

I think the problem is the decleration of the imageicon. 我认为问题在于图像图标的清晰化。 What you should do is instead of getting the direct path, do something like this: 您应该做的不是代替直接的路径,而是做这样的事情:

ImageIcon im1= new ImageIcon("Toolkit.getDefaultToolkit(). getImage(getClass().getResource("path upto image"))");
I do this with all of my applications, and it works every time. 我对所有应用程序都执行此操作,并且每次都有效。

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

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