j2me学习四_LCDui类学习(3)

 

Graphics

       Graphics对象可以用在任何可以画的地方,属于低级Api,执行的时候要么在屏幕上,要么在缓存中,Graphics对象可以通过getGraphics()方法获得。

    它有许多成员方法,能够画的有:text, images, lines, rectangles, arcs,记住在使用之前设置好颜色。

Commond

    Command(String label, int commandType, int priority)

看构造函数,第一个是Command的标题,第二个是Command类型,第三个是优先级。

       分类:普通命令,用于Displayable对象;Item命令,用于FormItem,当Item获得焦点时可用;默认命令,也是用于FormItem;设备提供操作,

       在多界面的程序中,一定要在每一个界面加入后退或取消命令,方便用户使用。

       要设置相应的CommandListener,否则系统会阻塞。

一个简单的例子

创建一个能用用输入数字控制gauge,还有一个能显示信息的Canvas,

Uml如下;

 

代码:

import javax.microedition.lcdui.*;

import javax.microedition.midlet.*;

public class SimpleTest extends MIDlet implements CommandListener {

    private HelloCanvas myCanvas;

    private Form myForm;

    private Gauge myGauge;

    private TextField textField;

    private Command backCommand = new Command("Back", Command.BACK, 1);

    private Command messageCommand = new Command("Message", Command.SCREEN,1);

    private Command displayCommand = new Command("Display Message", Command.SCREEN,1);

    private Command exitCommand = new Command("Exit", Command.EXIT, 1);

    private Command showCommand = new Command("Show Levels", Command.SCREEN, 1);

    public SimpleTest()

    {

       myCanvas=new HelloCanvas();

       myCanvas.addCommand(backCommand);

        myCanvas.addCommand(messageCommand);

        myForm=new Form("Gauge level");

        myGauge=new Gauge("Value", true, 120, 10);

        textField=new TextField("Enter Nunmber", "", 3, TextField.NUMERIC);

        myForm.append(myGauge);

        myForm.append(textField);

        myForm.addCommand(showCommand);

        myForm.addCommand(displayCommand);

        myForm.addCommand(exitCommand);

        myCanvas.setCommandListener(this);

        myForm.setCommandListener(this);

    }

    public void commandAction(Command arg0, Displayable arg1) {

       // TODO Auto-generated method stub

       if(arg0==exitCommand) notifyDestroyed();

       if(arg0==messageCommand) myCanvas.newMessage();

       if(arg0==backCommand) Display.getDisplay(this).setCurrent(myForm);

       if(arg0==displayCommand)

           {

           Display.getDisplay(this).setCurrent(myCanvas);

           myCanvas.start();

           }

       if(arg0==showCommand)

       {

           String valueString=textField.getString();

           int value=0;

           if(!valueString.equals(""))

           {

              value=Integer.parseInt(valueString);

           }

           myGauge.setValue(value);

       }

    }

    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.getDisplay(this).setCurrent(myForm);

    }

 

 

}

import javax.microedition.lcdui.Canvas;

import javax.microedition.lcdui.Font;

import javax.microedition.lcdui.Graphics;

 

import org.eclipse.swt.graphics.Color;

import org.eclipse.swt.widgets.ColorDialog;

 

 

public class HelloCanvas extends Canvas {

 

       boolean myCanvasTXT=true;

       public HelloCanvas()

       {

             

       }

       protected void paint(Graphics arg0) {

              // TODO Auto-generated method stub

              int w=getWidth();

              int h=getHeight();

              arg0.setColor(0,12,43);

              arg0.fillRect(0, 0, w, h);

              if(myCanvasTXT)

              {

                     Font font =arg0.getFont();

                     int fontHeight=font.getHeight();

                     int fontWidth=font.stringWidth("Hello");

                     arg0.setFont(font);

                     arg0.setColor(0,0,0);

                     arg0.drawString("Hello", (w - fontWidth) / 2, (h - fontHeight) / 2, Graphics.TOP | Graphics.LEFT);

              }

       }

       void start()

       {

              repaint();

       }

       public void newMessage()

       {

              myCanvasTXT=!myCanvasTXT;

              repaint();

       }

 

}

主要是Command和Graphic的使用。

运行截图

你可能感兴趣的:(String,command,Class,import,UML,j2me)