Ticker对象
Ticker对象是一个项目类型的对象,它的作用相当于一个滚动消息栏,在屏幕的上方显示滚动的信息。 Ticker类的构造函数仅有一个参数,那就是需要滚动显示的消息。
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ShowTicker extends MIDlet implements CommandListener
{
private Display display;
private Form props;
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
public ShowTicker()
{
display = Display.getDisplay(this);
}
public void startApp()
{
props = new Form("Hello World");
props.append("Hello World!/n");
Ticker ticker=new Ticker("С¥һҹ
;Ìý´ºÓê");
props.setTicker(ticker);
props.addCommand(exitCommand);
props.setCommandListener(this);
display.setCurrent(props);
}
public void commandAction(Command c, Displayable s)
{
if (c == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
}
public void destroyApp(boolean unconditional)
{
}
public void pauseApp()
{
display.setCurrent(null);
props = null;
}
}
ShowTicker.java程序的运行效果如下图所示:
获取文本框的值
发信站: 北大未名站 (2001年10月21日00:34:19 星期天) , 站内信件
在前面的例子中,我们已经演示了如何构造J2ME程序的用户界面。现在有一个问题,那就是如何与用户界面交互呢?亦即如何获取用户通过用户界面输入的值呢?请看下面的例子。
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class GetTextBoxValue extends MIDlet implements CommandListener
{
private Display display;
private TextBox txtBox;
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command getCommand = new Command("GETVALUE", Command.OK, 1);
public GetTextBoxValue()
{
display = Display.getDisplay(this);
}
public void startApp()
{
//or :
//String str="hello world";
//txtBox = new TextBox("Text Box",str,str.length(),0);
//the follow code is wrong:
//txtBox = new TextBox("Text Box",str,any number here,0);
txtBox = new TextBox("Text Box",null,200,0);
txtBox.addCommand(exitCommand);
txtBox.addCommand(getCommand);
txtBox.setCommandListener(this);
display.setCurrent(txtBox);
}
public void valueScreen()
{
Form props=new Form("get text box value");
props.append(txtBox.getString());
props.addCommand(exitCommand);
props.setCommandListener(this);
display.setCurrent(props);
}
public void commandAction(Command c, Displayable s)
{
if (c == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
if(c==getCommand)
{
valueScreen();
}
}
public void destroyApp(boolean unconditional)
{
}