复制代码代码如下:
impor java.io.File;
impor java.io.IOException;
impor java.awt.BorderLayout;
impor java.awt.event.ActionEvent;
impor java.awt.event.ActionListener;
impor javax.swing.Icon;
impor javax.swing.ImageIcon;
impor javax.swing.JButton;
impor javax.swing.JFrame;
impor javax.swing.JPanel;
impor javax.swing.JTextField;
impor javax.swing.JLabel;
impor javax.swing.BoxLayout;
impor javax.swing.filechooser.FileSystemView;
impor sun.awt.shell.ShellFolder;
FileIconExtractor kelas publik memperluas implementasi JFrame ActionListener{
pribadi JButton getIconBtn = new JButton("dapatkan Ikon");
pribadi JPanel iconPanel = JPanel baru();
pribadi JTextField extField = JTextField baru();
private JLabel smallIconLabel = new JLabel("Ikon kecil di sini");
private JLabel bigIconLabel = new JLabel("Ikon besar di sini");
FileIconExtractor publik() {
ini.setUkuran(200, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ini.setLayout(BorderLayout baru());
getIconBtn.setActionCommand("GETICON");
getIconBtn.addActionListener(ini);
iconPanel.setLayout(BoxLayout baru(iconPanel, BoxLayout.Y_AXIS));
iconPanel.tambahkan(smallIconLabel);
iconPanel.add(bigIconLabel);
this.add(extField, BorderLayout.UTARA);
this.add(iconPanel, BorderLayout.CENTER);
this.add(getIconBtn, BorderLayout.SOUTH);
this.setVisible(benar);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("GETICON")) {
String ext = extField.getText();
berkas berkas;
mencoba
{
file = File.createTempFile("icon", "." + ext);
Tampilan FileSystemView = FileSystemView.getFileSystemView();
Ikon smallIcon = view.getSystemIcon(file);
ShellFolder shellFolder = ShellFolder.getShellFolder(file);
Ikon bigIcon = ImageIcon baru(shellFolder.getIcon(true));
setIconLabel(Ikon kecil,Ikon besar);
file.hapus();
}
menangkap (IOException ioe)
{
ioe.printStackTrace();
}
}
}
private void setIconLabel(Ikon kecilIkon, Ikon besarIkon) {
smallIconLabel.setIcon(smallIcon);
bigIconLabel.setIcon(bigIcon);
}
public static void main(String[] args) {
FileIconExtractor fie = FileIconExtractor baru();
}
}