简体   繁体   English

为JFrame制作自定义图标

[英]Making a custom icon for a JFrame

Well I was wondering if I could make an icon image for a JFrame. 好吧,我想知道是否可以为JFrame制作图标图像。 I do know its posible, because, let me say, I am NOT digging the java logo. 我知道它的可能性,因为,我说,我不是在挖掘Java徽标。

Well if I just hava to use a Frame object I will. 好吧,如果我只是想使用Frame对象,我会的。

Can someone tell me, I know its possible! 有人可以告诉我,我知道它有可能!

Use an ImageIcon. 使用ImageIcon。

ImageIcon icon = new ImageIcon( pathToIcon );
yourFrame.setIconImage(icon.getImage());

Good Luck! 祝好运!

First, you have to have an image file on your computer. 首先,您必须在计算机上有一个图像文件。 It can be named anything. 可以命名任何东西。 For this example, we will call this one "pic.jpg". 对于此示例,我们将其称为“ pic.jpg”。 Next, you need to include it in the files that your application is using. 接下来,您需要将其包含在应用程序正在使用的文件中。 For example, if you're using NetBeans, you simply click on "Files" in the left hand side of the IDE (not File as in the menu, mind you). 例如,如果您使用的是NetBeans,则只需单击IDE左侧的“文件”(请注意,不是菜单中的“文件”)。 Drag the picture's file over to the folder that houses the main package. 将图片的文件拖到主程序包所在的文件夹中。 This will include it for available use in the code. 这将包括它以供代码使用。 Inside the method where you define the JFrame, you can create an image like this: 在定义JFrame的方法内部,可以创建如下图像:

Image frameImage = new ImageIcon("pic.jpg").getImage();

You can now set it as the IconImage for the frame like this: 您现在可以像这样将其设置为框架的IconImage:

JFrame frame = new JFrame("Title");
frame.setIconImage(frameImage);

Hope that's helpful. 希望对您有所帮助。

Note: the reason that the Image object has to be created like this is because Image is abstract and cannot be instantiated by saying new Image(); 注意:之所以必须这样创建Image对象,是因为Image是抽象的,无法通过说new Image()实例化。

Props to you, btw, kid. 顺便说一句,孩子。 I wish I would have started learning programming when I was your age. 我希望我在你这个年龄的时候就可以开始学习编程。 Keep at it! 继续吧!

You can do the following. 您可以执行以下操作。

    public Test {
   public static void main(String[] args) {
     JFrame frame = new JFrame("My Frame");
     frame.setIconImage(new ImageIcon(Test.class.getResource("image.png"));
     frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);                                                  
     frame.setVisible(true);
     frame.setSize(100, 100);               

     //other stuffs....
   }  
}

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

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