Lwuit中CheckBox、ComboBox组件的使用说明

这几天刚刚开始接触LWUIT,前两天从网上搜到一兄台的博客写的关于Lwuit的使用,写的挺好可惜只写了三篇就没有再写了,我希望能给大家补一点,希望大家共同努力吧!

[color=red]文中所用到的资源文件可以在附件中下载[/color]


[color=green]关于CheckBox组件的使用[/color]代码

package com.mopietek;

import java.io.IOException;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.sun.lwuit.CheckBox;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;

public class CheckBoxLwuit extends MIDlet implements ActionListener{

protected void destroyApp(boolean unconditional)
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.init(this);
try {
Resources res = Resources.open("/javaTheme.res");
UIManager.getInstance().setThemeProps(res.getTheme("javaTheme"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Form form = new Form("Test CheckBox");
form.setLayout(new BorderLayout());
final CheckBox checkbox = new CheckBox("Check Box");
checkbox.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent evt) {

if(checkbox.isSelected()){
System.out.println("CheckBox got selected");
}else{
System.out.println("CheckBox got unselected");
}
}

});

form.addComponent(BorderLayout.NORTH,checkbox);
form.show();

}

public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub

}

}


[color=green]关于ComboBox组件的使用[/color]代码

package com.mopietek;

import java.io.IOException;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.sun.lwuit.ComboBox;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;

public class ComboBoxLwuit extends MIDlet implements ActionListener{

protected void destroyApp(boolean unconditional)
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.init(this);
try {
Resources res = Resources.open("/javaTheme.res");
UIManager.getInstance().setThemeProps(res.getTheme("javaTheme"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Form form = new Form("Test ComboBox");
form.setLayout(new BorderLayout());

String [] content = {"Red","Blue","Green","Yellow"};
//creating the combo box
ComboBox comboBox = new ComboBox(content);
//Setting a checkBox renderer
comboBox.setListCellRenderer(new CheckBoxRenderer());
//Adding a action listener to catch user clicking
//to open this ComboBox
comboBox.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent evt) {
System.out.println("选中了!");
}
});
form.addComponent(BorderLayout.NORTH,comboBox);
form.show();



}

public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub

}

}


package com.mopietek;

import com.sun.lwuit.CheckBox;
import com.sun.lwuit.Component;
import com.sun.lwuit.List;
import com.sun.lwuit.list.ListCellRenderer;

public class CheckBoxRenderer extends CheckBox implements ListCellRenderer {

public CheckBoxRenderer(){
super("");
}



public Component getListCellRendererComponent(List list, Object value,
int index, boolean isSelected) {
setText(" "+value);
if(isSelected){
setFocus(true);
setSelected(true);
}else{
setFocus(false);
setSelected(false);
}
return this;
}

public Component getListFocusComponent(List list) {

setText("");
setFocus(true);
setSelected(true);
return this;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值