作业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();
}
}
}
界面图:
原来的程序效果图:
添加记录后的程序效果图: