如何实现j2me对话框[2]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

class DialogForm extends Form implements CommandListener{//UI部分
 Command yesCMD;
 Command noCMD;
 Dialog dialog;
 
 public DialogForm(Dialog dialog,String title,String content,String yes,String no) {
  super(title);
  this.dialog=dialog;
  append(content);
  yesCMD=new Command(yes,Command.OK,1);
  noCMD=new Command(no,Command.CANCEL,1);
  addCommand(yesCMD);
  addCommand(noCMD);
  setCommandListener(this);
 }

 public void commandAction(Command c, Displayable d) {
  if(c==yesCMD){
   dialog.setBlockFlag(true);
   dialog.setReturnValue(Dialog.YES);
   dialog.wakeup();
  }else if(c==noCMD){
   dialog.setBlockFlag(true);
   dialog.setReturnValue(Dialog.NO);
   dialog.wakeup();
  }
 }
 
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


如何使用


我编写了一个Test。当用户按下show按钮时,开启对话框,并将用户的选择打印出来。

DialogTest.java
package bean;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class DialogTest extends MIDlet implements CommandListener {
 Display display;

 Form f = new Form("DialogTest");

 Command showCMD = new Command("show", Command.ITEM, 1);

 public DialogTest() {
  super();
  display = Display.getDisplay(this);
  f.addCommand(showCMD);
  f.setCommandListener(this);
  display.setCurrent(f);
 }

 protected void startApp() throws MIDletStateChangeException {
 }

 protected void pauseApp() {
 }

 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
 }

 public void commandAction(Command c, Displayable d) {
  new work1(c).start();
 }

 class work1 extends Thread {
  Command c;
  
  public work1(Command c) {
   super();
   this.c = c;
  }
  public void run() {
   super.run();
   if(c==showCMD){
    int choose=new Dialog(display).show("Choose","Do you like your Operation?","yes","no");
    if(choose==Dialog.YES){
     f.append("Yes,user like\n");
    }else if(choose==Dialog.NO){
     f.append("No,user like\n");
    }
    display.setCurrent(f);
   }
  }
 }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

本文关键:如何实现j2me对话框
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top