码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA第五次作业

时间:2016-04-21 13:28:38      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:

作业1:将指定目录下的所有文件显示到列表框(JList)组件中。
代码如下:
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 = "文件";

private JList fileList;
private SoundEngine player;


public static void main(String[] args)
{
    wj gui = new wj();
}


public wj()
{
    super("文件");
    player = new SoundEngine();
    String[] FileNames = findFiles(WENJ_DIR, null);
    makeFrame(FileNames);
}



private void quit()
{
    System.exit(0);
}



private void showAbout()
{
    JOptionPane.showMessageDialog(this, 
                "文件\n" + VERSION,
                "About 文件", 
                JOptionPane.INFORMATION_MESSAGE);
}

private String[] 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>();
            for(String filename : allFiles) {
                if(filename.endsWith(suffix)) {
                    selected.add(filename);
                }
            }
            return selected.toArray(new String[selected.size()]);
        }
    }
    else {
        System.out.println("Error: " + dirName + " must be a directory");
        return null;
    }
}


public void stateChanged(ChangeEvent evt)
{
  
}

//添加监听器
public void 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));
}


private void 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);
}

//设置菜单项
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(ActionEvent e) { quit(); }
                       });
    menu.add(item);


    menu = new JMenu("Help");
    menubar.add(menu);
    
    item = new JMenuItem("About ...");
        item.addActionListener(new ActionListener() {
                           public void actionPerformed(ActionEvent e) { showAbout(); }
                       });
    menu.add(item);
}

}
界面图:
技术分享 技术分享 技术分享

作业2:将游戏中最高纪录的玩家信息输出。
方法:这个可以利用seek()方法跳转指针和getFilePointer()方法来获取当前指针位置实现。
代码如下:
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();

}

public void record(String record_breaker, int times){
    try{
        RandomAccessFile raf = new RandomAccessFile(file,"rw");
        boolean flag = false;
        while(raf.getFilePointer() < raf.length()){
            String name = raf.readUTF();
                             long prior = raf.getFilePointer();
                             if (record_breaker.equalsIgnoreCase(name)) {
                                 flag = true;
                                 //比较传递进来的数与之前数的大小
                                 if (raf.readInt() < times) {
                                    //利用seek()方法跳转到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();    
    }
}

public void init(){
    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();    
    }       
}

}
界面图:
原来的程序效果图:
技术分享

添加记录后的程序效果图:
技术分享

JAVA第五次作业

标签:

原文地址:http://www.cnblogs.com/chenhaotian/p/5416434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!