J2ME GUI實戰之七 ———-LWUIT的Tabbed分頁、Text文本

來源:互聯網
上載者:User
本文來自:http://blog.csdn.net/hellogv/ ,轉載必須註明出處!

首先,想讓大家看看本例實現之後的動畫:

    首先,先來說說“分頁”,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. import com.sun.lwuit.Button;
  8. import com.sun.lwuit.ButtonGroup;
  9. import com.sun.lwuit.Command;
  10. import com.sun.lwuit.Container;
  11. import com.sun.lwuit.Dialog;
  12. import com.sun.lwuit.Form;
  13. import com.sun.lwuit.Label;
  14. import com.sun.lwuit.RadioButton;
  15. import com.sun.lwuit.TabbedPane;
  16. import com.sun.lwuit.TextArea;
  17. import com.sun.lwuit.TextField;
  18. import com.sun.lwuit.events.ActionEvent;
  19. import com.sun.lwuit.events.ActionListener;
  20. import com.sun.lwuit.layouts.BorderLayout;
  21. import com.sun.lwuit.layouts.BoxLayout;
  22. /**
  23.  * 本例示範如何使用Tabbed、Text控制項
  24.  */
  25. public class TabbedPaneDemo implements ActionListener {
  26.     public Form form = new Form("TabbedPaneDemo");
  27.     private  Command backCommand = new Command("Back", 1);
  28.     final TextArea title ;
  29.     final TextArea body;
  30.     TabbedPane tp = null;
  31.     TabbedPaneDemo() {
  32.         form.setLayout(new BorderLayout());
  33.         form.setScrollable(false);
  34.         form.addCommand(backCommand);
  35.         form.setCommandListener(this);
  36.         tp = new TabbedPane();
  37.         //addTab可以為頁面添加控制項,也可以是Container(相當於容器的控制項)
  38.         tp.addTab("Tab 1", new Label("Welcome to TabbedPane demo!"));
  39.         //---------------------第二頁的內容------------------------------------
  40.         //Container就是一個控制項,只不過相當於容器,建議每頁有自己的事件處理
  41.         Container radioButtonsPanel = new Container(new BoxLayout(BoxLayout.Y_AXIS));
  42.         RadioButton topRB = new RadioButton("Top");
  43.         RadioButton LeftRB = new RadioButton("Left");
  44.         RadioButton BottomRB = new RadioButton("Bottom");
  45.         RadioButton RightRB = new RadioButton("Right");
  46.         RadioListener rbListener = new RadioListener();//自訂接收事件的類
  47.         topRB.addActionListener(rbListener);
  48.         LeftRB.addActionListener(rbListener);
  49.         BottomRB.addActionListener(rbListener);
  50.         RightRB.addActionListener(rbListener);
  51.         ButtonGroup group1 = new ButtonGroup();
  52.         group1.add(topRB);
  53.         group1.add(LeftRB);
  54.         group1.add(BottomRB);
  55.         group1.add(RightRB);
  56.         radioButtonsPanel.addComponent(new Label("Please choose a tab placement direction:"));
  57.         radioButtonsPanel.addComponent(topRB);
  58.         radioButtonsPanel.addComponent(LeftRB);
  59.         radioButtonsPanel.addComponent(BottomRB);
  60.         radioButtonsPanel.addComponent(RightRB);
  61.         tp.addTab("Tab 2", radioButtonsPanel);
  62.         //----------------------第三頁的內容----------------------------------
  63.         //Container就是一個控制項,只不過相當於容器,建議每頁有自己的事件處理
  64.         Container TextPanel = new Container(new BoxLayout(BoxLayout.Y_AXIS));
  65.         ButtonListener txtListener = new ButtonListener();//按鈕事件處理
  66.         title = new TextField("Title");   
  67.         title.getStyle().setBgTransparency(100);
  68.         body = new TextArea("This is the body of the alert....", 3, 20);
  69.         body.getStyle().setBgTransparency(100);
  70.         final Button ShowMessage =new Button("ok");
  71.         ShowMessage.getStyle().setBgTransparency(100);
  72.         ShowMessage.addActionListener(txtListener);
  73.         TextPanel.addComponent(title);
  74.         TextPanel.addComponent(body);
  75.         TextPanel.addComponent(ShowMessage);
  76.         tp.addTab("Tab 3", TextPanel);
  77.         form.addComponent("Center", tp);
  78.     }
  79.     /** 監聽 radio buttons事件 */
  80.     class RadioListener implements ActionListener {
  81.         public void actionPerformed(ActionEvent e) {
  82.             String title = ((RadioButton) e.getSource()).getText();
  83.             Dialog.show("TabbedPaneDemo", title, "OK", null);
  84.         }
  85.     }
  86.     /** 監聽 buttons事件 */
  87.     class ButtonListener implements ActionListener {
  88.         public void actionPerformed(ActionEvent e) {
  89.            Dialog.show(title.getText(), body.getText(), "OK", null);
  90.         }
  91.     }
  92.     /** 監聽command事件 */
  93.     public void actionPerformed(ActionEvent arg0) {
  94.         UIDemoMIDlet.backToMainMenu();
  95.     }
  96. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.