复制代码代码如下:
importar java.io.File;
importar java.io.IOException;
importar java.awt.BorderLayout;
importar java.awt.event.ActionEvent;
importar java.awt.event.ActionListener;
importar javax.swing.Icon;
importar javax.swing.ImageIcon;
importar javax.swing.JButton;
importar javax.swing.JFrame;
importar javax.swing.JPanel;
importar javax.swing.JTextField;
importar javax.swing.JLabel;
importar javax.swing.BoxLayout;
importar javax.swing.filechooser.FileSystemView;
importar sun.awt.shell.ShellFolder;
classe pública FileIconExtractor estende JFrame implementa ActionListener{
private JButton getIconBtn = new JButton("obter ícone");
JPanel privado iconPanel = new JPanel();
private JTextField extField = new JTextField();
private JLabel smallIconLabel = new JLabel("ícone pequeno aqui");
private JLabel bigIconLabel = new JLabel("ícone grande aqui");
public FileIconExtractor() {
this.setSize(200, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(novo BorderLayout());
getIconBtn.setActionCommand("GETICON");
getIconBtn.addActionListener (este);
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(verdadeiro);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("GETICON")) {
String ext = extField.getText();
Arquivo de arquivo;
tentar
{
arquivo = File.createTempFile("ícone", "." + ext);
Visualização FileSystemView = FileSystemView.getFileSystemView();
Ícone smallIcon = view.getSystemIcon(arquivo);
ShellFolder shellFolder = ShellFolder.getShellFolder(arquivo);
Ícone bigIcon = new ImageIcon(shellFolder.getIcon(true));
setIconLabel(pequeno, grandeIcon);
arquivo.delete();
}
pegar (IOException ioe)
{
ioe.printStackTrace();
}
}
}
private void setIconLabel(Ícone pequenoIcon, Ícone grandeIcon) {
smallIconLabel.setIcon(smallIcon);
bigIconLabel.setIcon(bigIcon);
}
public static void main(String[] args) {
FileIconExtractor fie = new FileIconExtractor();
}
}