J2ME中关于Lwuit的标签的综合使用

J2ME中关于Lwuit的标签的综合使用

由于我使用的主题对中文支持不好,希望大家下载或编辑一些好的主题。


package com.mopietek;

import java.io.IOException;

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

import com.sun.lwuit.Button;
import com.sun.lwuit.ButtonGroup;
import com.sun.lwuit.CheckBox;
import com.sun.lwuit.ComboBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Graphics;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.Painter;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.TabbedPane;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.animations.Transition;
import com.sun.lwuit.animations.Transition3D;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.geom.Rectangle;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.GridLayout;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.table.Table;
import com.sun.lwuit.util.Resources;

public class ComponentMIDlet extends MIDlet implements ActionListener{

	public Form mainForm;
	public List mexampleList;
	public Command mBackCommand;
	
	
	protected void startApp() throws MIDletStateChangeException {
		
		if(mainForm == null){
			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 f = new Form("测试组件");
		mainForm = f;
		f.setLayout(new BorderLayout());
		List exampleList = new List();
		mexampleList = exampleList;
		exampleList.addItem("Labels");
		exampleList.addItem("Buttons");
		exampleList.addItem("More Buttons");
		exampleList.addItem("List and ComboBox");
		exampleList.addItem("Text");
		exampleList.addItem("Layouts");
		exampleList.addItem("Events");
		exampleList.addItem("Style");
		exampleList.addItem("TabbedPane");
		exampleList.addItem("Painter");
		exampleList.addItem("Exit");
		exampleList.addActionListener(this);
		
		f.addComponent(BorderLayout.CENTER,exampleList);
		
		mBackCommand = new Command("返回");

		}
		
		mainForm.show();
	}
	
	protected void destroyApp(boolean unconditional)
			throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub
		
	}

	

	public void actionPerformed(ActionEvent ae) {
		
		if(ae.getSource() == mexampleList){
			//选中的List列表
			String selection = (String)mexampleList.getSelectedItem();
			if(selection.equals("Labels")){
				showLabels();
			}
			else if(selection.equals("Buttons")){
				showButtons();
			}
			else if(selection.equals("More Buttons")){
				showMoreButtons();
			}
			else if(selection.equals("List and ComboBox")){
				showListAndComboBox();
			}
			else if(selection.equals("Text")){
				showText();
			}
			else if(selection.equals("Layouts")){
				showLayouts();
			}
			else if(selection.equals("Events")){
				showEvents();
			}
			else if(selection.equals("Style")){
				showStyle();
			}
			else if(selection.equals("TabbedPane")){
				showTabbedPane();
			}
			else if(selection.equals("Painter")){
				showPainter();
			}
			else if(selection.equals("Exit")){
				try {
					destroyApp(true);
				} catch (MIDletStateChangeException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		        notifyDestroyed();
			}
			
		}
		
		if(ae.getCommand() == mBackCommand){
			mainForm.show();
		}
		
	}
	
	public void showLabels(){
		
		Form f = new Form("测试Label");
		f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
		Label label1 = new Label("label1");
		Image image = null;
		try {
			 image = Image.createImage("/baldy.png");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Label label2 = new Label(image);
		Label label3 = new Label(image);
		label3.setText("label3");
		label3.setTextPosition(Component.BOTTOM);

		f.addComponent(label1);
		f.addComponent(label2);
		f.addComponent(label3);
		f.addCommand(mBackCommand);
		f.setCommandListener(this);
		f.show();
	}
	
	public void showButtons(){
		
		Form f = new Form("测试Button");
		
		try {
			 Button button1 = new Button("ClickMe");
			 button1.setTextPosition(Component.BOTTOM);
			 Image	image = Image.createImage("/baldy.png");
			 Button button2 = new Button(image);
			 Button button3 = new Button("Pic",image);
			 button3.setTextPosition(Component.BOTTOM);
			 f.addComponent(button1);
			 f.addComponent(button2);
			 f.addComponent(button3);
				
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		f.addCommand(mBackCommand);
		f.setCommandListener(this);
		f.show();
	}
	
	public void showMoreButtons(){
		
		Form f = new Form("MoreButtons");
		
		ButtonGroup buttonGroup = new ButtonGroup();
		
		RadioButton rb1 = new RadioButton("Mike");
		RadioButton rb2 = new RadioButton("Coffer");
		RadioButton rb3 = new RadioButton("Tea");
		rb1.addActionListener(this);
		rb2.addActionListener(this);
		rb3.addActionListener(this);
		
		buttonGroup.add(rb1);
		buttonGroup.add(rb2);
		buttonGroup.add(rb3);
		
		f.addComponent(rb1);
		f.addComponent(rb2);
		f.addComponent(rb3);
		
		
		CheckBox checkBox1 = new CheckBox("checkBox1");
		CheckBox checkBox2 = new CheckBox("checkBox2");
		CheckBox checkBox3 = new CheckBox("checkBox3");
		CheckBox checkBox4 = new CheckBox("checkBox4");
		CheckBox checkBox5 = new CheckBox("checkBox5");
		CheckBox checkBox6 = new CheckBox("checkBox6");
		
		f.addComponent(checkBox1);
		f.addComponent(checkBox2);
		f.addComponent(checkBox3);
		f.addComponent(checkBox4);
		f.addComponent(checkBox5);
		f.addComponent(checkBox6);
		
		f.addCommand(mBackCommand);
		f.setCommandListener(this);
		f.show();
		
		
	}
	
	public void showListAndComboBox(){
		
		Form f = new Form("List and ComboBox");
		
		List list = new List();
		list.addItem("red");
		list.addItem("green");
		list.addItem("yellow");
		list.addItem("blue");
		list.addItem("white");
		
		ComboBox comboBox = new ComboBox(list.getModel());
		f.addComponent(list);
		f.addComponent(comboBox);
		
		f.addCommand(mBackCommand);
		f.setCommandListener(this);
		f.show();
		
		
	}
	
	public void showText(){
		
		
		
		Form f = new Form("测试 Text组件");
		TextField textField = new TextField("this is write content in textField");
		
		TextArea textArea = new TextArea("this is write content in TextArea!",5,20);
		textArea.setMaxSize(512);
		
		f.addComponent(textField);
		f.addComponent(textArea);
		
		f.addCommand(mBackCommand);
		f.setCommandListener(this);
		f.show();
		
		
	}
	
	public void showLayouts(){
		
		Form f = new Form("Text Layouts");
		f.setLayout(new BorderLayout());
		
		
		try{
			Image image = Image.createImage("/baldy.png");
			
			Label label = new Label(image);
			label.setAlignment(Component.CENTER);
            label.setText("Label in Layouts");
            label.setTextPosition(Component.BOTTOM);
            
//          Container buttonContainer = new Container(new BoxLayout(BoxLayout.X_AXIS));
            Container buttonContainer = new Container();
            GridLayout exampleLayout = new GridLayout(3,2);
            buttonContainer.setLayout(exampleLayout);
            buttonContainer.addComponent(new Button("Add"));
            buttonContainer.addComponent(new Button("Remove"));
            buttonContainer.addComponent(new Button("Edit"));
            buttonContainer.addComponent(new Button("Send"));
            buttonContainer.addComponent(new Button("Receive"));
            buttonContainer.addComponent(new Button("Exit"));
            
            f.addComponent(BorderLayout.NORTH,label);
            f.addComponent(BorderLayout.CENTER,buttonContainer);
            
            f.addCommand(mBackCommand);
            f.setCommandListener(this);
            f.show();
            
            
			
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}
	
	public void showEvents(){
	
		Form f = new Form("Test Events");
		f.setLayout(new BorderLayout());
		
	    final Button button = new Button("Click Me");
		button.addActionListener(new ActionListener(){
             int i = 0;
			public void actionPerformed(ActionEvent av) {
                     i++;
                     button.setText("NO."+i+"Clicked");
			}
			
		});
		
		f.addComponent(BorderLayout.NORTH,button);
		f.addCommand(mBackCommand);
		f.setCommandListener(this);
		f.show();
		
	}
	
	public void showStyle(){
		
		Form f = new Form("Style");
		f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
		
		Label label1 = new Label("No using Style");
		//设置标签为透明色
		Label label2 = new Label("Transparent");
		label2.getStyle().setBgTransparency(0);
		//改变标签背景色
		Label label3 = new Label("Change the BackGroud Color and Font Color");
		Style style = label3.getStyle();
		style.setBgColor(0xaa00ff);
		//字体颜色为白色
		style.setFgColor(0xffffff);
		//设置margin的大小
		Label label4 = new Label("Margin around is 10px");
		label4.getStyle().setMargin(10,10,10, 10);
		//设置padding的大小
		Label label5 = new Label("Padding around is 20px");
		label5.getStyle().setPadding(20,20,20,20);
		
		f.addComponent(label1);
		f.addComponent(label2);
		f.addComponent(label3);
		f.addComponent(label4);
		f.addComponent(label5);
		
		f.addCommand(mBackCommand);
		f.setCommandListener(this);
		f.show();
		
		
		
		
		
		
		
		
		
	}
	
	public void showTabbedPane(){
		
		Form f = new Form("TabbedPane");
		f.setLayout(new BorderLayout());
		
		TabbedPane tabbedPane = new TabbedPane(TabbedPane.TOP);
		tabbedPane.addTab("tab1", new Label("this is label One"));
		tabbedPane.addTab("Tab2", new Label("this is label Two"));
		f.addComponent(BorderLayout.NORTH, tabbedPane);
		f.addCommand(mBackCommand);
		f.setCommandListener(this);
		f.show();
	}
	
	public void showPainter(){
		
		Form f = new Form("Painter");

	    try {
	      Label label = new Label("Baldy");

	      Image image = Image.createImage("/baldy.png");
	      Label pictureLabel = new Label(image);

	      Label bottomText = new Label(image);
	      bottomText.setText("Baldassare");
	      bottomText.setTextPosition(Component.BOTTOM);

	      f.addComponent(label);
	      f.addComponent(pictureLabel);
	      f.addComponent(bottomText);
	    } catch (IOException ioe) {
	      System.out.println(ioe);
	    }
	    
	    final int bg = f.getStyle().getBgColor();
	    final int fg = f.getStyle().getFgColor();
	    
	    f.getStyle().setBgPainter(new Painter() {
	        public void paint(Graphics g, Rectangle rect) {
	            int s = 12;
	            
	            Dimension size = rect.getSize();
	            int rx = rect.getX();
	            int ry = rect.getY();
	            int rw = size.getWidth();
	            int rh = size.getHeight();
	            
	            g.setColor(bg);
	            g.fillRect(rx, ry, rw, rh);
	            //画棱形
	            g.setColor(fg);
	            for (int y = 0; y < rh; y += 2 * s) {
	              for (int x = 0; x < rw; x += s) {
	                g.fillTriangle(rx + x, ry + y + s, rx + x + s / 2, ry + y,        rx + x + s, ry + y + s);
	                g.fillTriangle(rx + x, ry + y + s, rx + x + s / 2, ry + y + 2 *s, rx + x + s, ry + y + s);
	              }
	            }
	        }
	    });

	    f.addCommand(mBackCommand);
	    f.setCommandListener(this);
	    
	    //增加3D特效                                                                //时间  //true为逆时针翻转(向右翻转) false为顺时针翻转(向左翻转)
	    Transition t = Transition3D.createCube(400, true);
	    if (t == null)   //当t为null时,通过 CommonTransitions创建特效 //第一参数为type , 第二参数 为翻转, 第三参数时间
	      t = CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, true, 700);
	    f.setTransitionInAnimator(t);
	    
	    t = Transition3D.createCube(400, false);
	    if (t == null)                    // type是"SLIDE_VERTICAL"为上下切换,SLIDE_HORIZONTAL为左右切换
	      t = CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, false, 700);
	    f.setTransitionOutAnimator(t);

	    f.show();
	}

}


你可能感兴趣的:(F#,sun)