highlevelevent.java
来自「《J2ME实用教程》清华大学出版社出版」· Java 代码 · 共 62 行
JAVA
62 行
package com.j2medev.chapter3;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class HighlevelEvent extends MIDlet implements CommandListener,ItemStateListener,ItemCommandListener {
private Display display = null;
private Form form = null;
private TextField input = new TextField("input something:","",25,TextField.ANY);
private StringItem button = new StringItem("","press me",StringItem.BUTTON);
private Command exit = new Command("Exit",Command.EXIT,1);
private Command ok = new Command("Ok",Command.OK,2);
public void startApp() {
if(display == null){
display = Display.getDisplay(this);
form = new Form("high level event");
form.append(input);
form.append(button);
form.addCommand(exit);
button.addCommand(ok);
//为StringItem注册ItemCommandListener
button.setItemCommandListener(this);
//为Form注册ItemStateListener和CommandListener
form.setItemStateListener(this);
form.setCommandListener(this);
}
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
//当exit被按下的时候,commandAction()方法被触发
public void commandAction(Command cmd,Displayable displayable){
if(cmd == exit){
destroyApp(false);
notifyDestroyed();
}
}
//button上的Command被触发后 commandAction()方法被触发
public void commandAction(Command cmd,Item item){
if(item == button){
if(cmd == ok){
System.out.println("Button is pressed");
display.vibrate(2000);
}
}
}
//input状态改变的时候,itemStateChanged方法被触发
public void itemStateChanged(Item item){
if(item == input){
System.out.println("the input's state is changed");
display.flashBacklight(2000);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?