Week five study record

Source: Internet
Author: User

Job 1: Displays all files under the specified directory in the list box (JList) component.
The code is as follows:
Import java.awt. ;
Import java.awt.event.
;
Import javax.swing. ;
Import javax.swing.event.
;
Import javax.swing.border.*;

Import java.util.List;
Import java.util.ArrayList;

Import java.io.*;

 Public classWjextendsJFrameImplementsChangeListener, actionlistener{Private Static FinalString Version = "Version 1.0";Private Static FinalString wenj_dir = "File";PrivateJList fileList;PrivateSoundengine player; Public Static voidMain (string[] args) {WJ GUI=NewWJ ();} PublicWJ () {Super("File"); Player=NewSoundengine (); String[] FileNames= FindFiles (Wenj_dir,NULL); Makeframe (FileNames);}Private voidquit () {System.exit (0);}Private voidshowabout () {Joptionpane.showmessagedialog ( This,                 "File \ n" +VERSION,"About File", joptionpane.information_message);}Privatestring[] FindFiles (string dirName, string suffix) {File dir=NewFile (dirName); if(Dir.isdirectory ()) {string[] allfiles=dir.list (); if(Suffix = =NULL) {            returnAllfiles; }        Else{List<String> selected =NewArraylist<string>();  for(String filename:allfiles) {if(filename.endswith (suffix)) {selected.add (filename); }            }            returnSelected.toarray (Newstring[selected.size ()]); }    }    Else{System.out.println ("Error:" + dirName + "must be a directory"); return NULL; }} Public voidstatechanged (ChangeEvent evt) {}//Add Listener Public voidactionperformed (ActionEvent evt) {JComboBox CB=(JComboBox) Evt.getsource (); String format=(String) Cb.getselecteditem (); if(Format.equals ("All") ) {format=NULL; } filelist.setlistdata (FindFiles (wenj_dir, format));}Private voidmakeframe (string[] Files) {setdefaultcloseoperation (exit_on_close); JPanel ContentPane=(JPanel) Getcontentpane (); Contentpane.setborder (NewEmptyborder (6, 10, 10, 10));        Makemenubar (); Contentpane.setlayout (NewBorderLayout (8, 8)); JPanel Leftpane=NewJPanel (); {Leftpane.setlayout (NewBorderLayout (8, 8)); string[] Formats= {"All", ". Doc", ". png", ". mp3" }; JComboBox formatlist=NewJComboBox (formats); Formatlist.addactionlistener ( This);           Leftpane.add (Formatlist, Borderlayout.north); FileList=NewJList (Files); Filelist.setforeground (NewColor (140,171,226)); Filelist.setbackground (NewColor (0,0,0)); Filelist.setselectionbackground (NewColor (87,49,134)); Filelist.setselectionforeground (NewColor (140,171,226)); JScrollPane ScrollPane=NewJScrollPane (fileList); Scrollpane.setcolumnheaderview (NewJLabel ("Wenj Files"));    Leftpane.add (ScrollPane, Borderlayout.center);     } contentpane.add (Leftpane, Borderlayout.center);       Pack (); Dimension D=Toolkit.getdefaulttoolkit (). Getscreensize (); setlocation (D.width/2-getwidth ()/2, D.HEIGHT/2-getheight ()/2); SetVisible (true);}//setting Menu ItemsPrivate voidMakemenubar () {Final intShortcut_mask =Toolkit.getdefaulttoolkit (). Getmenushortcutkeymask (); JMenuBar MenuBar=NewJMenuBar ();        Setjmenubar (menubar);    JMenu menu;           JMenuItem item; Menu=NewJMenu ("File");        Menubar.add (menu); Item=NewJMenuItem ("Quit");        Item.setaccelerator (Keystroke.getkeystroke (Keyevent.vk_q, shortcut_mask)); Item.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {quit ();}    });    Menu.add (item); Menu=NewJMenu ("Help");        Menubar.add (menu); Item=NewJMenuItem ("About ..."); Item.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {showabout ();}    }); Menu.add (item);}}

Interface diagram:

Job 2: Output the highest recorded player information in the game.
Method: This can use the Seek () method to jump the pointer and the Getfilepointer () method to get the current pointer position implementation.
The code is as follows:
Import Java.io.File;
Import java.io.IOException;
Import Java.io.RandomAccessFile;

 Public classtestrandomaccessfile{Privatefile file; Public Static voidMain (string[] args) {testrandomaccessfile Traf=Newtestrandomaccessfile (); Traf.init (); Traf.record ("Adom", 80); Traf.listallrecords ();} Public voidRecord (String Record_breaker,intTimes ) {    Try{Randomaccessfile RAF=NewRandomaccessfile (file, "RW"); BooleanFlag =false;  while(Raf.getfilepointer () <raf.length ()) {String name=Raf.readutf (); LongPrior =Raf.getfilepointer (); if(record_breaker.equalsignorecase (name)) {flag=true; //Compare the number of passes in and the size of the previous number                                 if(Raf.readint () <Times ) {                                    //use the Seek () method to jump to the location of the priorRaf.seek (Prior);                                     Raf.writeint (times);  Break; }                             } Else{raf.skipbytes (4); }                             }        if(!flag)            {Raf.writeutf (Record_breaker);            Raf.writeint (times);                    } raf.close (); }Catch(Exception e) {e.printstacktrace (); }} Public voidinit () {if(File = =NULL) {file=NewFile ("Record.txt"); Try{file.createnewfile (); }Catch(IOException e) {e.printstacktrace (); }       }   } Public voidlistallrecords () {Try{Randomaccessfile RAF=NewRandomaccessfile (file, "R");  while(Raf.getfilepointer () <raf.length ()) {String name=Raf.readutf (); intTimes =Raf.readint (); System.out.println ("Name:" + name + "\trecord:" +Times );                    } raf.close (); }Catch(Exception e) {e.printstacktrace (); }       }}

Interface diagram:

Week five study record

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.