Swinghacks——获取系统图标

首先,java是不支持ico图标的

例如,我的D盘有一个a.pdf文件,操作系统默认显示了图标,如图


当然,这个a.pdf是我新建的a.txt然后改后缀得到的

现在我想在java中也获取到这个pdf的图标,效果如下:


这样的话,我们没有必要准备一系列的图标了,需要哪个文件类型的系统图标,临时建一个空文件,获取图标即可

当然,也可以把获取到的图标保存成java能识别的文件类型

上代码:

import javax.swing.*;
import java.io.*;

public class LargeIconTest {

    public static void main(String[] args) throws Exception {

        // Create a File instance of an existing file
        File file = new File("D:/a.pdf");
        
        // Get large icon
        sun.awt.shell.ShellFolder sf = sun.awt.shell.ShellFolder.getShellFolder(file);
        Icon icon = new ImageIcon(sf.getIcon(true));
        System.out.println("type = " + sf.getFolderType());
        
        // show the icon
        JLabel label = new JLabel(icon);
        JFrame frame = new JFrame();
        frame.getContentPane().add(label);
        frame.pack();
        frame.show();
    
    }

}

当然,还有一种简单方法,不过获取到的是小图标

    	FileSystemView fsv=new JFileChooser().getFileSystemView();
    	Icon icon=fsv.getSystemIcon(new File("D:/a.pdf"));



评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值