📄 itemlayoutdemo.java
字号:
//itemLayoutDemo.java file
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class itemLayoutDemo extends MIDlet implements CommandListener ,ItemCommandListener
{
private Command exitCommand,layoutCommand, defaultCommand, btnCommand1, btnCommand2;
private Form form;
//定义两个输入框
private TextField tfUserName, tfPassword;
//定义文字标签和两个用作按钮的文字标签
private StringItem strTitle, strLogin, strReset;
public itemLayoutDemo()
{
exitCommand =new Command("Exit",Command.EXIT,1);
btnCommand1 =new Command("Submit",Command.SCREEN,1);
btnCommand2 =new Command("Reset",Command.SCREEN,1);
layoutCommand =new Command("setLayout",Command.SCREEN,2);
defaultCommand =new Command("defaultLayout",Command.SCREEN,2);
//创建Form对象
form = new Form("Layout Demo");
//添加文字标签到Form对象中
strTitle = new StringItem("","Fill the form and Login");
form.append(strTitle);
//添加输入框到Form对象中
tfUserName = new TextField("Username","",12,TextField.ANY);
form.append(tfUserName);
tfPassword = new TextField("Passowrd","",12,TextField.PASSWORD);
form.append(tfPassword);
//添加按钮形状的文字标签到Form 对象中
strLogin = new StringItem("Login", " ", Item.BUTTON);
form.append(strLogin);
strReset = new StringItem("Reset", " ", Item.BUTTON);
form.append(strReset);
//为了文字标签能够显示为按钮形状,添加命令
strLogin.addCommand(btnCommand1);
strReset.addCommand(btnCommand2);
strLogin.setItemCommandListener(this);
strReset.setItemCommandListener(this);
//添加Form 对象的命令
form.addCommand(exitCommand);
form.addCommand(layoutCommand);
form.addCommand(defaultCommand);
form.setCommandListener(this);
}
protected void startApp( ) throws MIDletStateChangeException
{
Display.getDisplay(this).setCurrent(form);
}
protected void pauseApp( )
{
}
protected void destroyApp( boolean p1 )
{
}
public void commandAction(Command c,Displayable d)
{
if (c ==exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
else if (c ==layoutCommand)
{//设置每个Item对象的布局方式
//居中,并且要求下一个对象必须换行
strTitle.setLayout(Item.LAYOUT_CENTER|Item.LAYOUT_NEWLINE_AFTER|Item.LAYOUT_2);
//靠左,使用最小宽度
tfUserName.setLayout(Item.LAYOUT_LEFT|Item.LAYOUT_SHRINK|Item.LAYOUT_2);
//靠右,使用最小宽度
tfPassword.setLayout(Item.LAYOUT_RIGHT|Item.LAYOUT_SHRINK|Item.LAYOUT_2);
//居中,使用最小宽度
strLogin.setLayout(Item.LAYOUT_CENTER|Item.LAYOUT_SHRINK|Item.LAYOUT_2);
//居中,使用最小宽度
strReset.setLayout(Item.LAYOUT_CENTER|Item.LAYOUT_SHRINK|Item.LAYOUT_2);
}
else if (c ==defaultCommand)
{//将每个对象的布局方式恢复到默认状态
strTitle.setLayout(Item.LAYOUT_DEFAULT);
strLogin.setLayout(Item.LAYOUT_DEFAULT);
strReset.setLayout(Item.LAYOUT_DEFAULT);
tfUserName.setLayout(Item.LAYOUT_DEFAULT);
tfPassword.setLayout(Item.LAYOUT_DEFAULT);
}
}
public void commandAction(Command c, Item item)
{ if (c == btnCommand1)
{ System.out.println("btnCommand2 Selected");
} else if (c == btnCommand2)
{ System.out.println("btnCommand2 Selected");
} }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -