⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dirlist.java

📁 J2ME程序设计实例教程的源码
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import java.io.IOException;

/**
 * 该类由于显示目录中的文件列表
 */
public class DirList extends List implements CommandListener {
    private FileManagerMIDlet midlet;
    private Image imgFile;
    private Image imgDir;
    
    private Command cmdExit = new Command("退出", Command.EXIT, 1);
    private Command cmdOK = new Command("打开", Command.OK, 2);
    private Command cmdDelete = new Command("删除", Command.SCREEN, 3);
    private Command cmdCreate = new Command("创建", Command.SCREEN, 3);
    private Command cmdProperty = new Command("属性", Command.SCREEN, 3);
    private Command cmdCopy = new Command("复制", Command.SCREEN, 3);
    private Command cmdPaste = new Command("粘贴", Command.SCREEN, 3);
    private String[] files;
    private String dir;
    
    public DirList(FileManagerMIDlet midlet) {
        super(null, IMPLICIT);
        this.midlet = midlet;
        try {
            imgFile = Image.createImage("/file.png");
            imgDir = Image.createImage("/dir.png");
        }
        catch(IOException ioe) {
            //
        }
        
        addCommand(cmdExit);
        addCommand(cmdOK);
        addCommand(cmdDelete);
        addCommand(cmdCreate);
        addCommand(cmdProperty);
        addCommand(cmdCopy);
        addCommand(cmdPaste);
        setCommandListener(this);
    }
    
    //使用dir目录下的文件files更新列表
    public void update(String dir, String[] files) {
        this.dir = dir;
        setTitle(dir);
        deleteAll();
        this.files = files;
        for(int i=0; i<files.length; i++) {
            if(FileManager.isDirectory(files[i])) {
                append(files[i], imgDir);
            }
            else {
                append(files[i], imgFile);
            }
        }
    }
    
    public void commandAction(Command cmd, Displayable d) {
        if(cmd == cmdExit) {
            midlet.exit();
        }
        else if(cmd == cmdOK) {
            Thread t = new Thread() {
                public void run() {
                    int index = getSelectedIndex();
                    try {
                        if(FileManager.isDirectory(files[index])) {
                            midlet.displayDirList(files[index]);
                        }
                        else {
                            midlet.openTxtFile(files[index], DirList.this);
                        }
                    }
                    catch(IOException ioe) {
                        midlet.showError(ioe.toString(), DirList.this);
                    }
                }
            };
            t.start();
        }
        else if(cmd == cmdDelete) {
            Thread t = new Thread() {
                public void run() {
                    int index = getSelectedIndex();
                    midlet.delete(files[index]);
                }
            };
            t.start();
        }
        else if(cmd == cmdCreate) {
            midlet.displayCreateForm();
        }
        else if(cmd == cmdProperty) {
            Thread t = new Thread() {
                public void run() {
                    int index = getSelectedIndex();
                    System.out.println(files[index]);
                    midlet.displayPropertyForm(getString(index), DirList.this);
                }
            };
            t.start();
        }
        else if(cmd == cmdCopy) {   //复制
            int index = getSelectedIndex();
            midlet.copy(files[index]);
        }
        else if(cmd == cmdPaste) {  //粘贴
            Thread t = new Thread() {
                public void run() {
                    try {
                        midlet.paste();
                    }
                    catch(IOException ioe) {
                        midlet.showError("复制错误:\n" + ioe.toString(), DirList.this);
                    }
                }
            };
            t.start();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -