j2me高階介面和低階介面的切換
阿新 • • 發佈:2019-01-23
今天在用j2me做低階介面應用的時候 需要接收使用者輸入的資料.以前沒有做過,整理了一下,寫了一個例子.留作參考;全文如下:
import javax.microedition.lcdui.Canvas;
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.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MainMIDlet extends MIDlet implements CommandListener {
public static MainMIDlet self ;
public static Display display;
MainCanvas mainCanvas ;
private Form form;
TextField t ;
String str ;
private Command confirmCommand = new Command("確定", Command.ITEM, 1);
private Command exitCommand = new Command("返回", Command.EXIT, 1);
public MainMIDlet() {
// TODO Auto-generated constructor stub
self = this ;
mainCanvas = new MainCanvas() ;
display = Display.getDisplay(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
display.setCurrent(mainCanvas);
}
public void gotoForm() {
form = new Form("高階使用者介面");
String label = "input:";
String text = "";
int size = 20;
t = new TextField(label, text, size, TextField.ANY);
form.append(t);
form.addCommand(exitCommand);
form.addCommand(confirmCommand);
form.setCommandListener(this);
display.setCurrent(form);
}
public void gotoCanvas(){
display.setCurrent(mainCanvas) ;
MainMIDlet.self.mainCanvas.setFullScreenMode(true) ;
}
class MainCanvas extends Canvas implements Runnable{
Image img ;
public MainCanvas(){
setFullScreenMode(true);
try{
img = Image.createImage("/img.png") ;
}catch(Exception e){
System.out.println("pic not found");
}
new Thread(this).start() ;
}
protected void paint(Graphics g) {
// TODO Auto-generated method stub
g.drawImage(img,0,0,0) ;
g.drawString("低階使用者介面",0,0,0) ;
g.drawString("[確定]切換到高階介面", getWidth()/2, getHeight()/2,0) ;
if(str!=null&&str.length()>0)
g.drawString("輸入:"+MainMIDlet.self.str, getWidth()/2, getHeight()/2+15, 0) ;
}
public void run() {
// TODO Auto-generated method stub
}
protected void keyPressed(int keyCode) {
// TODO Auto-generated method stub
switch(keyCode){
case -5: //確定
MainMIDlet.self.gotoForm();
break;
}
}
protected void keyReleased(int keyCode) {
// TODO Auto-generated method stub
super.keyReleased(keyCode);
}
}
public void commandAction(Command c, Displayable d) {
// TODO Auto-generated method stub
if(c==exitCommand){
str = null ;
gotoCanvas() ;
}
if(c==confirmCommand){
str = t.getString() ;
gotoCanvas() ;
}
}
}
import javax.microedition.lcdui.Canvas;
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.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MainMIDlet extends MIDlet implements CommandListener {
public static MainMIDlet self ;
public static Display display;
MainCanvas mainCanvas ;
private Form form;
TextField t ;
String str ;
private Command confirmCommand = new Command("確定", Command.ITEM, 1);
private Command exitCommand = new Command("返回", Command.EXIT, 1);
public MainMIDlet() {
// TODO Auto-generated constructor stub
self = this ;
mainCanvas = new MainCanvas() ;
display = Display.getDisplay(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
display.setCurrent(mainCanvas);
}
public void gotoForm() {
form = new Form("高階使用者介面");
String label = "input:";
String text = "";
int size = 20;
t = new TextField(label, text, size, TextField.ANY);
form.append(t);
form.addCommand(exitCommand);
form.addCommand(confirmCommand);
form.setCommandListener(this);
display.setCurrent(form);
}
public void gotoCanvas(){
display.setCurrent(mainCanvas) ;
MainMIDlet.self.mainCanvas.setFullScreenMode(true) ;
}
class MainCanvas extends Canvas implements Runnable{
Image img ;
public MainCanvas(){
setFullScreenMode(true);
try{
img = Image.createImage("/img.png") ;
}catch(Exception e){
System.out.println("pic not found");
}
new Thread(this).start() ;
}
protected void paint(Graphics g) {
// TODO Auto-generated method stub
g.drawImage(img,0,0,0) ;
g.drawString("低階使用者介面",0,0,0) ;
g.drawString("[確定]切換到高階介面", getWidth()/2, getHeight()/2,0) ;
if(str!=null&&str.length()>0)
g.drawString("輸入:"+MainMIDlet.self.str, getWidth()/2, getHeight()/2+15, 0) ;
}
public void run() {
// TODO Auto-generated method stub
}
protected void keyPressed(int keyCode) {
// TODO Auto-generated method stub
switch(keyCode){
case -5: //確定
MainMIDlet.self.gotoForm();
break;
}
}
protected void keyReleased(int keyCode) {
// TODO Auto-generated method stub
super.keyReleased(keyCode);
}
}
public void commandAction(Command c, Displayable d) {
// TODO Auto-generated method stub
if(c==exitCommand){
str = null ;
gotoCanvas() ;
}
if(c==confirmCommand){
str = t.getString() ;
gotoCanvas() ;
}
}
}