The job We practice this lesson is to scan the files in the specified location from the hard disk and display them in the graphical user interface. In this lesson in the work of the main use of two parts of the knowledge, part of the GUI interface, I added a list box, a drop-down menu, and a combo box, but the use of the combo box to classify the function is not implemented. Another part of the knowledge is to use the knowledge of the stream to obtain files on the hard disk. Here's my run.
Package test.com;
Import Java.awt.BorderLayout;
Import Java.awt.Color;
Import java.awt.Dimension;
Import Java.awt.Font;
Import Java.awt.Toolkit;
Import Java.io.File;
Import Javax.swing.JComboBox;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import javax.swing.JList;
Import Javax.swing.JPanel;
Import Javax.swing.JScrollPane;
Import Javax.swing.border.EmptyBorder;
public class FileUtils {
public static void Listendirectory (File dir) throws illegalaccessexception{
if (!dir.exists ()) {
throw new Illegalaccessexception ("directory" +dir+ "does not exist.) ");
}
if (!dir.isdirectory ()) {
throw new IllegalArgumentException (dir+ "is not a directory");
}
string[] FileNames = Dir.list ();
JList fileList = new JList (fileNames);
string[] Likes ={". mp3", ". mp4", ". jpg", ". txt"};
JComboBox combox =new JComboBox (likes);
JFrame frm = new JFrame ();
Frm.setdefaultcloseoperation (Jframe.exit_on_close);
JPanel ContentPane = new JPanel ();
Contentpane.setborder (New Emptyborder (6, 10, 10, 10));
Contentpane.setlayout (New BorderLayout (5, 5));
Contentpane.add (Combox, Borderlayout.north);
JPanel pane = new JPanel ();
Pane.setlayout (New BorderLayout (8, 8));
JLabel label = new JLabel ("File lists");
Label.setfont (New Font ("Serif", font.plain,16));
Filelist.setforeground (Color.Black);
Filelist.setbackground (Color.green);
Filelist.setselectionbackground (New Color (87,49,134));
Filelist.setselectionforeground (New Color (140,171,226));
JScrollPane ScrollPane = new JScrollPane (fileList);
Scrollpane.setcolumnheaderview (label);
Pane.add (ScrollPane, Borderlayout.center);
Contentpane.add (pane, Borderlayout.center);
Frm.add (ContentPane);
Frm.setbounds (500,300,300,400);
Frm.setvisible (TRUE);
}
public static void Main (string[] args) {
try {
Fileutils.listendirectory (New File ("d:\\"));
} catch (Illegalaccessexception e) {
E.printstacktrace ();
}
}
}
Java Learning (v)