分享

解读 LWUIT 之五:LWUIT 控件(下)

 CevenCheng 2010-11-08

解读 LWUIT 之五:LWUIT 控件(下)

LWUIT 开发指南下载
作者写的 Hello TabbedPane、Hello TextArea、Hello TextField、Hello Calendar、Hello Tickering 源代码下载
        本文继上篇博客继续对 LWUIT 常见控件进行介绍。本文主要介绍剩余的几个 LWUIT 常用控件选项卡(TabbedPane)、文本区域(TextArea)、文本框(TextField)、日历(Calendar)、滚动特效(Tickering)、双向语言(Bidi)的使用并附源代码。文章对这几种控件分别进行了说明和比较。
        注:源码编写中关于 .res 的编写这里不再赘述,详细编写步骤请参考作者的前一篇博客《解读 LWUIT 之二:关于 LWUIT 开发指南中的 Hello World》。每个项目的 .res 具体配置请到作者上传源码中的 res 目录下使用 ResourceEdit 查看。

com.sun.lwuit.TabbedPane 控件
        TabbedPane 可以把不同的内容放在不同的页项下面,这些内容只有在用户选择了该页项后才显示出来。系统默认显示第一个页项的内容。HelloTabbedPane 源代码如下:

  1. package com.defonds.lwuit;  
  2.   
  3. import com.sun.lwuit.Command;  
  4. import com.sun.lwuit.Display;  
  5. import com.sun.lwuit.Form;  
  6. import com.sun.lwuit.Label;  
  7. import com.sun.lwuit.TabbedPane;  
  8. import com.sun.lwuit.animations.CommonTransitions;  
  9. import com.sun.lwuit.layouts.BorderLayout;  
  10. import com.sun.lwuit.plaf.UIManager;  
  11. import com.sun.lwuit.util.Resources;  
  12.   
  13. public class HelloMidlet extends javax.microedition.midlet.MIDlet{  
  14.       
  15.     private Form exampleContainer;// declare a Form  
  16.     private TabbedPane tabbedPane;//declare a TabbedPane  
  17.       
  18.     public void startApp() {  
  19.         // init the LWUIT Display  
  20.         Display.init(this);  
  21.         // Setting the application theme is discussed  
  22.         // later in the theme chapter and the resources chapter  
  23.         try {  
  24.             Resources r = Resources.open("/myresources.res");  
  25.             UIManager.getInstance().setThemeProps(r.getTheme("myresources"));  
  26.         } catch (java.io.IOException e) {}  
  27.           
  28.         exampleContainer = new Form("Form Title");// Create a Form  
  29.         tabbedPane = new TabbedPane(TabbedPane.TOP);// Create a TabbedPane  
  30.           
  31.         tabbedPane.addTab("Tab 1"new Label("I am a TabbedPane!"));//Add a Label to the TabbedPane  
  32.         tabbedPane.addTab("Tab 2"new Label("Tab number 2"));//Add a Label to the TabbedPane  
  33.         tabbedPane.addTab("Tab 3"new Label("Tab number 3"));//Add a Label to the TabbedPane  
  34.         tabbedPane.addTab("Tab 4"new Label("Tab number 4"));//Add a Label to the TabbedPane  
  35.           
  36.         exampleContainer.setLayout(new BorderLayout());//Set LayoutManager  
  37.         exampleContainer.addComponent(BorderLayout.CENTER,tabbedPane);//Add a TabbedPane to the Form content pane  
  38.         exampleContainer.setTransitionOutAnimator(CommonTransitions.createFade(400));//Set Transitions animation of Fade  
  39.         exampleContainer.addCommand(new Command("Run"2));//Add Command key  
  40.         exampleContainer.show();//Show it  
  41.     }  
  42.   
  43.     public void pauseApp() {}  
  44.   
  45.     public void destroyApp(boolean unconditional) {}  
  46. }  
 
        HelloTabbedPane 运行效果图如下:

HelloTabbedPane 运行效果图

com.sun.lwuit.TextArea 控件
        TextArea 调用系统本地编辑器使用户可以输入文本。程序员通过对 TextArea 的定义可以限制用户输入的内容,比如只接收数字输入。HelloTextArea 源代码如下:

  1. package com.defonds.lwuit;  
  2.   
  3. import com.sun.lwuit.Command;  
  4. import com.sun.lwuit.Display;  
  5. import com.sun.lwuit.Form;  
  6. import com.sun.lwuit.TextArea;  
  7. import com.sun.lwuit.animations.CommonTransitions;  
  8. import com.sun.lwuit.layouts.FlowLayout;  
  9. import com.sun.lwuit.plaf.UIManager;  
  10. import com.sun.lwuit.util.Resources;  
  11.   
  12. public class HelloMidlet extends javax.microedition.midlet.MIDlet{  
  13.       
  14.     private Form exampleContainer;// declare a Form  
  15.     private TextArea textArea;//declare a TextArea  
  16.       
  17.     public void startApp() {  
  18.         // init the LWUIT Display  
  19.         Display.init(this);  
  20.         // Setting the application theme is discussed  
  21.         // later in the theme chapter and the resources chapter  
  22.         try {  
  23.             Resources r = Resources.open("/myresources.res");  
  24.             UIManager.getInstance().setThemeProps(r.getTheme("myresources"));  
  25.         } catch (java.io.IOException e) {}  
  26.           
  27.         exampleContainer = new Form("Form Title");// Create a Form  
  28.         textArea = new TextArea("I'm a TextArea",2,20,TextArea.NUMERIC);// Create a TextArea  
  29.           
  30.         exampleContainer.setLayout(new FlowLayout());//Set LayoutManager  
  31.         exampleContainer.addComponent(textArea);//Add a TextArea to the Form content pane  
  32.         exampleContainer.setTransitionOutAnimator(CommonTransitions.createFade(400));//Set Transitions animation of Fade  
  33.         exampleContainer.addCommand(new Command("Run"2));//Add Command key  
  34.         exampleContainer.show();//Show it  
  35.     }  
  36.   
  37.     public void pauseApp() {}  
  38.   
  39.     public void destroyApp(boolean unconditional) {}  
  40. }  
 
        HelloTextArea 运行效果图如下:

HelloTextArea 运行效果图

com.sun.lwuit.TextField 控件
        下面来认识一下 LWUIT 中的一个闪亮的控件——TextField。大多数 JavaME 框架用户编辑文本只能调用系统本地编辑器,用户编辑文本的时候即切换到了另一个画面,就好像另启动了一个程序(事实也是如此),用户体验很是不爽。有没有一种输入控制,可以让用户在当前界面中直接输入?LWUIT 可以做到:使用 TextField。HelloTextField 源代码如下:

  1. package com.defonds.lwuit;  
  2.   
  3. import com.sun.lwuit.Command;  
  4. import com.sun.lwuit.Display;  
  5. import com.sun.lwuit.Form;  
  6. import com.sun.lwuit.TextField;  
  7. import com.sun.lwuit.animations.CommonTransitions;  
  8. import com.sun.lwuit.layouts.FlowLayout;  
  9. import com.sun.lwuit.plaf.UIManager;  
  10. import com.sun.lwuit.util.Resources;  
  11.   
  12. public class HelloMidlet extends javax.microedition.midlet.MIDlet{  
  13.       
  14.     private Form exampleContainer;// declare a Form  
  15.     private TextField textField;//declare a TextField  
  16.       
  17.     public void startApp() {  
  18.         // init the LWUIT Display  
  19.         Display.init(this);  
  20.         // Setting the application theme is discussed  
  21.         // later in the theme chapter and the resources chapter  
  22.         try {  
  23.             Resources r = Resources.open("/myresources.res");  
  24.             UIManager.getInstance().setThemeProps(r.getTheme("myresources"));  
  25.         } catch (java.io.IOException e) {}  
  26.           
  27.         exampleContainer = new Form("Form Title");// Create a Form  
  28.         textField = new TextField();// Create a TextField  
  29.           
  30.         exampleContainer.setLayout(new FlowLayout());//Set LayoutManager  
  31.         exampleContainer.addComponent(textField);//Add a TextField to the Form content pane  
  32.         exampleContainer.setTransitionOutAnimator(CommonTransitions.createFade(400));//Set Transitions animation of Fade  
  33.         exampleContainer.addCommand(new Command("Run"2));//Add Command key  
  34.         exampleContainer.show();//Show it  
  35.     }  
  36.   
  37.     public void pauseApp() {}  
  38.   
  39.     public void destroyApp(boolean unconditional) {}  
  40. }  
 
        HelloTextField 运行效果图如下:

HelloTextField 运行效果图
        任何美好的东西都有瑕疵,而看起来很美好的 TextField 的缺陷却是致命的:不支持本地语言输入(即使你的真机提供了对本地语言输入的支持),只能接收英文输入。这一个缺陷让 TextField 的用武之地大大减小。但是 TextField 确是 LWUIT 的闪光点,它证明了 LWUIT 的设计者们处处在考虑用户体验,LWUIT 是一个优秀的框架。

com.sun.lwuit.Calendar 控件
        Calendar 和 JavaME 中的 java.util.Calendar 重名,用时一定要注意。com.sun.lwuit.Calendar 用于 UI,java.util.Calendar 用来日期管理。HelloCalendar 源代码如下:

  1. package com.defonds.lwuit;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import com.sun.lwuit.Command;  
  6. import com.sun.lwuit.Display;  
  7. import com.sun.lwuit.Form;  
  8. import com.sun.lwuit.animations.CommonTransitions;  
  9. import com.sun.lwuit.events.ActionEvent;  
  10. import com.sun.lwuit.events.ActionListener;  
  11. import com.sun.lwuit.layouts.BorderLayout;  
  12. import com.sun.lwuit.plaf.UIManager;  
  13. import com.sun.lwuit.util.Resources;  
  14.   
  15. public class HelloMidlet extends javax.microedition.midlet.MIDlet implements ActionListener{  
  16.       
  17.     private Form exampleContainer;// declare a Form  
  18.     private com.sun.lwuit.Calendar calendar;//declare a Calendar  
  19.     private Date date;//declare a Date  
  20.       
  21.     public void startApp() {  
  22.         // init the LWUIT Display  
  23.         Display.init(this);  
  24.         // Setting the application theme is discussed  
  25.         // later in the theme chapter and the resources chapter  
  26.         try {  
  27.             Resources r = Resources.open("/myresources.res");  
  28.             UIManager.getInstance().setThemeProps(r.getTheme("myresources"));  
  29.         } catch (java.io.IOException e) {}  
  30.           
  31.         exampleContainer = new Form("Form Title");// Create a Form  
  32.         calendar = new com.sun.lwuit.Calendar();// Create a Calendar  
  33.         calendar.addActionListener(this);//add a listener to the Calendar     
  34.           
  35.         exampleContainer.setLayout(new BorderLayout());//Set LayoutManager  
  36.         exampleContainer.addComponent(BorderLayout.CENTER,calendar);//Add a Calendar to the Form content pane  
  37.         exampleContainer.setTransitionOutAnimator(CommonTransitions.createFade(400));//Set Transitions animation of Fade  
  38.         exampleContainer.addCommand(new Command("Run"2));//Add Command key  
  39.         exampleContainer.show();//Show it  
  40.     }  
  41.   
  42.     public void pauseApp() {}  
  43.   
  44.     public void destroyApp(boolean unconditional) {}  
  45.   
  46.     //implements the method of ActionListener    
  47.     public void actionPerformed(ActionEvent evt) {  
  48.         date = new Date(calendar.getSelectedDay());//Create a Date  
  49.         java.util.Calendar myCalendar = java.util.Calendar.getInstance();//Get a Calendar instance  
  50.         myCalendar.setTime(date);//set the Calendar time you selected  
  51.         System.out.println("The date you selected is:" + myCalendar.get(java.util.Calendar.YEAR) + "-" + (myCalendar.get(java.util.Calendar.MONTH) + 1) + "-" + myCalendar.get(java.util.Calendar.DATE));  
  52.     }  
  53. }  
 
        HelloCalendar 运行效果图如下:

HelloCalendar 运行效果图

Tickering 滚动特效
        Tickering 设置 Label 内容滚动效果。Label.startTicker(long delay, boolean rightToLeft); 方法中 delay 设置滚动速率(delay 越大,滚动地越慢),rightToLeft 可以设置滚动方向。HelloTickering 源代码如下:

  1. package com.defonds.lwuit;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import com.sun.lwuit.Button;  
  6. import com.sun.lwuit.Command;  
  7. import com.sun.lwuit.Component;  
  8. import com.sun.lwuit.Display;  
  9. import com.sun.lwuit.Form;  
  10. import com.sun.lwuit.Image;  
  11. import com.sun.lwuit.animations.CommonTransitions;  
  12. import com.sun.lwuit.events.ActionEvent;  
  13. import com.sun.lwuit.events.ActionListener;  
  14. import com.sun.lwuit.layouts.FlowLayout;  
  15. import com.sun.lwuit.plaf.UIManager;  
  16. import com.sun.lwuit.util.Resources;  
  17.   
  18. public class HelloMidlet extends javax.microedition.midlet.MIDlet {  
  19.     public void startApp() {  
  20.         // init the LWUIT Display  
  21.         Display.init(this);  
  22.         // Setting the application theme is discussed  
  23.         // later in the theme chapter and the resources chapter  
  24.         try {  
  25.             Resources r = Resources.open("/myresources.res");  
  26.             UIManager.getInstance().setThemeProps(  
  27.                     r.getTheme(r.getThemeResourceNames()[0]));  
  28.         } catch (java.io.IOException e) {}  
  29.           
  30.         Form mainForm = new Form("Form Title");// Create a Form  
  31.         //mainForm.setLayout(new BoxLayout(BoxLayout.Y_AXIS));//Set LayoutManager  
  32.         mainForm.setLayout(new FlowLayout());//Set LayoutManager  
  33.           
  34.         final Button button = new Button("Old Text");// Create a Button  
  35.         button.setAlignment(Component.CENTER);//Sets the Alignment of the button  
  36.         button.addActionListener(new ActionListener() {  
  37.             private int i = 0;  
  38.   
  39.             public void actionPerformed(ActionEvent evt) {  
  40.                 this.i++;  
  41.                 if(i % 2 == 1){  
  42.                     button.setText("New Text");// changes the text on the button,every time the user clicks it  
  43.                 }else{  
  44.                     button.setText("Old Text");// changes the text on the button,every time the user clicks it  
  45.                 }  
  46.             }  
  47.         });  
  48.         button.startTicker(66true);//start Ticker  
  49.         mainForm.addComponent(button);//Add a button to the Form content pane  
  50.           
  51.         Image image = null;//declare an image for an icon label  
  52.         try {  
  53.             image = Image.createImage("/duke3_1.gif");//create a image  
  54.         } catch (IOException e) {}  
  55.           
  56.         final Button imageButton = new Button(image);//for a image button  
  57.         imageButton.setAlignment(Component.CENTER);//Sets the Alignment of the button  
  58.         imageButton.startTicker(66true);////start Ticker  
  59.         mainForm.addComponent(imageButton);//Add a button to the Form content pane  
  60.           
  61.         final Button imageTextButton = new Button(image);//for an image and text button  
  62.         imageTextButton.setText("Image and Text Button");//set the text of the image and text button  
  63.         imageTextButton.setTextPosition(Component.RIGHT);//update the text position  
  64.         imageTextButton.setAlignment(Component.CENTER);//Sets the Alignment of the Button  
  65.         imageTextButton.startTicker(66true);////start Ticker  
  66.         mainForm.addComponent(imageTextButton);//Add a button to the content pane  
  67.           
  68.         final Button imageTextButton2 = new Button(image);//for an image and text button  
  69.         imageTextButton2.setText("Image and Text Button");//set the text of the image and text button  
  70.         imageTextButton2.setTextPosition(Component.BOTTOM);//update the text position  
  71.         imageTextButton2.setAlignment(Component.CENTER);//Sets the Alignment of the Button  
  72.         imageTextButton2.startTicker(66true);////start Ticker  
  73.         mainForm.addComponent(imageTextButton2);//Add a button to the content pane  
  74.           
  75.         mainForm.setTransitionOutAnimator(CommonTransitions.createFade(400));//Set Transitions animation of Fade  
  76.         mainForm.addCommand(new Command("Run"2));//Add Command key  
  77.         mainForm.show();//Show it  
  78.     }  
  79.   
  80.     public void pauseApp() {}  
  81.   
  82.     public void destroyApp(boolean unconditional) {}  
  83. }  
 
        HelloTickering 运行效果图如下:

HelloTickering 运行效果图

Bidi 双向语言
        Bidi 即 bidirectional,设计的本意提供双向语言支持,因为西方语言是从左到右写(LTR),而有些语言则是从右到左(RTL)。这是一个阅读/书写习惯。程序中如果想支持 RTL,只需要在 MIDlet 的 startApp 方法中加上 UIManager.getInstance().getLookAndFeel().setRTL(true); 句即可。下面是 LTR 和 RTL 两种效果图的比较。
        LTR 的 ComboBox 效果图:

LTR 的 ComboBox 效果图
        RTL 的 ComboBox 效果图:

RTL 的 ComboBox 效果图
        注意比较 LTR 和 RTL 两种方式下滚动条、选项、菜单软键等的排列方式。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多