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

📄 browsermidlet.java

📁 j2me简单实例,j2me教程加源码,希望大家喜欢
💻 JAVA
字号:
package com.j2medev.chapter8;

import java.io.*;
import java.util.Enumeration;
import javax.microedition.io.*;
import javax.microedition.io.file.FileConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class BrowserMIDlet extends MIDlet implements CommandListener{
    
    private Display display = null;
    private List list = null;
    private String currentDir = "";//存储当前目录
    private Command exitCommand = new Command("Exit",Command.EXIT,1);
    private Command captureCommand = new Command("new picture",Command.OK,0);
    private Command deleteCommand = new Command("delete",Command.OK,1);
    //目录和文件由不同的图片代表
    private static Image dirImg = null;
    private static Image fileImg = null;
    //路径的分隔符
    public static final char SEP = '/';
    //根目录
    public static final String ROOT = "file:///root1/";
    
    
    public void startApp() {
        if(display == null){
            display = Display.getDisplay(this);
            //初始化目录和文件的icon
            try{
                dirImg = Image.createImage("/dir.png");
                fileImg = Image.createImage("/file.png");
            }catch(IOException ex){
                ex.printStackTrace();
            }
            list = new List("favorite",List.IMPLICIT);
            //显示根目录的列表
            showPath(ROOT);
            list.addCommand(exitCommand);
            list.addCommand(deleteCommand);
            list.addCommand(captureCommand);
            list.setCommandListener(this);
        }
        display.setCurrent(list);
    }
    public void setCurrent(Displayable displayable){
        display.setCurrent(displayable);
    }
    
    public void main(){
        showPath(currentDir);
    }
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
    public void commandAction(Command command, Displayable displayable) {
        if(command == exitCommand){
            //退出
            destroyApp(false);
            notifyDestroyed();
        }else if(command == List.SELECT_COMMAND){
            String url = list.getString(list.getSelectedIndex());
            String temp = "";
            if(url.equals("..")){
                //用户选择返回上级目录
                int i = currentDir.lastIndexOf('/',currentDir.length()-2);
                temp = currentDir.substring(0,i+1);
            }else{
                //下一级目录或者浏览图片
                temp = currentDir + url;
            }
            showPath(temp);
        }else if(command == captureCommand){
            //新照片
            CaptureUI capture = new CaptureUI(this);
            display.setCurrent(capture);
        }else if(command.getCommandType() == Command.BACK){
            if(displayable instanceof Form){
                //从浏览照片的Form中返回
                int index = currentDir.lastIndexOf(SEP);
                currentDir = currentDir.substring(0,index+1);
                showPath(currentDir);
            }
        }else if(command == deleteCommand){
            String url = list.getString(list.getSelectedIndex());
            if(!url.endsWith("/")){
                //如果是文件,则删除;如果是目录不进行任何操作
                deleteFile(currentDir+url);
            }
        }
    }
    //删除文件
    private void deleteFile(final String fileName){
        //注意在新线程操作
        new Thread(){
            public void run(){
                try {
                    FileConnection fc = (FileConnection)Connector.open(fileName);
                    if(!fc.exists())
                        return;
                    fc.delete();
                    fc.close();
                    fc = null;
                    showPath(currentDir);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }.start();
    }
    //显示目录信息,包括目录和文件
    private void showPath(final String path){
        new Thread(){
            public void run(){
                try {
                    FileConnection fc = (FileConnection)Connector.open(path);
                    if(!fc.exists()){
                        fc.close();
                        fc = null;
                        return;
                    }
                    if(fc.isDirectory()){
                        //是目录,还是在list中显示
                        list.deleteAll();
                        Enumeration sub = fc.list();
                        while(sub.hasMoreElements()){
                            String p = (String)sub.nextElement();
                            //判断是文件还是目录
                            boolean fd = p.endsWith(SEP+"");
                            list.append(p,fd?dirImg:fileImg);
                        }
                        currentDir = path;
                        //如果不是根目录则添加一个返回上级的连接
                        if(!currentDir.equals(ROOT)){
                            list.append("..",dirImg);
                        }
                    }else{
                        //是png图片,在Form中显示出来
                        if(path.endsWith("png")){
                            //读取文件
                            byte[] buffer = new byte[(int)fc.fileSize()];
                            InputStream is = fc.openInputStream();
                            is.read(buffer);
                            Image img = Image.createImage(buffer,0,buffer.length);
                            Form form = new Form("img");
                            form.append(img);
                            form.addCommand(new Command("back",Command.BACK,1));
                            form.setCommandListener(BrowserMIDlet.this);
                            currentDir = path;
                            is.close();
                            fc.close();
                            fc = null;
                            setCurrent(form);
                            return;
                        }
                    }
                    //关闭连接
                    fc.close();
                    fc = null;
                    setCurrent(list);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }.start();
    }
}

⌨️ 快捷键说明

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