如何为swing应用设置图标图像? [英] How to set icon image for swing application?

查看:495
本文介绍了如何为swing应用设置图标图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多不同的例子,展示了如何设置JFrame的 IconImage ,以便应用程序使用该图标而不是标准咖啡杯。他们都没有为我工作。

I've seen many different examples showing how to set a JFrame's IconImage so that the application uses that icon instead of the standard coffee mug. None of them are working for me.

这是我的代码(大量借用其他帖子和互联网):

Here's "my" code (heavily borrowed from other posts and the internet at large):

public class MyApp extends JFrame
{
    public MyApp()
    {
        ImageIcon myAppImage = loadIcon("myimage.jpg");
        if(myAppImage != null)
            setIconImage(myAppImage.getImage());
    }

    private ImageIcon loadIcon(String strPath)
    {
        URL imgURL = getResource(strPath);
        if(imgURL != null)
            return new ImageIcon(imgURL);
        else
            return null;
    }
}

此代码在中失败load_con 在调用 getResource()方法时。对我来说,这里只有两种可能:(1) myImage.jpg 位于错误的目录中,或者(2) getResource()不喜欢我的图像(我必须在Photoshop中将它从CMYK转换为RGB,这样我就可以在其他地方用 ImageIO 使用相同的图像。)

This code fails down in loadIcon when making a call to the getResource() method. To me, there's only 2 possibilities here: (1) the myImage.jpg is in the wrong directory, or (2) getResource() doesn't like something about my image (I had to convert it from CMYK to RGB in Photoshop so I could use the same image elsewhere with ImageIO.)

我使用了 System.out.println(新文件(。)。getAbsolutePath()); 找到应该存储图像JPG的目录的技巧,但仍然没有任何效果。我随后将JPG放在我项目中的每个目录中,只是将文件位置作为罪魁祸首。

I have used the System.out.println(new File(".").getAbsolutePath()); trick to locate the directory where the image JPG should be stored, and still nothing worked. I have subsequently placed the JPG in just about every directory inside my project, just to rule file location out as the culprit.

这让我相信有些东西 getResource()不喜欢JPG本身。但是现在我已经厌倦了对强大,广阔的Swing世界中的图像和图标的理解。

So that leaves me to believe there's something that getResource() doesn't like about the JPG itself. But I have now already exhausted my understanding of images and icons in the mighty, wide world of Swing.

我的JPG在其他图像查看器中加载得很好,因此被排除在外好。有什么想法吗?

My JPG loads fine in other image viewers, so that's ruled out as well. Anyone have any ideas?

提前致谢!

推荐答案

确定我想到了。出于某种原因,显然您需要为要与其他单元格不同的特定单元格添加特殊条件。观察我的原始代码:

OK I figured it out. For some reason apparently you need to add a special condition for the specific cells you want to make different from the others. Observe my original code:

if(row == 2 && column == 2) 
    c.setBackground(new java.awt.Color(0, 0, 255)); 
    else 
        c.setBackground(table.getBackground()); 

这不起作用,因为我需要将它包装在条件中。

This didn't work, because I needed to wrap it in a conditional.

if (!table.isRowSelected(row)) 
{ 
    if(row == 2 && column == 2) 
        c.setBackground(new java.awt.Color(0, 0, 255)); 
    else 
        c.setBackground(table.getBackground()); 
} 

这真是太棒了!

这篇关于如何为swing应用设置图标图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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