javaMe Tab

 首先,先来说说“分页”,Win32控件中,有种控件叫做Tab,这个功能是把一个窗体分层,同时可以容纳更多控件(注:这里的分页并不是B/S的网络数据库中的分页)。

  在J2ME中实现Tab,可谓是件好事,包含Tab控件的Class可以作为主类,从而根据需求关联更多的应用。然后,使用Tab效果最好的是触摸屏手机...........

  LWUIT里的Text控件,跟原来的Text控件差不多,很多人都疑惑:Text控件输入汉字时,到底是用高级的输入框,还是在当前界面输入........答案是调用高级输入框!

  OK,废话少说,直接来代码,这里的代码也是修改自Sample例子:

   1. /*
   2.  * Copyright ?2008 Sun Microsystems, Inc. All rights reserved.
   3.  * Use is subject to license terms.
   4.  *
   5.  */
   6. package com.sun.lwuit.uidemo;
   7.
   8. import com.sun.lwuit.Button;
   9. import com.sun.lwuit.ButtonGroup;
  10. import com.sun.lwuit.Command;
  11. import com.sun.lwuit.Container;
  12. import com.sun.lwuit.Dialog;
  13. import com.sun.lwuit.Form;
  14. import com.sun.lwuit.Label;
  15. import com.sun.lwuit.RadioButton;
  16. import com.sun.lwuit.TabbedPane;
  17. import com.sun.lwuit.TextArea;
  18. import com.sun.lwuit.TextField;
  19. import com.sun.lwuit.events.ActionEvent;
  20. import com.sun.lwuit.events.ActionListener;
  21. import com.sun.lwuit.layouts.BorderLayout;
  22. import com.sun.lwuit.layouts.BoxLayout;
  23.
  24. /**
  25.  * 本例演示如何使用Tabbed、Text控件
  26.  */
  27. public class TabbedPaneDemo implements ActionListener {
  28.     public Form form = new Form("TabbedPaneDemo");
  29.     private  Command backCommand = new Command("Back", 1);
  30.     final TextArea title ;
  31.     final TextArea body;
  32.     TabbedPane tp = null;
  33.
  34.     TabbedPaneDemo() {
  35.         form.setLayout(new BorderLayout());
  36.         form.setScrollable(false);
  37.         form.addCommand(backCommand);
  38.         form.setCommandListener(this);
  39.         tp = new TabbedPane();
  40.
  41.         //addTab可以为页面添加控件,也可以是Container(相当于容器的控件)
  42.         tp.addTab("Tab 1", new Label("Welcome to TabbedPane demo!"));
  43.
  44.         //---------------------第二页的内容------------------------------------
  45.         //Container就是一个控件,只不过相当于容器,建议每页有自己的事件处理
  46.         Container radioButtonsPanel = new Container(new BoxLayout(BoxLayout.Y_AXIS));
  47.
  48.         RadioButton topRB = new RadioButton("Top");
  49.         RadioButton LeftRB = new RadioButton("Left");
  50.         RadioButton BottomRB = new RadioButton("Bottom");
  51.         RadioButton RightRB = new RadioButton("Right");
  52.
  53.         RadioListener rbListener = new RadioListener();//自定义接收事件的类
  54.         topRB.addActionListener(rbListener);
  55.         LeftRB.addActionListener(rbListener);
  56.         BottomRB.addActionListener(rbListener);
  57.         RightRB.addActionListener(rbListener);
  58.
  59.         ButtonGroup group1 = new ButtonGroup();
  60.         group1.add(topRB);
  61.         group1.add(LeftRB);
  62.         group1.add(BottomRB);
  63.         group1.add(RightRB);
  64.
  65.         radioButtonsPanel.addComponent(new Label("Please choose a tab placement direction:"));
  66.         radioButtonsPanel.addComponent(topRB);
  67.         radioButtonsPanel.addComponent(LeftRB);
  68.         radioButtonsPanel.addComponent(BottomRB);
  69.         radioButtonsPanel.addComponent(RightRB);
  70.
  71.         tp.addTab("Tab 2", radioButtonsPanel);
  72.
  73.         //----------------------第三页的内容----------------------------------
  74.         //Container就是一个控件,只不过相当于容器,建议每页有自己的事件处理
  75.         Container TextPanel = new Container(new BoxLayout(BoxLayout.Y_AXIS));
  76.         ButtonListener txtListener = new ButtonListener();//按钮事件处理
  77.
  78.         title = new TextField("Title");   
  79.         title.getStyle().setBgTransparency(100);
  80.
  81.         body = new TextArea("This is the body of the alert....", 3, 20);
  82.         body.getStyle().setBgTransparency(100);
  83.
  84.         final Button ShowMessage =new Button("ok");
  85.         ShowMessage.getStyle().setBgTransparency(100);
  86.         ShowMessage.addActionListener(txtListener);
  87.
  88.         TextPanel.addComponent(title);
  89.         TextPanel.addComponent(body);
  90.         TextPanel.addComponent(ShowMessage);
  91.
  92.
  93.         tp.addTab("Tab 3", TextPanel);
  94.
  95.         form.addComponent("Center", tp);
  96.     }
  97.
  98.     /** 监听 radio buttons事件 */
  99.     class RadioListener implements ActionListener {
 100.         public void actionPerformed(ActionEvent e) {
 101.             String title = ((RadioButton) e.getSource()).getText();
 102.             Dialog.show("TabbedPaneDemo", title, "OK", null);
 103.         }
 104.     }
 105.
 106.     /** 监听 buttons事件 */
 107.     class ButtonListener implements ActionListener {
 108.         public void actionPerformed(ActionEvent e) {
 109.            Dialog.show(title.getText(), body.getText(), "OK", null);
 110.         }
 111.     }
 112.
 113.     /** 监听command事件 */
 114.     public void actionPerformed(ActionEvent arg0) {
 115.         UIDemoMIDlet.backToMainMenu();
 116.     }
 117. }
118.

你可能感兴趣的:(java,职场,休闲,me)