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 (10,171,226));Sets the color of the JList filelist.setbackground (New Color (0,0,0));Sets the color of the JList filelist.setselectionbackground (New Color (8,49,134));Sets the color of the JList filelist.setselectionforeground (New Color (140,171,226));Set the color of the JListFinal JScrollPane ScrollPane =New JScrollPane (fileList); Scrollpane.setcolumnheaderview (New JLabel ("Picture catalogue"));Add a label to the top of the JList 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:\ \java file ")); } catch (Illegalaccessexception e) { //TODO auto-generated catch block E.printstacktrace (); }}}
Fifth time Assignment