iアプリで複数の画面(パネル)を使う方法

このQ&Aのポイント
  • iアプリで複数の画面(パネル)を使う方法について教えてください。
  • 以下のコードを実行すると、ボタンとラベルが一緒の画面に表示されます。しかし、ボタンを押した時に新しい画面(パネル)でラベルを表示したいです。
  • どのようにすれば良いでしょうか?
回答を見る
  • ベストアンサー

iアプリで複数の画面(パネル)を使いたいのですが。

以下のコードを実行するとボタンとラベルが一緒の画面に表示されますが、ボタンを押した時に新しい画面(パネル)でラベルを表示したいのです。どのようにすれば良いのでしょうか? import com.nttdocomo.ui.*; import com.nttdocomo.io.*; public class test01 extends IApplication { public void start() { mypanel w_panel = new mypanel(); w_panel.setSoftLabel(w_panel.SOFT_KEY_1,"終了"); Display.setCurrent(w_panel); } class mypanel extends Panel implements SoftKeyListener { private Label w_label; private Button w_button; public mypanel(){ w_button = new Button("ボタン"); add(w_button); w_label = new Label("ラベル"); add(w_label); setSoftKeyListener(this); } public void softKeyReleased(int param){ if(param == this.SOFT_KEY_1){ terminate(); } } public void softKeyPressed(int param){ } } }

  • Java
  • 回答数2
  • ありがとう数1

質問者が選んだベストアンサー

  • ベストアンサー
  • koki_m
  • ベストアンサー率83% (45/54)
回答No.2

こんばんは。 1つのクラスで実現するまで単純化した例です。 - - - - - - - - - - import com.nttdocomo.ui.*; public class Test02 extends IApplication implements ComponentListener { private Panel p1, p2; private Label lbl1, lbl2; private Button btn; public void start() { // 最初の画面作成 p1 = new Panel(); lbl1 = new Label("最初の画面"); btn = new Button("次画面へ"); p1.add(lbl1); p1.add(btn); p1.setComponentListener(this); // 次の画面作成 p2 = new Panel(); lbl2 = new Label("次の画面"); p2.add(lbl2); // 最初の画面をセット Display.setCurrent(p1); } public void componentAction(Component source, int type, int param) { if(source == btn && type == ComponentListener.BUTTON_PRESSED) { Display.setCurrent(p2); // 次の画面をセット } } }

vega0707
質問者

お礼

早速の回答ありがとうございます!! おかげさまで謎がとけました。

その他の回答 (1)

  • koki_m
  • ベストアンサー率83% (45/54)
回答No.1

こんばんは。 2つ目の画面を用意することと、コンポーネントリスナーを実装するだけで出来ると思います。 ↓元ソースよりシンプルにしてあります。 - - - - - - - - - - import com.nttdocomo.ui.*; public class test01 extends IApplication { private mypanel p1; private mypanel p2; public void start() { p1 = new mypanel("PANEL 1"); p2 = new mypanel("PANEL 2"); Display.setCurrent(p1); } class mypanel extends Panel implements ComponentListener { // * private Label lbl; private Button btn; public mypanel(String lblt){ btn = new Button("NEXT"); add(btn); lbl = new Label(lblt); add(lbl); setComponentListener(this); // * } public void componentAction(Component source, int type, int param) { // * if(type == ComponentListener.BUTTON_PRESSED) { // ボタンのイベントか判定 if(source == p1.btn) { // イベント発信元がパネル1のボタンの場合 Display.setCurrent(p2); // パネル2を表示 } else if(source == p2.btn) { // イベント発信元がパネル2の場合 Display.setCurrent(p1); // パネル1を表示 } } } } }

vega0707
質問者

補足

回答ありがとうございます。 素人なのでなかなか理解できなくて恐縮ですが、2つ目の画面でボタンを表示せず、ラベルだけ表示することはできるのでしょうか?

関連するQ&A

  • iアプリでの関数の計算について

    関数(トランク関数とアークタンジェント)を計算したいのですが、以下のコードだと「シンボルを解釈処理できません」とメッセージが出てコンパイルできません。何か間違っているのでしょうか。 import com.nttdocomo.ui.*; public class test01 extends IApplication { private Panel p1; private Label label1,label2; public void start() { p1 = new Panel(); double num1,num2; num1 = Math.atan2(1.0,1.0); num2 = Math.trunc(1.0); label1=new Label("計算結果A "+ num1); label2=new Label("計算結果B "+ num2); p1.add(label1); p1.add(label2); Display.setCurrent(p1); } }

    • ベストアンサー
    • Java
  • iアプリ 複数パネル切替時の変数受渡(再度)

    すみませんが、再度質問です。 パネルを切替えると、変数が書換えられてません。 import com.nttdocomo.ui.*; public class TEST01 extends IApplication { MainCanvas mc; MainPanel mp; public void start() { mc = new MainCanvas(); mp = new MainPanel(); Display.setCurrent(mc); } } class MainCanvas extends Canvas{ TEST01 app; int data; public MainCanvas() { data=1; //----- data=1 Graphics g=getGraphics(); app = (TEST01)IApplication.getCurrentApp(); } public void paint(Graphics g) { data=2; // ここでdataを書換えてるのに・・・・ g.drawString("data="+data,100,100); g.drawString("セレクトキーで画面切替",50,200); } public void processEvent(int type, int param) { if (type == Display.KEY_PRESSED_EVENT) { if (param == Display.KEY_SOFT2) { IApplication.getCurrentApp().terminate(); } if (param == Display.KEY_SELECT) { Display.setCurrent(app.mp); } } } } class MainPanel extends Panel implements ComponentListener{ TEST01 app; Button b1; Label l1; public MainPanel() { app=(TEST01)IApplication.getCurrentApp(); l1=new Label("data="+app.mc.data); //書換えたdata=2にならないのはなぜ? b1=new Button("Push me"); add(l1); add(b1); setComponentListener(this); } public void componentAction(Component c,int type, int param){ if (c == b1 && type == BUTTON_PRESSED) Display.setCurrent(((TEST01)IApplication.getCurrentApp()).mc); } } data=1だったのをdata=2にしたのに、1のままです。

    • ベストアンサー
    • Java
  • p.add(myPanel());にエラーが出ます

    試行錯誤の末、テキストボックスの下にタイマーを表示したいので、下のソースのように両方Panelで実装したのですが、p.add(myPanel());にエラーがでます。どう変更すればよいでしょうか? import com.nttdocomo.ui.*; import com.nttdocomo.util.*; public class textbox extends IApplication implements ComponentListener { TextBox textbox1; TextBox textbox2; public void start(){ Panel p = new Panel(); textbox1 = new TextBox("",16,2,TextBox.DISPLAY_ANY); p.add(textbox1); textbox2 = new TextBox("(未入力)",16,2, TextBox.DISPLAY_ANY); textbox2.setEditable(false); p.add(textbox2); p.setComponentListener(this); p.add(myPanel()); Display.setCurrent(p); } class myPanel extends Panel implements TimerListener{ public Timer myTimer; public int cnt = 0; public Label lb; public myPanel(){ //新しいラベルをつくり表示する lb = new Label("カウント開始"); add(lb); //タイマーを作成 myTimer = new Timer(); //タイマーイベントの時間間隔を設定 myTimer.setTime(1000); //タイマーイベントを繰り返し発生させるかどうか設定 myTimer.setRepeat(true); //タイマーリスナーを登録 myTimer.setListener(this); //タイマーの開始 myTimer.start(); } //タイマーイベントの処理 public void timerExpired(Timer iTimer) { cnt++; lb.setText("カウント["+cnt+"]"); } } public void componentAction(Component source , int type, int param) { if(type == ComponentListener.TEXT_CHANGED && source == textbox1) { textbox2.setText(textbox1.getText()); } } }

  • Java 他クラスの呼び出しが上手くいきません。

    Java初心者です。 過去Q&Aとして掲載されていた「iアプリでのクラスファイルが複数ある時の画面遷移」と同趣旨のことがしたく、記事を参考にプログラムを組んでみました。 しかし、コンパイルの際、画面遷移のための別クラスファイルを呼び出しインスタンス化するところで生じる「シンボルを見つけられません。」というエラーの原因がつかめず困っています。 作成したプログラムのどこがいけないのか、ご教示いただければ幸いです。 作成したプログラム ========= Test01.java ========= import com.nttdocomo.ui.*; public class Test01 extends IApplication implements ComponentListener{ Button btn1; Label lbl1; public void start() { Panel p1 = new Panel(); p1.setTitle("p1"); Label lbl1=new Label("p1に居ます。"); p1.add(lbl1); Button btn1=new Button("next"); p1.add(btn1); p1.setComponentListener(this); Display.setCurrent(p1); } public void componentAction(Component source, int type, int param) { if(type == ComponentListener.BUTTON_PRESSED) { if(source==btn1){ Test02 test02 =new Test02(); //ここでエラーが発生します。 Display.setCurrent(test02); lbl1.setText("p2に遷移"); } } } } ============ Test02.java ============ import com.nttdocomo.ui.*; public class Test02 extends IApplication implements ComponentListener{ Button btn1; Label lbl1; public void start() { Panel p2 = new Panel(); p2.setTitle("p2"); Label lbl1=new Label("p2に居ます。"); p2.add(lbl1); Button btn1=new Button("next"); p2.add(btn1); p2.setComponentListener(this); Display.setCurrent(p2); } public void componentAction(Component source, int type, int param) { if(type == ComponentListener.BUTTON_PRESSED) { if(source==btn1){ lbl1.setText("p1に遷移"); //コンパイルエラーは生じませんが、ラベル表示の切り替えができません。 } } } }

    • ベストアンサー
    • Java
  • private voidなメソッドはstatic コンテキストですか

    コンパイルエラーで、 「static コンテキストから参照することはできません」 と表示されます。 ソースにstaticという文字は有りません。 private voidなメソッドはstatic コンテキストですか? [Foo.java] import com.nttdocomo.ui.*; public class Foo extends IApplication {  public void start() {   Display.setCurrent(new MyPanel());  } } class MyPanel extends Panel {  private void e() {   Bar.DShow(Dialog.DIALOG_ERROR, "", "");  } } [Bar.java] import com.nttdocomo.ui.*; public class Bar {  public void DShow(int type, String title, String body) {   Dialog d = new Dialog(type, title);   d.setText(body);   d.show();  } }

    • ベストアンサー
    • Java
  • ActionListener について

    JFrame の上に JPanel があり、JPanel の上に JButton があるとします。 JButton が押されたことを JFrame に知らせるにはどうしたらいいでしょうか。 わかるところまで書いてみました。 class MyFrame extends JFrame implements ActionListener { MyPanel myPanel = new MyPanel(); MyFrame() { myPanel.addActionListener(this); add(myPanel); } public void actionPerformed(ActionEvent e) { System.out.println("The button on the MyPanel was pressed!"); } public static void main(String[] args) { new MyFrame(); } } class MyPanel extends JPanel implements ActionListener { JButton button1 = new JButton(); MyPanel() { button1.addActionListener(this); add(button1); } public void actionPerformed(ActionEvent e) { } }

    • ベストアンサー
    • Java
  • JButtonの配置

    FlowLayoutによって、JFrameにボタン6個を2×3に配列するプログラムを考えています。ボタンを配置することはできたのですが、ウインドウの大きさを変えるとボタンが3×2になったりと変更してしまいます。 ウインドウの大きさを変えても、ボタンの大きさは変えず、ボタン6個が2×3配列になるプログラムはどうしたらよいのでしょうか?以下は、途中のプログラムです。 import java.awt.*; import javax.swing.*; public class sample { public static void main(String[] args){ MyFrame frame = new MyFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } public class MyFrame extends JFrame { public static final int WIDTH = 200; public static final int HEIGHT = 300; public MyFrame(){ setSize(WIDTH,HEIGHT); Container contentPane = getContentPane(); MyPanel panel = new MyPanel(); contentPane.add(panel); } } public class MyPanel extends JPanel{ public static final int MESSAGE_X = 200; public static final int MESSAGE_Y = 200; setLayout(new FlowLayout()); JButton button1 = new JButton("Button1"); contentPane.add(button1); JButton button2 = new JButton("Button2"); contentPane.add(button2); JButton button3 = new JButton("Button3"); contentPane.add(button3); JButton button4 = new JButton("Button4"); contentPane.add(button4); JButton button5 = new JButton("Button5"); contentPane.add(button5); JButton button6 = new JButton("Button6"); contentPane.add(button6); }

    • ベストアンサー
    • Java
  • iアプリ Tomcatと連携

    前回の質問は、分かりづらいようなので新たに質問させていただきます。 現在、TomcatとiアプリDoja_Ver5.1、MySQLを使いDojaの方からTomcatへアクセスしTomcatからMySQLのデータを取りDojaの方へ表示させたいのですが、下記の文で何度やっても「java.lang.SecurityException: Illegal host」と出てしまいできません。 ADFファイルである、Jamファイルは、Jarファイルと同じ場所にあるので、PackageURLには「dosei_jisaku.jar」としか記述していません。 何故か、java.lang.SecurityException: Illegal hostが出て解決策も見出せずでいます;; public class dosei_jisaku extends IApplication { public void start() { mypanel wp_mypanel = new mypanel(); Display.setCurrent(wp_mypanel); } class mypanel extends Panel{ public mypanel() { add(new Label("項目名:")); TextBox w_name = new TextBox("",8,1,TextBox.DISPLAY_ANY); add(w_name); try { HttpConnection c = (HttpConnection)Connector.open("http://localhost:8080/mfs/mfs/test/dosei_jisakuSV"); ←同PCにあるTomcatのServlet(classファイル)へアクセスしています。 c.setRequestMethod(HttpConnection.GET); c.connect(); if(c.getResponseCode() != HttpConnection.HTTP_OK){ throw new IOException(); } InputStream is = c.openInputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream(); for(;;){ int n = is.read(); if (n == -1) { break; } os.write(n); } String str = new String(); str = os.toString(); w_name.setText(str); os.close(); is.close(); c.close(); } catch(Throwable e) { w_name.setText("読込エラー" + e); } } } } どなたかアドバイスを頂けるようお願い致します…。

  • javaでのiアプリの画面表示について

    javaでiアプリを作成したいのですが、行きずまっています。 テキストボックスとストップウォッチを画面上に両方表示させたいのですが、下のDisplay.setCurrent(p);とDisplay.setCurrent(mainWin);のどちらかしか表示されません。 ストップウォッチは、MainWin.javaが他にありますのでしっかり起動します。 どうすれば良いでしょうか? package stopwatch; import com.nttdocomo.ui.*; public class StopWatch extends IApplication implements ComponentListener { MainWindow mainWin; TextBox textbox1; TextBox textbox2; public void start() { Panel p = new Panel(); textbox1 = new TextBox("",16,2,TextBox.DISPLAY_ANY); p.add(textbox1); textbox2 = new TextBox("(未入力)",16,2, TextBox.DISPLAY_ANY); textbox2.setEditable(false); p.add(textbox2); p.setComponentListener(this); Display.setCurrent(p); mainWin = new MainWindow(); Display.setCurrent(mainWin); } public void componentAction(Component source , int type, int param) { if(type == ComponentListener.TEXT_CHANGED && source == textbox1) { textbox2.setText(textbox1.getText()); } } }

  • javaでのiアプリの画面表示について

    javaでiアプリを作成したいのですが、行きずまっています。 テキストボックスとストップウォッチを画面上に両方表示させたいのですが、下のDisplay.setCurrent(p);とDisplay.setCurrent(mainWin);のどちらかしか表示されません。 ストップウォッチは、MainWin.javaが他にありますのでしっかり起動します。 どうすれば良いでしょうか? package stopwatch; import com.nttdocomo.ui.*; public class StopWatch extends IApplication implements ComponentListener { MainWindow mainWin; TextBox textbox1; TextBox textbox2; public void start() { Panel p = new Panel(); textbox1 = new TextBox("",16,2,TextBox.DISPLAY_ANY); p.add(textbox1); textbox2 = new TextBox("(未入力)",16,2, TextBox.DISPLAY_ANY); textbox2.setEditable(false); p.add(textbox2); p.setComponentListener(this); Display.setCurrent(p); mainWin = new MainWindow(); Display.setCurrent(mainWin); } public void componentAction(Component source , int type, int param) { if(type == ComponentListener.TEXT_CHANGED && source == textbox1) { textbox2.setText(textbox1.getText()); } } }

専門家に質問してみよう