//--------------------------------------------------------------------------- ■Panelを土台にした部品配置とイベント処理・レイアウト設定の例 //--------------------------------------------------------------------------- import com.nttdocomo.ui.*; // IApplicationのサブクラスとして"PanelEvent01"を宣言 // ※IApplicationは、iアプリ作成時、必須クラスです。 public class PanelEvent01 extends IApplication { // 初期化処理 // ※iアプリ起動時に、この"start"メソッドが呼ばれる public void start() { // "Mypanel"オブジェクトを生成 Mypanel panel = new Mypanel(); // 現在の画面に設定 Display.setCurrent(panel); } // Panelのサブクラスとして内部クラス"Mypanel"を宣言 class Mypanel extends Panel implements ComponentListener { // 変数の宣言 private Button button1,button2; private TextBox textbox; //コンストラクタ public Mypanel(){ //HTML風レイアウトを設定 HTMLLayout hl = new HTMLLayout(); this.setLayoutManager(hl); //部品を中央寄せ hl.begin(HTMLLayout.CENTER); // ボタン作成 button1 = new Button("ボタン1"); button2 = new Button("ボタン2"); // パネルに追加 add(button1); add(button2); hl.br(); hl.br(); // テキストボックス textbox = new TextBox("テキストボックス", 18, 2, TextBox.DISPLAY_ANY); // パネルに追加 add(textbox); //部品を中央寄せを終える hl.end(); // イベントリスナーのインスタンス登録 // コンポーネントリスナー setComponentListener(this); } // コンポーネントイベント発生時に実行--------------------------------------- public void componentAction(Component component, int type, int param){ // "ボタン"が押された時に実行 if((component == button1) && (type == BUTTON_PRESSED)){ textbox.setText("ボタン1が押された"); } if((component == button2) && (type == BUTTON_PRESSED)){ textbox.setText("ボタン2が押された"); } } //-------------------------------------------------------------------------- }//class Mypanelの終り }//public class PanelEvent01の終り //--------------------------------------------------------------------------- ■Panelを土台にした部品配置とイベント処理・リスト選択の例 //--------------------------------------------------------------------------- import com.nttdocomo.ui.*; public class PanelList01 extends IApplication { // 初期化処理 // ※iアプリ起動時に、この"start"メソッドが呼ばれる public void start() { // "Mypanel"オブジェクトを生成 Mypanel panel = new Mypanel(); // 現在の画面に設定 Display.setCurrent(panel); } // Panelのサブクラスとして"Mypanel"を宣言 class Mypanel extends Panel implements ComponentListener { // 変数の宣言 private ListBox listbox; private TextBox textbox; public Mypanel(){ this.setTitle("アンケート"); //HTML風レイアウトを設定 HTMLLayout hl = new HTMLLayout(); this.setLayoutManager(hl); //余白を空ける hl.br();hl.br(); //部品を中央寄せ hl.begin(HTMLLayout.CENTER); // ボタン作成 Label label = new Label("Q1. 野球は好きですか?"); add(label); hl.br(); listbox = new ListBox(ListBox.SINGLE_SELECT, 3); add(listbox); listbox.append("好き"); listbox.append("嫌い"); listbox.append("どちらでもない"); listbox.setSize(100,42); hl.br(); hl.br(); // テキストボックス textbox = new TextBox("テキストボックス", 18, 1, TextBox.DISPLAY_ANY); textbox.setEditable(false); // パネルに追加 add(textbox); //部品を中央寄せを終える hl.end(); // イベントリスナーのインスタンス登録 // コンポーネントリスナー setComponentListener(this); } // コンポーネントイベント発生時に実行 public void componentAction(Component component, int type, int param){ // "ボタン"が押された時に実行 if((component == listbox) && (type == SELECTION_CHANGED)){ String num_string = listbox.getItem(param); textbox.setText(num_string); } } } }//終り //--------------------------------------------------------------------------- ■Panelを土台にしたリスト部品配置とイベント処理2・複数リスト選択の例 //--------------------------------------------------------------------------- import com.nttdocomo.ui.*; public class PanelList02 extends IApplication { // 初期化処理 // ※iアプリ起動時に、この"start"メソッドが呼ばれる public void start() { // "Mypanel"オブジェクトを生成 Mypanel panel = new Mypanel(); // 現在の画面に設定 Display.setCurrent(panel); } // Panelのサブクラスとして"Mypanel"を宣言 class Mypanel extends Panel implements ComponentListener,SoftKeyListener,KeyListener { // 変数の宣言 private ListBox listbox; private TextBox textbox, textbox2, textbox3; public Mypanel(){ this.setSoftLabel(Frame.SOFT_KEY_1,"終了"); this.setTitle("アンケート"); //HTML風レイアウトを設定 HTMLLayout hl = new HTMLLayout(); this.setLayoutManager(hl); //余白を空ける hl.br();hl.br(); //部品を中央寄せ hl.begin(HTMLLayout.CENTER); // ボタン作成 Label label = new Label("Q. 好きな果物を選んでください(複数可)"); add(label); hl.br(); //複数選択可能なリストボックスを生成 listbox = new ListBox(ListBox.MULTIPLE_SELECT); add(listbox); listbox.append("ココナッツ"); listbox.append("パパイヤ"); listbox.append("マンゴー"); listbox.setSize(70,42); hl.br(); hl.br(); // テキストボックス textbox = new TextBox("", 10, 1, TextBox.DISPLAY_ANY); textbox.setEditable(false); // パネルに追加 add(textbox); textbox2 = new TextBox("", 10, 1, TextBox.DISPLAY_ANY); textbox2.setEditable(false); // パネルに追加 add(textbox2); textbox3 = new TextBox("", 10, 1, TextBox.DISPLAY_ANY); textbox3.setEditable(false); // パネルに追加 add(textbox3); // 部品を中央寄せを終える hl.end(); // イベントリスナーのインスタンス登録 // コンポーネントリスナー setComponentListener(this); setSoftKeyListener(this); } // コンポーネントイベント発生時に実行--------------------------------------- public void componentAction(Component component, int type, int param){ // "ボタン"が押された時に実行 if((component == listbox) && (type == SELECTION_CHANGED)){ //選択されていればテキストボックス1に記載する if(listbox.isIndexSelected(0)){ String num_string = listbox.getItem(0); textbox.setText(num_string); }else{//選択されていなければテキストボックス1を空にする textbox.setText(""); } //選択されていればテキストボックス2に記載する if(listbox.isIndexSelected(1)){ String num_string = listbox.getItem(1); textbox2.setText(num_string); }else{//選択されていなければテキストボックス2を空にする textbox2.setText(""); } //選択されていればテキストボックス3に記載する if(listbox.isIndexSelected(2)){ String num_string = listbox.getItem(2); textbox3.setText(num_string); }else{//選択されていなければテキストボックス3を空にする textbox3.setText(""); } }//ifの終り }//------------------------------------------------------------------- public void softKeyPressed(int arg0) { // TODO 自動生成されたメソッド・スタブ } public void softKeyReleased(int arg0) { // ソフトキー1が離された時に実行 if(arg0 == Frame.SOFT_KEY_1){ // iアプリ自体を終了させるメソッド terminate(); } } public void keyPressed(Panel arg0, int arg1) { // TODO 自動生成されたメソッド・スタブ } public void keyReleased(Panel arg0, int arg1) { // TODO 自動生成されたメソッド・スタブ } } }//終り //--------------------------------------------------------------------------- ■i-modeのスクラッチパッド(データ保存領域)を利用したメモ・アプリ //--------------------------------------------------------------------------- import com.nttdocomo.ui.*; import javax.microedition.io.*; import java.io.*; public class DataSaveIappli extends IApplication { // 初期化処理 // ※iアプリ起動時に、この"start"メソッドが呼ばれる public void start() { // "mypanel"オブジェクトを生成 mypanel w_panel = new mypanel(); // ソフトキーのラベル設定 w_panel.setSoftLabel(Frame.SOFT_KEY_1,"終了"); w_panel.setSoftLabel(Frame.SOFT_KEY_2,"保存"); // 現在の画面に設定 Display.setCurrent(w_panel); } // Panelのサブクラスとして"mypanel"を宣言 class mypanel extends Panel implements ComponentListener,SoftKeyListener { // 変数の宣言 private Button w_button; private TextBox w_textbox; private String ww_textbox; public mypanel(){ // テキストボックス w_textbox = new TextBox("テキストボックス", 18, 5, TextBox.DISPLAY_ANY); // パネルに追加 add(w_textbox); // ボタン作成 w_button = new Button("ScratchPad読込"); // パネルに追加 add(w_button); // イベントリスナーのインスタンス登録 // コンポーネントリスナー setComponentListener(this); // ソフトキーリスナー setSoftKeyListener(this); } // コンポーネントイベント発生時に実行 public void componentAction(Component w_component, int w_type, int w_param){ if((w_component == w_button) && (w_type == BUTTON_PRESSED)){ // ScratchPad読込 load(); } } // ソフトキーが離されたときに実行 public void softKeyReleased(int w_param){ // ソフトキー1が離された時に実行 if(w_param == Frame.SOFT_KEY_1){ // iアプリ終了 terminate(); } // ソフトキー2が離された時に実行 if(w_param == Frame.SOFT_KEY_2){ // テキストボックスからデータを受取 ww_textbox = w_textbox.getText(); // 保存 save(); } } // ソフトキーが押されたときに実行 // ※今回は使用しないが、記述しておかないとコンパイルエラーになる public void softKeyPressed(int w_param){ } //---------------------------------------------------------------------# // 保存データ読み込み //---------------------------------------------------------------------# private void load(){ try{ // scratchpadから、テキストデータを読込 DataInputStream w_datainput = Connector.openDataInputStream("scratchpad:///0"); ww_textbox = w_datainput.readUTF(); w_datainput.close(); }catch(IOException ie){ System.out.println("load err!!"); } // データをパネルに表示 w_textbox.setText(ww_textbox); } //---------------------------------------------------------------------# // データ保存 //---------------------------------------------------------------------# public void save(){ try{ // テキストデータを、scratchpadに書込 DataOutputStream w_dataoutput = Connector.openDataOutputStream("scratchpad:///0"); w_dataoutput.writeUTF(ww_textbox); w_dataoutput.close(); } catch(IOException ie) { System.out.println("save err!!"); } } } }//終り