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

📄 selectfileform.java

📁 简单的MP3播放器
💻 JAVA
字号:
package com.wootion.ui;

import java.io.IOException;
import java.util.Enumeration;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;

import com.wootion.mainMidlet.MainMidlet;
import com.wootion.player.MessageAlert;
import com.wootion.rms.PlayListRMS;

public class SelectFileForm extends List implements CommandListener,Runnable{
	
	PlayListRMS plrms = new PlayListRMS();
	
	private PlayerUI pui;
	
	String str = "";
	
	String name = "";
	
	/**
	 * 表示根目录的字符串
	 */
	private final static String ROOT = "/";
	
	private Command back;

	/**
	 * 表示父目录的字符串
	 */
	private final static String PARENT_FOLDER = "..";

	// 文件和目录的图标
	Image imgFile = null; // 文件图标

	Image imgDir = null; // 目录图标

	Image imgRoot = null; // 根目录图标

	// 保存当前的路径,缺省为根路径
	String curURI = ROOT;

	public SelectFileForm(Displayable dis) {
		super("浏览文件和文件夹", List.IMPLICIT);
		this.pui = (PlayerUI) dis;
		// 装载文件图标
		try {
			imgFile = Image.createImage("/res/txtfile.JPG");
		} catch (IOException e) {
			System.out.println("不能装载文件图标");
		}

		// 装载目录图标
		try {
			imgDir = Image.createImage("/res/dirclose.JPG");
		} catch (IOException e) {
			System.out.println("不能装载目录图标");
		}

		// 装载根目录图标
		try {
			imgRoot = Image.createImage("/res/drive.JPG");
		} catch (IOException e) {
			System.out.println("不能装载根目录图标");
		}
		back = new Command("返回",Command.BACK,1);
		this.addCommand(back);
		this.setCommandListener(this); 
		new Thread(this).start();
	}

	/**
	 * 处理命令按钮事件
	 */
	public void commandAction(Command c, Displayable d) {
		if (c == List.SELECT_COMMAND) {
			int index = this.getSelectedIndex();
			name = this.getString(index);
			if (name == PARENT_FOLDER) {
				// 返回上级目录
				int i = curURI.lastIndexOf('/', curURI.length() - 2);
				if (i != -1) {
					// 获得父目录名字
					curURI = curURI.substring(0, i + 1);
				} else {
					curURI = ROOT; // 根目录
				}
			} else {
				// 判断是否为子目录
				str = curURI + name;
				if (name.indexOf("/") > -1) {
					curURI = str;
				} else {
					// 如果为文件则显示属性
					addPlayList();
				}
			}

			// 在线程中显示当前目录
			new Thread(this).start();

		}
		if(c == back){
			pui.initialize();
			pui.repaint();
			Display.getDisplay(MainMidlet.getInstance()).setCurrent(pui);
		}
	}
	private void addPlayList() {
		String musicUrl = "file://"+str;
		//选择目标为:/c/bcgm.wav
		String musicName;
		if(name.indexOf(".") > 0){
			musicName = name.substring(0, name.indexOf(".")); 
		}else{
			musicName = name;
		}

		if(plrms.addRecord("playlistdb", musicName, musicUrl)){
			this.setSelectedIndex(getSelectedIndex(), true);
			Display.getDisplay(MainMidlet.getInstance()).setCurrent(new MessageAlert("提示","添加成功!",this));
		}else {
			Display.getDisplay(MainMidlet.getInstance()).setCurrent(new MessageAlert("警告","添加失败!",this));
		}
    }
	public void run() {
		this.deleteAll();

		if (curURI.equals(ROOT)) {
			Enumeration e = FileSystemRegistry.listRoots();
			while (e.hasMoreElements()) {
				String rootName = (String) e.nextElement();
				this.append(rootName, imgRoot);
			}
		} else {
			// 显示目录中的全部文件和文件夹
			this.append(PARENT_FOLDER, imgDir); // 添加父目录

			try {
				FileConnection fc = (FileConnection) Connector.open("file://"
						+ curURI);
				Enumeration e = fc.list();
				while (e.hasMoreElements()) {
					String name = (String) e.nextElement();
					if (name.indexOf("/") > -1) {
						// 添加目录
						this.append(name, imgDir);
					} else {
						// 添加文件
						this.append(name, imgFile);
					}
				}
				fc.close();
			} catch (IOException ioe) {
			}

		}
		
	}


}

⌨️ 快捷键说明

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