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

📄 imagemidlet.java

📁 <j2me 开发精解> 詹建光著 里所有的源码。对J2me的开发相当有帮助
💻 JAVA
字号:
package com.j2medev.ch6.image;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ImageMIDlet extends MIDlet implements CommandListener{
    
    private Display display = null;
    private Form form = null;
    HttpHandler handler = null;
    private TextField url = new TextField("图片路径", "", 40, TextField.ANY);
    public static final Command getCommand = new Command("读取图片", Command.OK,1);
    public static final Command backCommand = new Command("返回",Command.BACK,2);
    public static final Command exitCommand = new Command("退出",Command.EXIT, 2);
    
    public void startApp() {
        if(display == null){
            //初始化MIDlet,显示Form为当前屏幕,用户可以输入URL。
            display = Display.getDisplay(this);
            form = new Form("读取图片");
            form.append(url);
            form.addCommand(getCommand);
            form.addCommand(exitCommand);
            form.setCommandListener(this);
            handler = new HttpHandler(this);
            handler.start();
            display.setCurrent(form);
        }else{
            display.setCurrent(form);
        }
    }
    
    public void displayError(String message){
        //提示出错信息
        Alert alert = new Alert("错误信息");
        alert.setType(AlertType.ERROR);
        alert.setString(message);
        alert.setTimeout(2000);
        display.setCurrent(alert, form);
    }
    
    public void setImage(Image image){
        //当从服务器读取图片成功后,显示在手机屏幕中。
        Form imgForm = new Form("浏览图片");
        imgForm.append(image);
        //添加backCommand,方便用户返回到输入Form。
        imgForm.addCommand(backCommand);
        imgForm.setCommandListener(this);
        display.setCurrent(imgForm);
    }
    
    public void setCurrent(Displayable displayable){
        display.setCurrent(displayable);
    }
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) throws MIDletStateChangeException{
        handler.stop();
        handler = null;
    }
    //退出程序
    private void exitMIDlet() {
        try { 
            destroyApp(false);
            notifyDestroyed();
        } catch (MIDletStateChangeException e) {
            e.printStackTrace();
        }
    }
    
    public void commandAction(Command cmd,Displayable displayable){
        if(cmd == getCommand){
            //读取用户输入URL,应该检查用户输入的url是否合法。为了简单这里不做特殊处理。
            String imageUrl = url.getString();
            //启动线程
            handler.setURL(imageUrl);
            //唤醒处于等待状态的线程
            synchronized (this) {
                notify();
            }
        }else if(cmd == backCommand){
            display.setCurrent(form);
        }else if(cmd == exitCommand){
            exitMIDlet();
        }
    }
}

⌨️ 快捷键说明

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