DBMNG数据库管理与应用

科学是实事求是的学问,来不得半点虚假。
当前位置:首页 > 移动应用 > JavaME

七、LWUIT的使用

The Lightweight UI Toolkit (LWUIT) 是一个轻量级JavaME UI工具包。LWUIT类似Swing 的MVC架构, 支持多种布局(Layouts), 皮肤更换, 字体, 触摸屏, 动画效果, Rich控件, 3D集成, Painter, 模式对画框, I18N/L10N等。
    学习Lwuit时,一些基础知识是一定要掌握的,以下是一些基本应用,包括组件的一些基本属性,使用方式,以及布局,并提及一些需要注意的地方。
    在MIDlet中初始化Lwuit 也是唯一使用原有J2me的东西
      可以在startApp方法中定义启动的过程
        public void startApp() {
            //init the LWUIT Display
            Display.init(this);
            //加载资源文件
      try {
      Resources r = Resources.open("/myresources.res");
      UIManager.getInstance().setThemeProps(r.getTheme(
      r.getThemeResourceNames()[0])
      ); }
      catch (java.io.IOException e) {
      }
简单的使用一个内置Form进行显示,而无须使用一个而外的类
   Form f = new Form();
   f.setTitle("Hello World");
   f.setLayout(new BorderLayout());
   f.addComponent("Center", new Label("I am a Label"));
   f.show();
   注意使用show方法进行显示

组件介绍:
1. Component,Container,Form.
2. Label.
3. Button.
4. RadioButton,ButtonGroup,CheckBox,ComboBox.
5. TextArea.
6. TabbedPane.
7. 使用 Lists.
8. ListCellRenderer 渲染器.
9. 使用 Dialogs 对话框.
10. 使用LayoutManagers 使用布局管理器(BorderLayout,BoxLayout,FlowLayout,GridLayout,GroupLayout)
11. 使用 Painter.
12. Using the Style Object 设置样式对象.
13. Theming 皮肤.
14. Resources 资源.
15. Localization (L10N) 本地化.
16. M3G 用于实现3D功能,需要手机的支持 Jsr184.
17. Logging 日志,可以保存在RMS或者本地文件中.
18. 创建自定义的组件,只要继承Component对象,然后重写paint方法即可,类似Canvas的操作.
19. 用于在窗口变化时候获取合适的显示效果.
20. 影响内存使用的因素.
21. Speed 速度方面.

1. Component,Container,Form
      Component:用于显示到页面上的基础部分,可以理解为DisplayObject,也就是组件部分.
      Container:用于保存多个Components的容器类型,提供了布局的能力.
      Form : 提供title和menus,并且提供一个content放置Components,并且提供了content的滚动能力,addComponent用于添加Components到Content中.

  提供的一些方法和使用方式:
      Form mainForm = new Form("Form Title");  //声明一个带标题的Form
      mainForm.setLayout(new BorderLayout()); //设置布局管理器
      mainForm.addComponent(BorderLayout.CENTER, new Label(“Hello,World”)); //添加Components组件,
      mainForm.setTransitionOutAnimator(CommonTransitions.createFade(400)); //设置动画
      mainForm.addCommand(new Command("Run", 2)); //添加菜单命令
      mainForm.show(); //显示Form

  一些技巧:  
      1:可以使用带参数的构造函数,设置标题.也可以不设置标题的构造函数
      2:addComponent() 方法提供两个重载,区别在于是否指定布局位置
      3:Transition动画用于在forms之间进行切换时候触发
      4:Form通过Commands添加菜单,根据添加顺序排序显示,其他使用与普通的J2me一样

2. Label
      Label: 可以同时显示图片和文字,设置align布局,可以通过继承Label实现扩展,如果更复杂,可以使用Button

 提供的一些方法和使用方式:
      Label textLabel = new Label("I am a Label");  //创建一个单行文本
      Image icon = Image.createImage("/images/duke.png");
      Label imageLabel = new Label(icon);   //创建一个带图片的Label
      setTextPosition(int alignment); //设置布局方式,CENTER, LEFT, RIGHT,LEFT,TOP, BOTTOM, LEFT, RIGHT.

3. Button
      Button 可以用于触发一个点击的事件Action,使用一个ActionListeners 进行监听
      final Button button  = new Button("Old Text");
      //使用匿名的ActionListener接口实现类进行监听
      button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
      button.setText("New Text");
      }});
      Button继承Label, 所以可以设置不同图文混编的风格,并且提供了三种状态ollover, pressed, and the default state.

4. RadioButton,ButtonGroup,CheckBox,ComboBox

 RadioButton  单选按钮,继承自Button 
      RadioButton radioButton = new RadioButton(“Radio Button”);
      同样,也会触发action

 ButtonGroup 用于保证RadioButton只能选中一项 
      RadioButton rb1 = new RadioButton("First RadioButton in Group 1");
      RadioButton rb2 = new RadioButton("Second RadioButton in Group 1");
      ButtonGroup group1 = new ButtonGroup();
      group1.add(rb1);
      group1.add(rb2);
      exampleContainer.addComponent(rb1);
      exampleContainer.addComponent(rb2);
      这里需要注意,只需要添加addComponent(RadioButton) 到容器中,不需要添加ButtonGroup,ButtonGroup只用于后台管理.

 CheckBox  多选框,不需要添加到group中,同样也触发事件 
      final CheckBox checkBox = new CheckBox(“Check Box”);
      通过checkBox.isSelected() 判断是否被选中.

 ComboBox  列表,只允许一个被选中,多用于空间有限时候提供多选项的单选功能 
      创建需要一个数组作为数据源
      String[] content = { "Red", "Blue", "Green", "Yellow" };
      ComboBox comboBox = new ComboBox(content); //创建一个Combox,在这个构造函数中也是使用List实现
      comboBox.setListCellRenderer(new checkBoxRenderer()); //设置renderer 渲染器
      也可以使用addActionListener注册事件
      通过实现不同的renderer接口可以给组件提供不同的显示效果 implements ListCellRenderer

5. TextArea
 例子 
      TextArea textArea = new TextArea(5, 20, TextArea.NUMERIC);
      textArea.setEditable(false);
      构造函数的前两个参数为 总行数和单行长度,第三个参数用于传递到本地文本输入,用于指定限制的类型
      可以使用TextArea的常量 ANY,EMAILADDR, NUMERIC, PHONENUMBER, URL, or DECIMAL,PASSWORD, UNEDITABLE, SENSITIVE,NON_PREDICTIVE, INITIAL_CAPS_SENTENCE, INITIAL_CAPS_WORD.也可以使用 | 设置复合约束 ,默认情况下可编辑

6. TabbedPane  
      TabbedPane 通过类似Tab的选项卡方式并排放置一组容器,容器的tab标签可以使用图文混编方式
      可以使用addTab or insertTab方法添加容器
      removeTabAt(int index) 移除容器
      tab的索引由0开始
      tab标签可以有四种不同的放置方法 LEFT, RIGHT, TOP or BOTTOM using the setTabPlacement method.
      TabbedPane tabbedPane = new TabbedPane(TabbedPane.TOP);
      tabbedPane.addTab("Tab 1", new Label("I am a TabbedPane!"));
      tabbedPane.addTab("Tab 2", new Label("Tab number 2"));
      构造函数使用一个Tab标签布局的常量作为参数
      addTab,第一个为tab标签名,第二个参数为直接放置到容器中的内容

7. List
 List,使用Swing的MVC模式进行创建 
      rendered using a ListCellRenderer and are extracted using the ListModel.
      使用ListCellRenderer 作为显示的V 
      使用ListModel 作为数据源M
      其他部分为C进行操作
 创建List 有四个构造函数 
      List()           
      List(ListModel model)   //直接实例化ListModel
      List(Object[] items) //Array数组
      List(Vector items) //Vector集合
 ListModel也可以分为自定ListModel的实现,以及DefaultListModel 
      DefaultListModel的使用
      String[] items = { "Red", "Blue", "Green", "Yellow" };
      DefaultListModel myListModel = new DefaultListModel(items);  //使用Array作为数据源

8. ListCellRenderer 渲染器
 同样也可以自定ListCellRenderer的实现,以及DefaultListCellRenderer 
      自定义的时候,可以通过继承Label来节约开发的速度.主要实现两个方法:
      public class MyYesNoRenderer extends Label implements ListCellRenderer{
      public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
      ((Boolean)value).booleanValue()  来获取单项的值
      }

      public Component getListFocusComponent(List list) {
      Label label = new label("");
      label.getStyle().setBgTransparency(100);
      return label;
      }}
 DefaultListCellRenderer,默认也是使用Label and the ListCellRenderer interface. 
      实现两个方法,大体和上面相同
      getListCellRendererComponent()
      getListFocusComponent()  //选中风格,可以通过修改透明度来表现


 添加或移除item 
      添加的方法有两种,创建一个ListModel,然后add到list中,重置一个List 使用setModel(ListModel model). 方法
      移除的方法有removeItem(int index) or removeAll() methods.
      myListModel.addItem(“New Item”); //添加Item
      myListModel.removeItem(index);   //删除Item
      myListModel.removeAll(); //删除所有Item

 List 事件提供了两种事件 ActionEvent and SelectionsListener 
      addFocusListener(FocusListener l) 区别于常用的Action
      SelectionListener 应该会更有用一些

 Fixed Selection Feature 应该是只有一行的List,不弹出菜单的模式 //具体还是demo才知道 
      通过 setFixedSelection(int fixedSelection) 进行设置
      通过四个常量属性进行设置
      FIXED_NONE   //普通的List
      FIXED_TRAIL  //静态
      FIXED_LEAD   // ...下面两种都需要用demo看下区别
      FIXED_CENTER
      setSmoothScrolling(true) //设置这个属性,应该是在移动时增加动画效果,默认为false

9. Dialogs对话框
      也提供一个Content,可以存放其他的Components  //对于是否阻塞线程,需要了解一下

 Dialog提供了五种不同的类型,默认附加了不同的提示音 
      ALARM //警告
      CONFIRMATION //确认
      ERROR //错误
      INFO //信息
      WARNING //警告提示 
      当前默认情况下不支持Icon,不过可以自定义Dialog


 调用dialog的show()方法进行显示,提供的参数很多,可以选择合适的重载进行显示 
      1:String title  标题
      2:Component body 一个单独的Component
      3:String text 用于代替Component body部分
      4:Command[] cmds 可以添加Commands
      5:int type 设置不同的类型
      6:Image icon 添加icon
      7:timeout 打开持续时间,设置成0表示为持续打开
      8:Transition transition 添加动画
      9:String okText 设置Ok按钮的内容
      10:String cancelText 设置cancel的内容
      11:int top 设置top所在位置
      12:int bottom 设置bottom位置
      13:int left  同上
      14:int right 同上
      15:boolean includeTitle  //需要了解下

      show方法可以返回三个类型 void,Command,boolean
      boolean值在点击ok时为true, cancel时为null

      使用非静态的方法显示Dialog
      dialog.show(90, 90, 10, 10, true);

      关闭dialog  dispose()
      需要先show之前设置timer去关闭dialog

      Dialog也可以返回一个Command 区别于点击OK按钮


10. LayoutManagers布局管理器 
      BorderLayout
      BoxLayout
      FlowLayout
      GridLayout
      GroupLayout

 BorderLayout 边框布局模式(东西南北中) 
      Center
      East
      North
      South
      West

      在添加Components的时候进行设置
      addComponent(BorderLayout.CENTER, component) // preferred
      或者直接使用字符串
      addComponent(“Center”, component) // valid but error prone  容易出错

 BoxLayout  盒子布局 
      包括了 X_AXIS(横向)  Y_AXIS(竖向)的布局,并排的方式
      BoxLayout boxLayout = new BoxLayout(BoxLayout.X_AXIS);
      BoxLayout boxLayout = new BoxLayout(BoxLayout.Y_AXIS);
 FlowLayout 流式布局 
      这个也是容器默认的布局方式
      横向布局,如果长度超出,则自动换行处理
      FlowLayout exampleLayout = new FlowLayout();
      container.setLayout(exampleLayout);
      也可以通过设置构造函数,用于设置布局起始的位置,比如 Left, Right, or Center
      FlowLayout exampleLayout = new FlowLayout(Component.RIGHT);
 GridLayout 表格式布局,可以用于设置九宫图 
      可以定制行和列,
      GridLayout exampleLayout = new GridLayout(0,2);  //2表示每行显示两个单元格cell,0不知道是啥
 GroupLayout  
      // GUI builders 的方式进行布局,用于NetBeans中的Swing开发

11. Painter
      是一个接口,可以用于绘制一个Components的背景,将绘制在Components限定的范围内
      如果要查看painter绘制结果,需要设置对应的transparency //需要查看下
      painter可以使用绝对坐标的绘图,所以可以重用在其他组件之上
      Painter diagonalPainter = new Painter() {
      public void paint(Graphics g, Rectangle rect) {
            g.drawLine(rect.getX(),
            rect.getY(),
            rect.getX() + rect.getSize().getWidth(),
            rect.getY() + rect.getSize().getHeight());
            }      
      };
      注意查看这里获取位置的方法 x y, size width height
      使用时候只要给指定的组件设置Painter即可
      myComponent.getStyle().setBgPainter(diagonalPainter);
      将会自动应用绘图

      记得显示的调用设置透明度  0-255之间
      myButton.getStyle().setBgTransparency(100);

12。Using the Style Object 设置样式对象
      The Style object sets colors, fonts, transparency, margin, padding, images, and borders to define the style for a given component.
      使用component.getStyle(). 获取该对象,可以在运行时候改变
 颜色Style 
      Foreground color 前景色,主要指文字颜色
      Foreground selection color 当组件获取焦点时,字体的颜色
      Background color 背景色
      Background selection color  获取焦点时候的背景色
      这里颜色使用的是RGB(格式为0x000000),不带alpha透明色,有而外的透明设置在Style中

 Font 字体 
 Transparency 透明度 
      setBgTransparency进行设置 范围是0-255之间

 Margin and Padding 外边距和内间距,与css的盒模型一样 
      设置的方式
      setPadding(int top, int bottom, int left, int right)
      setPadding(int orientation, int gap)
      setMargin(int top, int bottom, int left, int right)
      setMargin(int orientation, int gap)
      // orientation can be Component.TOP, BOTTOM, LEFT or RIGHT
      这两个类型方法的区别在于同时设置一个属性,还是多个属性

 Images  背景色,默认为不限制背景色  使用bgImage 可以用于进行设置 
 Borders边框类型 
 Style Listener 样式事件 
      myComponent.getStyle().addStyleListener(new StyleListener() {
      public void styleChanged(String propertyName, Style source) {}
      }}

      Painters也可以用于在Style中设置样式,用于按照要求绘制背景
      mycomponent.getStyle().setBgPainter(myPainter);

13. Theming 皮肤
      refreshTheme()方法用于更新所有的样式,不过手动修改的样式元素,并不会被更新

 设置样式属性需要注意的地方 
      Button.font – font for all buttons //将会作用到所有Button组件的字体
      font- 将会影响到所有没有定义默认值的组components件
      这里不像css那样有特殊的单独样式设置

 所有支持的样式属性 
      fgColor   前景色 16进制的格式
      bgColor   背景色 16进制的格式,也可以使用缩写ff
      bgSelectionColor 背景选中色 16x
      fgSelectionColor 前景选中色 16x
      bgImage    背景图片
      transparency  设置背景图样式透明色 0-255之间,不过目前对背景图无效

 font 分为Bitmap font和System font 定义的方式为Bitmap{myFontName} 和System{face;style;size} 
      其中系统字体的参数为
      face=FACE_SYSTEM,FACE_PROPORTIONALFACE_PROPORTIONAL
      style=STYLE_PLAIN,STYLE_BOLD,STYLE_ITALIC
      size=SIZE_MEDIUM,SIZE_SMALL,SIZE_LARGE
      例如
      System{FACE_SYSTEM;STYLE_PLAIN;SIZE_SMALL}

      scaledImage      使用boolean值设置背景图使用平铺tiled还是重复scaled  默认为true,重复scaled
      margin 外边距,使用四个参数进行设置,代表top, bottom, left, and right. For example, 1, 2, 3, 4
      padding 内边距,使用与margin类似

 更新主题时 
      使用Resources.open("/javaTheme.res");//进行加载
      使用UIManager.getInstance().setThemeProps(r.getTheme(r.getThemeResourceNames()[0]));//进行设置
      然后可以使用components.refreshTheme 更新组件的样式

 LookAndFeel  DefaultLookAndFeel  似乎是用来定义一些样式无法修改到的东西,比如滚动条,具体要看API Documention 

14. Resources 资源
      LWUIT允许的resource elements的元素包括:
      Image Resources       //图像资源
      Animation Resources   //动画资源
      Bitmap Fonts       //位图字体
      Localization (L10N)   //本地化
      Themes       //主题

      可以通过专门的编辑器 Resource Editor 或者Ant tasks来创建Resources

      Loading a Resource 读取一个资源文件
      Resources res = Resources.open(“/myresourceFile.res”);
      Image i = res.getImage(“imageName”);

      图片可以保存在res文件中,或者直接放置在jar文件中

      读取jar中图片的方式
      Image image = Image.createImage("/images/duke.png");

      image的标签,应该是res配置的时候,可以有三个属性
      name //名称,默认为文件名
      file //图像文件所在位置
      pack //boolean值 ,默认为false ,用于设置是否为Indexed

      图像少于256色的时候,可以使用Index image,有利于节约空间


 使用indexed image的注意地方 
      1:因为需要按照像素的去操作图像,所以在显示的时候速度比较慢,不过在现在的手机上并不明显
      2:在运行时转换indexed image将会比较慢,而且可能会失败,所以应该在编译时候进行转换
      3:Indexed image并没有在res文件中被压缩,所以res文件看起来比较大,不过在jar文件中却能被很好的压缩,而且效果比使用 png更好
      4:indexed images  创建后将不能被改变,比如getGraphics(),详情看API


Animation Resources 
      LWUIT支持从Gif文件中得到动画效果,保存的结构类似image structure
      StaticAnimation 用于操作动画..派生自Image,所以可以直接使用在icon或者其他地方

Fonts 字体 
      LWUIT支持bitmap fonts and system fonts.
      System Font  包括了三个参数
      Face   //如FACE_SYSTEM, FACE_PROPORTIONAL and FACE_MONOSPACE.
      Style  //如STYLE_PLAIN, STYLE_ITALIC, STYLE_BOLD.
      Size   //如SIZE_SMALL, SIZE_MEDIUM, SIZE_LARGE.

      在类的内部可以去创建字体引用
      Font.createSystemFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_MEDIUM);

      同时也可以去创建复合类型的字体(加粗和倾斜)
      Font.createSystemFont(Font.FACE_SYSTEM,Font.STYLE_BOLD | Font.STYLE_ITALIC,Font.SIZE_MEDIUM);

      Bitmap Fonts
      三个属性
      name  //在res文件中,用于程序加载
      charset //就是包含的字符,默认为24个字母,数字和一些符号
      src //字体文件TrueType所在位置
      size    //字体大小
      bold //是否加粗,默认为false
      trueType //默认为true,根据src属性决定是否生效
      antiAliasing //默认为true,如果为false表示别名
      logicalName  //逻辑名称,表示不同的字体编码类型,类似css中使用的4个大区字体
      //这里注意文档中只介绍了使用ant创建Bitmap font

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号