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

📄 turbomidlet.java

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

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class TurboMidlet extends MIDlet implements CommandListener{
    
    private Display display = null;
    private Form mainForm = null;
    public static final Command exitCommand = new Command("退出",Command.OK, 1);
    
    //startApp()方法是程序的入口,相当于java应用程序中的main()方法
    public void startApp() {
        if(display == null){
            display = Display.getDisplay(this);
        }
        mainForm = new Form("TurboMIDlet");
        Image img = getImage("JavaPowered-8.png");
        mainForm.append(img);
        String text = "";
        try{
            text = getText("j2medev.txt");
        }catch(IOException ex){
            text = "读取文本出错";
        }
        mainForm.append("\n"+text);
        mainForm.addCommand(exitCommand);
        mainForm.setCommandListener(this);
        //将mainForm设置为手机屏幕的当前显示对象
        display.setCurrent(mainForm);
    }
    
    private Image getImage(String name){
        Image img = null;
        try{
            //读取images目录下的图片
            img = Image.createImage("/images/"+name);
        }catch(IOException ex){
            ex.printStackTrace();
        }
        return img;
    }
    
    private String getText(String name) throws IOException{
        
        //将资源文件,例如图片和文本,与java流关联的最好方法
        InputStream is = getClass().getResourceAsStream("/text/"+name);
        if(is != null){
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int ch = 0;
            //将文本文件的内容写入到baos开辟的内存中
            while((ch = is.read()) != -1){
                baos.write(ch);
            }
            //返回文本文件内容的字节数组
            byte[] text = baos.toByteArray();
            baos.close();
            //由于包含中文,所以编码转换为UTF-8
            return new String(text,"UTF-8");
        }else{
            return null;
        }
        
    }
    public void pauseApp() {
        //在本例中不做任何处理
    }
    
    public void destroyApp(boolean unconditional) {
        //在本例中不做任何处理
    }
    
    public void commandAction(Command cmd,Displayable displayable){
        if(cmd == exitCommand){
            destroyApp(false);
            notifyDestroyed();
        }
    }
    
}

⌨️ 快捷键说明

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