?? choicegroupdemo.java
字號:
/*
* ChoiceGroupDemo.java
*
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author Liu Bin
* @version
*/
public class ChoiceGroupDemo extends MIDlet
implements CommandListener , ItemStateListener {
private Display display;
private Form form;
private Command cmdExit;
private StringItem testItem;
/**
* Radio buttons
*/
private ChoiceGroup radioGroup;
/**
* Checkboxes
*/
private ChoiceGroup checkboxGroup;
/**
* Popup
*/
private ChoiceGroup popupGroup;
//Popup ChoiceGroup的內容
private static final String[] popupMenu = {"全部", "布局","字體"};
//Radio ChoiceGroup的內容
private static final String[] radioMenu = {"左對齊",
"居中", "右對齊"
};
//Checkbox ChoiceGroup的內容
private static final String[] checkboxMenu = {"黑體",
"傾斜", "下劃線"
};
public ChoiceGroupDemo() {
try {
nbInit();
} catch(Exception e) {
e.printStackTrace();
}
}
private void nbInit() throws Exception {
cmdExit = new Command("退出", Command.EXIT, 0);
popupGroup = new ChoiceGroup("選擇操作方法", ChoiceGroup.POPUP,
popupMenu, null);
radioGroup = new ChoiceGroup("選擇布局", ChoiceGroup.EXCLUSIVE,
radioMenu, null);
checkboxGroup = new ChoiceGroup("選擇字體式樣", ChoiceGroup.MULTIPLE,
checkboxMenu, null);
boolean selectArray[] = {false, false, false};
checkboxGroup.setSelectedFlags(selectArray);
form = new Form("ChoiceGroup演示");
form.append(popupGroup);
form.append(radioGroup);
form.append(checkboxGroup);
testItem = new StringItem("","測試效果");
testItem.setPreferredSize(50, 20);
form.append(testItem);
form.addCommand(cmdExit);
form.setCommandListener(this);
form.setItemStateListener(this);
}
public void startApp() {
display = Display.getDisplay(this);
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
/**
* 處理命令按鈕事件
*/
public void commandAction(Command cmd, Displayable d) {
if (cmd == cmdExit) {
destroyApp(true);
}
}
//處理選擇框內選項改變的事件
public void itemStateChanged(Item item) {
if( item instanceof ChoiceGroup) {
ChoiceGroup cg = (ChoiceGroup)item;
int index = cg.getSelectedIndex();
if (cg == radioGroup) {
switch(index) {
case 0:
testItem.setLayout(Item.LAYOUT_LEFT);
break;
case 1:
testItem.setLayout(Item.LAYOUT_CENTER);
break;
case 2:
testItem.setLayout(Item.LAYOUT_RIGHT);
break;
}
} else if (cg == popupGroup) {
switch(index) {
case 0:
showAll();
break;
case 1:
showRadioChoiceGroup();
break;
case 2:
showCheckboxChoiceGroup();
break;
}
} else if (cg == checkboxGroup) {
boolean[] sf = new boolean[cg.size()];
cg.getSelectedFlags(sf);
int style = 0;
for (int i=0;i<sf.length;i++) {
if (sf[i]) {
style |= 1 << i;
}
}
Font oldFont, newFont;
oldFont = testItem.getFont();
newFont = Font.getFont(oldFont.getFace(),
style, oldFont.getSize());
testItem.setFont(newFont);
//force form to update UI at once
form.delete(form.size()-1);
form.append(testItem);
}
}
}
private void showAll() {
form.deleteAll();
form.append(popupGroup);
form.append(radioGroup);
form.append(checkboxGroup);
form.append(testItem);
}
private void showRadioChoiceGroup() {
form.deleteAll();
form.append(popupGroup);
form.append(radioGroup);
form.append(testItem);
}
private void showCheckboxChoiceGroup() {
form.deleteAll();
form.append(popupGroup);
form.append(checkboxGroup);
form.append(testItem);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -