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

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

文中所用到的资源文件可以在附件中下载


关于CheckBox组件的使用代码

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
		
	}

}


关于ComboBox组件的使用代码

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;
	}

}


你可能感兴趣的:(sun)