Java obtains system icon and file graph example _java according to extension

Source: Internet
Author: User

Copy Code code as follows:

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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.