Java Fifth time job

Source: Internet
Author: User
Tags string format

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 class WJ extends JFrame
Implements ChangeListener, ActionListener
{
Private static final String Version = "Version 1.0";
private static final String Wenj_dir = "File";

Private JList fileList;Private Soundengine player;PublicStaticvoid Main (String[] args) {WJ GUI =New WJ ();}Public WJ () {Super"Documents"); Player =New Soundengine ();string[] FileNames = FindFiles (Wenj_dir,NULL); Makeframe (FileNames);}Privatevoid Quit () {System.exit (0);}Privatevoid Showabout () {Joptionpane.showmessagedialog (This"File \ n" + VERSION,"About file", joptionpane.information_message);}PrivateString[] FindFiles (String DirName,String suffix) {File dir =New File (DirName);if (Dir.isdirectory ()) {string[] Allfiles = Dir.list ();if (suffix = =NULL) {return allfiles; }else {list<String> selected =New arraylist<String> ();ForString filename:allfiles) {if (filename.endswith (suffix)) {selected.add (filename);}}Return Selected.toarray (NewString[selected.size ()]); } }else {System.out.println ("Error:" + DirName +"Must be a directory");ReturnNull }}Publicvoid StateChanged (ChangeEvent evt) {}Add ListenerPublicvoid actionperformed (ActionEvent evt) {JComboBox cb = (JComboBox) evt.getsource ();String format = (String) Cb.getselecteditem ();if (Format.equals ("All")) {format =Null } filelist.setlistdata (FindFiles (wenj_dir, format));}Privatevoid Makeframe (String[] Files) {setdefaultcloseoperation (exit_on_close); JPanel ContentPane = (JPanel) getcontentpane (); Contentpane.setborder (New Emptyborder (6,10,10,10)); Makemenubar (); Contentpane.setlayout (New BorderLayout (8,8)); JPanel Leftpane =New JPanel (); {Leftpane.setlayout (New BorderLayout (8,8));string[] formats = {"All",". Doc",". png",". mp3"}; JComboBox formatlist =New JComboBox (formats); Formatlist.addactionlistener (this); Leftpane.add (Formatlist, Borderlayout.north); FileList =New JList (Files); Filelist.setforeground (New Color (140,171,226)); Filelist.setbackground (New Color (0,0,0)); Filelist.setselectionbackground (new Color (87,49, 134)); Filelist.setselectionforeground (new Color (140,171,226)); JScrollPane ScrollPane = new JScrollPane (fileList); Scrollpane.setcolumnheaderview (new JLabel ( "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);           

Set Menu items
private void Makemenubar ()
{
Final int shortcut_mask =
Toolkit.getdefaulttoolkit (). Getmenushortcutkeymask ();

JMenuBar menubar =New JMenuBar ();        Setjmenubar (menubar);    JMenu menu;           JMenuItem item; menu =New JMenu ("File");        Menubar.add (menu); item =New JMenuItem ( "Quit"); Item.setaccelerator (Keystroke.getkeystroke (Keyevent.vk_q, shortcut_mask)); Item.addactionlistener (new ActionListener () {public void actionperformed new jmenu ( "help"); Menubar.add (menu); item = new JMenuItem (" about ... "); Item.addactionlistener (new ActionListener () {public void actionperformed                

}
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 class testrandomaccessfile{
private file file;
public static void Main (string[] args) {
Testrandomaccessfile Traf = new Testrandomaccessfile ();
Traf.init ();
Traf.record ("Adom", 80);
Traf.listallrecords ();

}PublicvoidRecordString Record_breaker,int times) {try{Randomaccessfile RAF =New Randomaccessfile (file,"RW"); Boolean flag =Falsewhile (Raf.getfilepointer () < Raf.length ()) {String name = Raf.readutf ();Long prior = Raf.getfilepointer ();if (record_breaker.equalsignorecase (name)) {flag =TrueCompare the number of passes in and the size of the previous numberif (Raf.readint () < times) {Use the Seek () method to jump to the position of prior raf.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 ();}}PublicvoidInit){if (file = =NULL) {file =New File ( "Record.txt"); try{file.createnewfile ();} catch (IOException e) {e.printstacktrace ();}} }public void listallrecords (try{ Randomaccessfile RAF = new randomaccessfile (File, "R"); while (Raf.getfilepointer () < Raf.length ()) {String name = Raf.readutf (); int times = Raf.readint (); System. out.println ( "name:" + name +  "\ Trecord: "+ Times";} Raf.close (); }catch (Exception e) {e.printstacktrace ();}}        

}
Interface diagram:
The original program:

To add a post-recorded program:

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.