Java (v)

Source: Internet
Author: User

♥ Job 1:

Displays all files for the specified path and implements file filtering. Intelligent monitoring, the program can automatically generate all the suffix of the file under any path in the drop-down menu .

in-code note content as a step and a holistic approach

1 Importjava.awt.BorderLayout;2 ImportJava.awt.Color;3 ImportJava.awt.Font;4 Importjava.awt.event.ActionEvent;5 ImportJava.awt.event.ActionListener;6 ImportJava.io.File;7 ImportJava.util.HashMap;8 ImportJavax.swing.JComboBox;9 ImportJavax.swing.JFrame;Ten ImportJavax.swing.JLabel; One ImportJavax.swing.JPanel; A ImportJavax.swing.JScrollPane; - ImportJavax.swing.JTextArea; - ImportJavax.swing.border.EmptyBorder; the  - /** - * File sorting system.  -  *  +  * @author  -  * +  */ A  Public classFileUtils { at  -     /** - * used to store categorized files.  - * Key: suffix name, value:stringbuilder store the corresponding file.  -      */ -     Privatehashmap<string, stringbuilder> resultmap =NewHashmap<string, stringbuilder>(); in      -     /** to * Listen to the file directory.  +      *  -      * @paramdir directory.  the      * @throwsillegalaccessexception Access Illegal exception.  *      */ $      Public voidListendirectory (File dir)throwsillegalaccessexception {Panax Notoginseng         if(!dir.exists ()) { -             Throw NewIllegalaccessexception ("directory" + dir + "does not exist.) "); the         } +  A         if(!dir.isdirectory ()) { the             Throw NewIllegalArgumentException (dir + "not directory"); +         } -          $string[] FileNames =dir.list (); $Resultmap.put ("All",NewStringBuilder ());//default All files.  -Resultmap.put ("folder",NewStringBuilder ());//folder form.  -          the         //suffix.  - String suffix;Wuyi          for(String filename:filenames) { theResultmap.get ("All"). Append (FileName + "\ n")); -             if(Filename.indexof (".") > 0) { Wusuffix = filename.substring (Filename.indexof (".")), Filename.length ()); -                  About                 if(!resultmap.containskey (suffix)) { $StringBuilder StringBuilder =NewStringBuilder (); -Stringbuilder.append (fileName + "\ n")); - resultmap.put (suffix, stringBuilder); -}Else { AResultmap.get (suffix). Append (FileName + "\ n")); +                 } the}Else { -Resultmap.get ("folder"). Append (FileName + "\ n")); $             } the         } the          the Buildgui (); the     } -      in     /** the * Build GUI.  the      */ About      Public voidBuildgui () { the         FinalJTextArea fileList =NewJTextArea (); theFilelist.settext (Resultmap.get ("All"). toString ()); thestring[] Likes =Newstring[resultmap.keyset (). Size (); + Resultmap.keyset (). ToArray (likes); -         FinalJComboBox Combox =NewJComboBox (likes); the         BayiJFrame frm =NewJFrame (); the frm.setdefaultcloseoperation (jframe.exit_on_close); the  -JPanel ContentPane =NewJPanel (); -Contentpane.setborder (NewEmptyborder (6, 10, 10, 10)); theContentpane.setlayout (NewBorderLayout (5, 5)); the Contentpane.add (Combox, Borderlayout.north); the          theJPanel pane =NewJPanel (); -Pane.setlayout (NewBorderLayout (8, 8)); the  theJLabel label =NewJLabel ("File lists:"); theLabel.setfont (NewFont ("Serif", Font.plain, 16));94  theFilelist.setforeground (NewColor (140, 171, 226)); the Filelist.setbackground (color.white); theFilelist.setselectedtextcolor (NewColor (87, 49, 134));98 Filelist.setforeground (color.black); About  -JScrollPane ScrollPane =NewJScrollPane (fileList);101 Scrollpane.setcolumnheaderview (label);102         103 Pane.add (ScrollPane, borderlayout.center);104 contentpane.add (pane, borderlayout.center); the Frm.add (contentpane);106Frm.setbounds (500, 300, 300, 400);107Frm.setvisible (true);108         109         //JComboBox Event Monitoring.  theCombox.addactionlistener (NewActionListener () {111 @Override the              Public voidactionperformed (ActionEvent e) {113                 Try { the                     //gets the item of the combo box theString item =(String) Combox.getitemat (Combox.getselectedindex ()); the Filelist.settext (Resultmap.get (item). toString ());117}Catch(Exception E1) {118 e1.printstacktrace ();119                 } -             }121         });122     }123 124      Public Static voidMain (string[] args) { theString Path = "d:\\";126         Try {127             NewFileUtils (). Listendirectory (NewFile (path)); -}Catch(illegalaccessexception e) {129 e.printstacktrace (); the         }131     } the}

Here's the result:

(1) D disk file types are many

(2) Also if there is only a folder under the F-drive path, the drop-down menu will only show folders

♥ Job 2: trying to improve ... (? ω?)? Teacher Please understand

Java (v)

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.