Java Fifth time job

Source: Internet
Author: User

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

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.