Week five Java Study notes (v)

Source: Internet
Author: User

Displays all files under the specified directory in the list box (JList) component,

Following

Importjava.awt.BorderLayout;ImportJava.io.File;ImportJavax.swing.JComboBox;ImportJavax.swing.JFrame;Importjavax.swing.JList;ImportJavax.swing.JScrollPane; Public classKuang { Public Static voidListdrectory (File dir)throwsillegalaccessexception {JFrame frm=NewJFrame ("Drop down combo box");        Frm.setdefaultcloseoperation (Jframe.exit_on_close); Frm.setlayout (NewBorderLayout ()); String[] Likes={"All Formats", ". jpg", ". WPs", ". Doc", ". ppt", ". xls"}; JComboBox JB1=NewJComboBox (likes);                     Frm.add (JB1, Borderlayout.north); Frm.setbounds (550,190,260,360); Frm.setvisible (true); //determine if the file (directory) is being passed in        if(!dir.exists ()) {            Throw NewIllegalaccessexception ("directory" + dir + "does not exist"); }        //determine if the pass in is a directory        if(!dir.isdirectory ()) {            Throw NewIllegalaccessexception (dir + "not directory"); }        //use the list () method in the file class to list all the file (directory) names in the current directorystring[] FileNames =dir.list (); JList L1=NewJList (fileNames); JScrollPane SP=NewJScrollPane (L1);//Put the jlist in the scroll bar and put the scroll bar in the Layout centerFrm.add (sp,borderlayout.center);//For (String s:filenames) {//System.out.println (dir + "\ \" +s);//output The full file directory name//        }//        //lists all files (folders) under the specified directory (including its subdirectories)//file[] fs = Dir.listfiles ();//For (File file:fs) {//if (file.isdirectory ()) {//                //Recursive//listdrectory (file);//}else {//System.out.println (dir + "\ \" +file); //string[] fileNames = Dir.list ();//JList JL =new JList (fileNames);//Jl.setforeground (New Color (140,171,226));//Jl.setbackground (New Color (0,0,0));//Jl.setselectionbackground (New Color (87,49,134));//Jl.setselectionforeground (New Color (140,171,226));//JScrollPane sp=new JScrollPane (JL);//Frm.add (sp,borderlayout.center);                                                        }                                    /**     * @paramargs*/     Public Static voidMain (String[]args) {//TODO auto-generated Method Stub                Try{kuang.listdrectory (NewFile ("C:\\users\\think\\desktop\\1")); } Catch(illegalaccessexception e) {//Todo:handle ExceptionE.printstacktrace (); }            }}

Week five Java Study notes (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.