欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

在文件选择器中显示快捷方式,选择器显示快捷方式,class Shortc

来源: javaer 分享于  点击 279 次 点评:78

在文件选择器中显示快捷方式,选择器显示快捷方式,class Shortc


class ShortcutFileView extends FileView {        public boolean isDirLink(File f) {                 if(f.getName().toLowerCase().endsWith(".lnk")) {            return true;        }        return false;    }    public Boolean isTraversable(File f) {        if(isDirLink(f)) {            return new Boolean(true);        }        return null;     }    public Icon getIcon(File f) {        if(isDirLink(f)) {            JFileChooser chooser = new JFileChooser();            FileChooserUI fcui = (FileChooserUI) UIManager.getUI(chooser);            fcui.installUI(chooser);            FileView def = fcui.getFileView(chooser);//Once you have the file view, you need to pull out a folder icon. You can do this by asking for the icon of a known folder, in this case C:\\windows:    // get the standard icon for a folder    File tmp = new File("C:\\\\windows");    Icon folder = def.getIcon(tmp);    int w = folder.getIconWidth();    int h = folder.getIconHeight();//Once you have the icon, you can build a new image to draw it on, and then overlay the link graphic:    // create a buffered image the same size as the icon     Image img = new BufferedImage(w,h,                BufferedImage.TYPE_4BYTE_ABGR);     Graphics g = img.getGraphics();    // draw the normal icon    folder.paintIcon(chooser,g,0,0);    // draw the shortcut image on top of the icon            Image shortcut = new ImageIcon("shortcut.png").getImage();     g.drawImage(shortcut,0,0,null);    // clean up and return    g.dispose();    return new ImageIcon(img);   }   return super.getIcon(f); }  }    public class DisplayShortcutTest {        public static void main(String[] args) throws Exception {            JFileChooser chooser = new JFileChooser();            chooser.setFileView(new ShortcutFileView());            chooser.showOpenDialog(null);        }    }//该片段来自于http://byrx.net
相关栏目:

用户点评