Java Learning (v)

Source: Internet
Author: User

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)

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.