Java Fifth time job

Source: Internet
Author: User

Job Requirements: Scan the files in the specified location from the hard disk and then display them to the graphical user interface, add a drop-down menu in the GUI interface, list box, below is my code:

Import Java.awt.BorderLayout; Import Java.awt.Color; Import java.awt.Dimension; Import Java.awt.Toolkit; Import java.awt.event.ActionEvent; Import Java.awt.event.ActionListener; Import Java.io.File; Import Java.nio.file.Path; Import java.nio.file.Paths; Import java.util.ArrayList; Import java.util.List; Import Javax.swing.JComboBox; Import Javax.swing.JFrame; Import Javax.swing.JLabel; Import javax.swing.JList; Import Javax.swing.JScrollPane; Import javax.swing.event.ChangeEvent; public class List {public JList fileList, public static string[] BMP =new string[20], public static string[] gif =new STR ING[20]; public static string[] jpg =new string[20]; public static int i=0,j=0,k=0; /** * @param args */public static void Listendirectory (File dir) throws illegalaccessexception{//Determine if dir exists, use the Fire class if (! Dir.exists ()) {//If not present, throws an exception throw new Illegalaccessexception ("directory" +dir+ "does not exist. "); } Determine if Dir uses the Fire class's Isdirectory method if (!dir.isdirectory ()) {throw new IllegalArgumentException (dir+ "not a Directory") for the directory; }

    Finalfile[] files = dir.listfiles ();Forfile file:files) {if (File.getname ().  Contains ( "BMP")) {//Look for Files with "BMP" file name Bmp[i]=dir+" \ \ "+file.getname (); //the file name with "BMP" into the array i++;} if (File.getname ().  Contains ( "gif")) {//looking for files with "gif" file name Gif[j]=dir+" \ \ "+file.getname (); //put the file name with "GIF" in the array J + +;} if (File.getname ().  Contains ( "jpg") {//Look for files with "JPG" file name Jpg[k]=dir+" \ \ "+file.getname (); //the file name with "JPG" into the array k++;}           

System.out.println (file); }

    Final JFrame frame =New JFrame ();    Frame.getcontentpane (); Frame.setlayout (New BorderLayout (2,1)); string[] formats = {"All Formats","BMP format","GIF format","JPG format"};Final JComboBox formatlist =New JComboBox (formats);Final JList fileList =New JList (files); Filelist.setforeground (New Color (100,88,60)); Filelist.setbackground (New Color (0,0,0)); Filelist.setselectionbackground (New Color (80,49,134)); Filelist.setselectionforeground (New Color (140,71,26));Final JScrollPane ScrollPane =New JScrollPane (fileList); Scrollpane.setcolumnheaderview (New JLabel ("Picture catalogue")); Frame.add (Formatlist, Borderlayout.north); Frame.add (ScrollPane, Borderlayout.center); Frame.setvisible (true); Dimension d = Toolkit.getdefaulttoolkit (). Getscreensize (); Frame.setbounds (d.width/2, d.height/2,250,400);Formatlist.addactionlistener (New ActionListener () {PublicvoidActionperformed(ActionEvent e) {if (e.getsource () = = Formatlist) {if (formatlist.getselectedindex () = =0) {filelist.setlistdata (files);All files are displayed when the list box selects all Files}if (formatlist.getselectedindex () = =1) {filelist.setlistdata (BMP);When the list box selects the "BMP" file, the file with "BMP" is displayed}if (formatlist.getselectedindex () = =2) {filelist.setlistdata (GIF); //list box when you select the "GIF" file, the file with "GIF" is displayed} if (formatlist.getselectedindex () = =3) {filelist.setlistdata (jpg); //list box when selecting "JPG" file, display the file with "JPG"}}}); the public static void main(string[] args) { try {list.listendirectory ("New File" ("D:\ \")); } catch (Illegalaccessexception e) { //TODO auto-generated catch block E.printstacktrace ();  }}} 

Java Fifth job

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.