本文來自:http://blog.csdn.net/hellogv/ ,轉載必須註明出處!
首先,想讓大家看看本例實現之後的動畫:
首先,先來說說“分頁”,Win32控制項中,有種控制項叫做Tab,這個功能是把一個表單分層,同時可以容納更多控制項(註:這裡的分頁並不是B/S的網路資料庫中的分頁)。
在J2ME中實現Tab,可謂是件好事,包含Tab控制項的Class可以作為主類,從而根據需求關聯更多的應用。然後,使用Tab效果最好的是觸控螢幕手機...........
LWUIT裡的Text控制項,跟原來的Text控制項差不多,很多人都疑惑:Text控制項輸入漢字時,到底是用進階的輸入框,還是在當前介面輸入........答案是調用進階輸入框!
OK,廢話少說,直接來代碼,這裡的代碼也是修改自Sample例子:
- /*
- * Copyright ?2008 Sun Microsystems, Inc. All rights reserved.
- * Use is subject to license terms.
- *
- */
- package com.sun.lwuit.uidemo;
- import com.sun.lwuit.Button;
- import com.sun.lwuit.ButtonGroup;
- import com.sun.lwuit.Command;
- import com.sun.lwuit.Container;
- import com.sun.lwuit.Dialog;
- import com.sun.lwuit.Form;
- import com.sun.lwuit.Label;
- import com.sun.lwuit.RadioButton;
- import com.sun.lwuit.TabbedPane;
- import com.sun.lwuit.TextArea;
- import com.sun.lwuit.TextField;
- import com.sun.lwuit.events.ActionEvent;
- import com.sun.lwuit.events.ActionListener;
- import com.sun.lwuit.layouts.BorderLayout;
- import com.sun.lwuit.layouts.BoxLayout;
- /**
- * 本例示範如何使用Tabbed、Text控制項
- */
- public class TabbedPaneDemo implements ActionListener {
- public Form form = new Form("TabbedPaneDemo");
- private Command backCommand = new Command("Back", 1);
- final TextArea title ;
- final TextArea body;
- TabbedPane tp = null;
- TabbedPaneDemo() {
- form.setLayout(new BorderLayout());
- form.setScrollable(false);
- form.addCommand(backCommand);
- form.setCommandListener(this);
- tp = new TabbedPane();
- //addTab可以為頁面添加控制項,也可以是Container(相當於容器的控制項)
- tp.addTab("Tab 1", new Label("Welcome to TabbedPane demo!"));
- //---------------------第二頁的內容------------------------------------
- //Container就是一個控制項,只不過相當於容器,建議每頁有自己的事件處理
- Container radioButtonsPanel = new Container(new BoxLayout(BoxLayout.Y_AXIS));
- RadioButton topRB = new RadioButton("Top");
- RadioButton LeftRB = new RadioButton("Left");
- RadioButton BottomRB = new RadioButton("Bottom");
- RadioButton RightRB = new RadioButton("Right");
- RadioListener rbListener = new RadioListener();//自訂接收事件的類
- topRB.addActionListener(rbListener);
- LeftRB.addActionListener(rbListener);
- BottomRB.addActionListener(rbListener);
- RightRB.addActionListener(rbListener);
- ButtonGroup group1 = new ButtonGroup();
- group1.add(topRB);
- group1.add(LeftRB);
- group1.add(BottomRB);
- group1.add(RightRB);
- radioButtonsPanel.addComponent(new Label("Please choose a tab placement direction:"));
- radioButtonsPanel.addComponent(topRB);
- radioButtonsPanel.addComponent(LeftRB);
- radioButtonsPanel.addComponent(BottomRB);
- radioButtonsPanel.addComponent(RightRB);
- tp.addTab("Tab 2", radioButtonsPanel);
- //----------------------第三頁的內容----------------------------------
- //Container就是一個控制項,只不過相當於容器,建議每頁有自己的事件處理
- Container TextPanel = new Container(new BoxLayout(BoxLayout.Y_AXIS));
- ButtonListener txtListener = new ButtonListener();//按鈕事件處理
- title = new TextField("Title");
- title.getStyle().setBgTransparency(100);
- body = new TextArea("This is the body of the alert....", 3, 20);
- body.getStyle().setBgTransparency(100);
- final Button ShowMessage =new Button("ok");
- ShowMessage.getStyle().setBgTransparency(100);
- ShowMessage.addActionListener(txtListener);
- TextPanel.addComponent(title);
- TextPanel.addComponent(body);
- TextPanel.addComponent(ShowMessage);
- tp.addTab("Tab 3", TextPanel);
- form.addComponent("Center", tp);
- }
- /** 監聽 radio buttons事件 */
- class RadioListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- String title = ((RadioButton) e.getSource()).getText();
- Dialog.show("TabbedPaneDemo", title, "OK", null);
- }
- }
- /** 監聽 buttons事件 */
- class ButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- Dialog.show(title.getText(), body.getText(), "OK", null);
- }
- }
- /** 監聽command事件 */
- public void actionPerformed(ActionEvent arg0) {
- UIDemoMIDlet.backToMainMenu();
- }
- }