j2ME高级用户界面设计
MIDP 高级用户界面设计
1 高级用户界面组件全部定义在
javax.microedition.lcduijavax.microedition.lcduijavax.microedition.
包中。
2 高级屏幕对象(代码在NetBeans下面调试)
(1)第一个J2ME程序
代码
|
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package hello; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * @author Administrator */ //public class helloj2me extends MIDlet { public class helloj2me extends MIDlet implements CommandListener { private Display display=null; private Form form1=null; private static final Command exitCommand=new Command("关闭",Command.OK,1); public void startApp() { if (display==null) { display=Display.getDisplay(this); } form1=new Form("主窗体"); String text="HelloWorld"; form1.append(text); form1.addCommand(exitCommand); form1.setCommandListener(this); display.setCurrent(form1); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command cmd, Displayable d) { if (cmd==exitCommand) { //destoyApp(false); //notifyDestoyed(); destroyApp(false); notifyDestroyed(); } } } |

(2) 判断设备是否是彩色显示屏幕
代码
|
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package hello; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * @author Administrator */ public class CheckColor extends MIDlet implements CommandListener{ private Display display; private TextBox textbox; private Command exit; public CheckColor() { display =Display.getDisplay(this); exit=new Command("退出",Command.SCREEN,1); String message=null; System.out.println(display.isColor()); if (display.isColor()) { message="彩色显示屏幕"; } else { message="黑白显示屏幕"; } textbox=new TextBox("检验屏幕是否是彩色显示屏幕",message,17,TextField.UNEDITABLE); textbox.addCommand(exit); textbox.setCommandListener(this); } public void startApp() { //display.setCurrentItem(textbox); display.setCurrent(textbox); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command command, Displayable displayable) { if (command==exit) { destroyApp(true); notifyDestroyed(); } } } |
(3) Textbox 文本框
代码
|
/* * To change this template, choose Tools | Templates * and open the template in the editor. */
package hello; import javax.microedition.midlet.*; import javax.microedition.lcdui.*;
/** * @author Administrator */ public class TextBoxDemo extends MIDlet implements CommandListener{ private Display display; private TextBox textbox; private Command submit; private Command exit; public TextBoxDemo() { display=Display.getDisplay(this); submit=new Command("OK",Command.SCREEN,1); exit=new Command("Exit",Command.EXIT,1); textbox=new TextBox("Password","",30,TextField.PASSWORD); textbox.addCommand(exit); textbox.addCommand(submit); textbox.setCommandListener(this); }
public void startApp() { display.setCurrent(textbox); }
public void pauseApp() {
}
public void destroyApp(boolean unconditional) { }
public void commandAction(Command c, Displayable d) { if (c==submit) { textbox.setConstraints(TextField.ANY); textbox.setString("Password is "+textbox.getString()); textbox.removeCommand(submit); } else if (c==exit) { destroyApp(false); notifyDestroyed(); } } } |

(4)List列表、Alert消息框
代码
|
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package hello; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * @author Administrator */ public class ListCheckBox extends MIDlet implements CommandListener{ private Display display; private Command exit; private Command submit; private List list; public ListCheckBox() { display=Display.getDisplay(this); list=new List("您获取信息的途径是:",List.MULTIPLE); list.append("电视", null); list.append("报纸", null); list.append("网络", null); list.append("广播", null); exit=new Command("Exit",Command.EXIT,1); submit=new Command("Submit",Command.SCREEN,2); list.addCommand(exit); list.addCommand(submit); list.setCommandListener(this); } public void startApp() { display.setCurrent(list); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable d) { if (c==submit) { boolean choice[]=new boolean[list.size()]; StringBuffer message=new StringBuffer(); list.getSelectedFlags(choice); for (int x=0;x<choice.length;x++) { if (choice[x]) { message.append(list.getString(x)); message.append(" "); } } Alert alert=new Alert("拟提交的选项是:",message.toString(),null,null); alert.setTimeout(Alert.FOREVER); alert.setType(AlertType.INFO); display.setCurrent(alert); list.removeCommand(c); } else if (c==exit) { destroyApp(false); notifyDestroyed(); } } } |

(5)Form表单:可滚动表单
代码
|
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package hello; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * @author Administrator */ public class SliderForm extends MIDlet implements CommandListener{ private Display display; private Form form; private Command exit; public SliderForm() { display=Display.getDisplay(this); exit=new Command("退出",Command.SCREEN,1); StringItem message[]=new StringItem[15]; message[0]=new StringItem("短消息0","未读的短消息"); message[1]=new StringItem("短消息1","未读的短消息"); message[2]=new StringItem("短消息2","未读的短消息"); message[3]=new StringItem("短消息3","未读的短消息"); message[4]=new StringItem("短消息4","未读的短消息"); message[5]=new StringItem("短消息5","未读的短消息"); message[6]=new StringItem("短消息6","未读的短消息"); message[7]=new StringItem("短消息7","未读的短消息"); message[8]=new StringItem("短消息8","未读的短消息"); message[9]=new StringItem("短消息9","未读的短消息"); message[10]=new StringItem("短消息10","未读的短消息"); message[11]=new StringItem("短消息11","未读的短消息"); message[12]=new StringItem("短消息12","未读的短消息"); message[13]=new StringItem("短消息13","未读的短消息"); message[14]=new StringItem("短消息14","未读的短消息"); form=new Form("收件箱",message); form.addCommand(exit); form.setCommandListener(this); } public void startApp() { display.setCurrent(form); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable d) { if (c==exit) { destroyApp(true); notifyDestroyed(); } } } |

(6)Ticker滚动条
代码:
|
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package hello; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * @author Administrator */ public class TickerList extends MIDlet implements CommandListener{ private Display display; private List list; private final String tech; private final String entertain; //声明滚动条 private Ticker ticker; private Command exit; private Command submit; public TickerList() { display=Display.getDisplay(this); tech=new String ("诺安股票 1.2302 华夏基金大盘精选 5.8820 融通动力先锋 2.4580 泰达荷银稳定 1.5789"); entertain=new String ("基金开元 2.534 基金惠普 2.630 基金同益 2.138 基金宏景"); exit=new Command("Exit",Command.EXIT,1); submit=new Command("Submit",Command.SCREEN,1); //创建滚动条 ticker=new Ticker(tech); list=new List("显示基金当前净值",Choice.EXCLUSIVE); list.append("开放基金", null); list.append("封闭基金", null); list.addCommand(exit); list.addCommand(submit); list.setCommandListener(this); list.setTicker(ticker); } public void startApp() { display.setCurrent(list); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable d) { if (c==exit) { destroyApp(true); notifyDestroyed(); } else if (c==submit) { if (list.getSelectedIndex()==0) { ticker.setString(tech); } else { ticker.setString(entertain); } } } } |

浙公网安备 33010602011771号