java根據(jù)擴展名獲取系統(tǒng)圖標和文件圖標示例
import java.io.File;
import java.io.IOException;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.filechooser.FileSystemView;
import sun.awt.shell.ShellFolder;
public class FileIconExtractor extends JFrame implements ActionListener{
private JButton getIconBtn = new JButton("get Icon");
private JPanel iconPanel = new JPanel();
private JTextField extField = new JTextField();
private JLabel smallIconLabel = new JLabel("small Icon here");
private JLabel bigIconLabel = new JLabel("big Icon here");
public FileIconExtractor() {
this.setSize(200, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
getIconBtn.setActionCommand("GETICON");
getIconBtn.addActionListener(this);
iconPanel.setLayout(new BoxLayout(iconPanel, BoxLayout.Y_AXIS));
iconPanel.add(smallIconLabel);
iconPanel.add(bigIconLabel);
this.add(extField, BorderLayout.NORTH);
this.add(iconPanel, BorderLayout.CENTER);
this.add(getIconBtn, BorderLayout.SOUTH);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("GETICON")) {
String ext = extField.getText();
File file;
try
{
file = File.createTempFile("icon", "." + ext);
FileSystemView view = FileSystemView.getFileSystemView();
Icon smallIcon = view.getSystemIcon(file);
ShellFolder shellFolder = ShellFolder.getShellFolder(file);
Icon bigIcon = new ImageIcon(shellFolder.getIcon(true));
setIconLabel(smallIcon, bigIcon);
file.delete();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
private void setIconLabel(Icon smallIcon, Icon bigIcon) {
smallIconLabel.setIcon(smallIcon);
bigIconLabel.setIcon(bigIcon);
}
public static void main(String[] args) {
FileIconExtractor fie = new FileIconExtractor();
}
}
相關文章
JAVA使用hutool工具實現(xiàn)查詢樹結構數(shù)據(jù)(省市區(qū))
今天通過本文給大家分享JAVA使用hutool工具實現(xiàn)查詢樹結構數(shù)據(jù)(省市區(qū)),代碼分為表結構和數(shù)據(jù)結構,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-08-08
Java利用剪貼板實現(xiàn)交換程序間數(shù)據(jù)的方法
這篇文章主要介紹了Java利用剪貼板實現(xiàn)交換程序間數(shù)據(jù)的方法,需要的朋友可以參考下2014-07-07
java微信開發(fā)API第三步 微信獲取以及保存接口調(diào)用憑證
這篇文章主要為大家詳細介紹了java微信開發(fā)API第二步,微信獲取以及保存接口調(diào)用憑證,感興趣的小伙伴們可以參考一下2016-06-06
數(shù)組和二維數(shù)組感覺用王者榮耀的裝備欄來舉例解釋,應該更易懂一些。從基礎開始講,后續(xù)會講到JAVA高級,中間會穿插面試題和項目實戰(zhàn),希望能給大家?guī)韼椭?/div> 2022-03-03最新評論

