複製程式碼如下:
導入java.io.File;
導入java.io.IOException;
導入 java.awt.BorderLayout;
導入 java.awt.event.ActionEvent;
導入 java.awt.event.ActionListener;
導入 javax.swing.Icon;
導入 javax.swing.ImageIcon;
導入 javax.swing.JButton;
導入 javax.swing.JFrame;
導入javax.swing.JPanel;
導入 javax.swing.JTextField;
導入 javax.swing.JLabel;
導入 javax.swing.BoxLayout;
導入 javax.swing.filechooser.FileSystemView;
導入 sun.awt.shell.ShellFolder;
公共類別 FileIconExtractor 擴充 JFrame 實作 ActionListener{
private JButton getIconBtn = new JButton("取得圖示");
私有 JPanel iconPanel = new JPanel();
私有 JTextField extField = new JTextField();
private JLabelsmallIconLabel = new JLabel("這裡是小圖示");
private JLabel bigIconLabel = new JLabel("此處為大圖示");
公共文件圖示提取器(){
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);
}
公共無效actionPerformed(ActionEvent e){
if (e.getActionCommand().equals("GETICON")) {
字串 ext = extField.getText();
文件檔案;
嘗試
{
file = File.createTempFile("icon", "." + ext);
FileSystemView 檢視 = FileSystemView.getFileSystemView();
圖示smallIcon = view.getSystemIcon(file);
ShellFolder shellFolder = ShellFolder.getShellFolder(檔案);
圖示 bigIcon = new ImageIcon(shellFolder.getIcon(true));
setIconLabel(小圖示, 大圖示);
文件.刪除();
}
捕獲(IOException ioe)
{
ioe.printStackTrace();
}
}
}
私人無效 setIconLabel(圖標小圖標,圖標大圖標){
小圖標標籤.setIcon(小圖標);
bigIconLabel.setIcon(bigIcon);
}
公共靜態無效主(字串[] args){
FileIconExtractor fie = new FileIconExtractor();
}
}