This job code is as follows
Import Java.awt.BorderLayout;
Import Java.awt.Color;
Import Java.awt.ScrollPane;
Import Java.io.File;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import javax.swing.JList;
Import Javax.swing.JScrollPane;
Import Javax.swing.JTextArea;
public class Fileuilt {
/**
* Lists the names of all files and subdirectories under the specified directory
* @throws illegalaccessexception
*
*/
public static void ListDirectory (File dir) throws illegalaccessexception{
JFrame frm = new JFrame ();
Frm.setdefaultcloseoperation (Jframe.exit_on_close);
Frm.setvisible (TRUE);
Frm.setlayout (New BorderLayout ());
JScrollPane sp=new JScrollPane (TL);
Frm.add (SP);
To determine if the file object Dir exists, use the exists () method of the File class
if (!dir.exists ()) {
Throws an exception if it does not exist
throw new Illegalaccessexception ("directory" + dir + "not present");
}
To determine if dir is a directory, use the Isdirectory () method of the File class
if (!dir.isdirectory ()) {
If Dir is not a directory, throw an exception
throw new IllegalArgumentException (dir + "not Directory");
}
Obtain the files and subdirectories under dir of the specified directory and output (1.list (); 2.listFiles ())
Use the list () method to implement
string[] FileName = Dir.list ();
for (String s:filename) {
System.out.println (dir + "\" + s);
T1.append (dir + "\ \" +s);
T1.append ("\ n");
T1.seteditable (FALSE);
}
System.out.println ();
Use the Listfiles () method to implement all file names that are listed in the directory only
file[] files = dir.listfiles ();
for (File file:files) {
If it is a file, the output
if (File.isfile ()) {
System.out.println (file);
}
}
JLabel J1 = new JLabel ("File Scan");
JList T1 = new JList (files);
JScrollPane sp = new JScrollPane (t1);
Frm.add (Sp,borderlayout.center);
Frm.add (J1,borderlayout.north);
Frm.setbounds (700, 300, 300, 400);
T1.setforeground (New Color (140,171,226));
T1.setbackground (New Color (0,0,0));
T1.setselectionbackground (New Color (87,49,134));
T1.setselectionforeground (New Color (140,171,226));
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
try {
Fileuilt.listdirectory (New File ("D:\\soundplayer"));
} catch (Illegalaccessexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Through this practice study, the method of output file name in GUI interface and the setting of interface background color are preliminarily mastered.
Java Fifth time job