• 締切済み

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
  • 回答数2
  • ありがとう数0

みんなの回答

回答No.2

Panelに別のPanelはadd出来ません。 別の質問でも書きましたが、テキストボックスとストップウォッチを1画面に表示したければ 1つのPanelあるいは1つのCanvas内にテキストボックスとストップウォッチの両方を実装する必要があります。 これも別の質問でも書きましたが、一度「iアプリコンテンツ開発ガイド」をよく読むことをお勧めします。 少なくともPanelに別のPanelをadd出来ないことはガイドを読めば分かりますし APIリファレンスのPanel.add()を見ればPanelをadd出来ないことは分かるかと思います。

  • Tacosan
  • ベストアンサー率23% (3656/15482)
回答No.1

せめて new してあげよう.

関連するQ&A

  • 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()); } } }

  • 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
  • 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
  • iアプリ パネル遷移の際の変数の受け渡し方について

    iアプリで、パネル遷移の際に遷移元の変数を遷移先に引き渡したいのですが、その方法がわかりません。 テスト用のプログラムを作成しましたので、どなたかご教示いただければ幸いです。 ソフトキー1:終了 ソフトキー2:TextBoxのリセット ボタン:パネル遷移とTextBoxの入力文字の遷移先への引き渡し TextBox:受納したテキストの表示と送付用のテキストの入力 以上の機能をパネル'p1'と'p2'とに備え、二つのパネル間での遷移とテキストの受け渡しをしたいと思います。 ========== Test.java ========== public class testx extends IApplication { public void start() { P1 p1 = new P1(); P2 p2 = new P2(); Display.setCurrent(p1); } } ======== P1.java ======== public class P1 extends Panel implements ComponentListener, SoftKeyListener{ IApplication iapp; String str =""; Button btn1 = new Button("送る"); TextBox tb1=new TextBox("",33,1,TextBox.DISPLAY_ANY); P1(){ setTitle("P1"); HTMLLayout lm=new HTMLLayout(); setLayoutManager(lm); lm.begin(HTMLLayout.LEFT); add(tb1); add(btn1); lm.end(); setSoftKeyListener(this); setSoftLabel(SOFT_KEY_1,"終了"); setSoftLabel(SOFT_KEY_2,"リセット"); setComponentListener(this); Display.setCurrent(this); } public void componentAction(Component source,int type,int param){ if(type==BUTTON_PRESSED){ /* p2 からのテキストをstrと定義しTextBoxに表示したい。 その定義の仕方がわからない。 str = ; */ tb1.setText(str); /* TextBoxに新たに入力した文字列をstrと再定義。 変数strをp2に渡したい。 */ str = tb1.getText(); P2 p2 = new P2(); Display.setCurrent(p2); } } public void softKeyPressed(int softKey){ if(softKey==SOFT_KEY_1){ iapp.terminate(); } if(softKey==SOFT_KEY_2){ tb1.setText(""); } } public void softKeyReleased(int softKey){ } } ======== P2.java ======== public class P2 extends Panel implements ComponentListener, SoftKeyListener{ IApplication iapp; String str =""; Button btn1 = new Button("送る"); TextBox tb1=new TextBox("",33,1,TextBox.DISPLAY_ANY); P2(){ setTitle("P2"); HTMLLayout lm=new HTMLLayout(); setLayoutManager(lm); lm.begin(HTMLLayout.LEFT); add(tb1); add(btn1); lm.end(); setSoftKeyListener(this); setSoftLabel(SOFT_KEY_1,"終了"); setSoftLabel(SOFT_KEY_2,"リセット"); setComponentListener(this); Display.setCurrent(this); } public void componentAction(Component source,int type,int param){ if(type==BUTTON_PRESSED){ /* p1 からのテキストをstrと定義しTextBoxに表示したい。 その定義の仕方がわからない。 str = ; */ tb1.setText(str); /* TextBoxに新たに入力した文字列をstrと再定義。 変数strをp1に渡したい。 */ str = tb1.getText(); P1 p1 = new P1(); Display.setCurrent(p1); } } public void softKeyPressed(int softKey){ if(softKey==SOFT_KEY_1){ iapp.terminate(); } if(softKey==SOFT_KEY_2){ tb1.setText(""); } } public void softKeyReleased(int softKey){ } }

  • javaの初心者です。

    コンピュータとじゃんけんをするプログラムをつくりたいのですが コンパイルしたところ、次のようなエラーが出てきました。 java:8: メソッドの宣言が不正です。戻り値の型が必要です。 すいませんが、どこをどう直したらいいのかわかりません。 よろしければお教え願います。 以下がプログラムです。 import java.awt.event.*; import java.awt.*; import javax.swing.*; import java.applet.AudioClip; class Zyanken extends YJFrame implements ActionListener{ JButton bt1,bt2,bt3; JLabel lb1,lb2,lb3; YJFrame(String s, int w, int h,int b){ super(s,w,h,b); String[] ttl={"グー","チョキ","パー","Exit"}; String[] tips={"グーです","チョキです","パーです","終了します"}; int i; setUp(); Icon[] ic=new Icon[4]; for(i=0;i<4;i++) ic[i]=new ImageIcon("icon"+i+".gif"); btn=new JButton[4]; for(i=0;i<4;i++){ btn[i]=new JButton(ttl[i],ic[i]); btn[i].setToolTipText(tips[i]); btn[i].addActionListener(this); } JPanel p=new JPanel(); for(i=0;i<4;i++) p.add(btn[i]); p.setBackground(Color.pink); pane.add(p,BorderLayout.SOUTH); panel=new JPanel(); pane.add(panel,BorderLayout.CENTER); } public void actionPerformed(ActionEvent e){ int com=(int)(Math.random()*4); if(com==1){ lb2.setText("COMはグーです。"); } else if(com==2){ lb2.setText("COMはチョキです。"); } else{ lb2.setText("COMはパーです。"); } if(e.getSource()==bt1){ if(com==1){ lb3.setText("あいこです。"); } else if(com==2){ lb3.setText("あなたの勝ちです。"); } else{ lb3.setText("あなたの負けです。"); } } else if(e.getSource()==bt2){ if(com==1){ lb3.setText("あなたの負けです。"); } else if(com==2){ lb3.setText("あいこです。"); } else{ lb3.setText("あなたの勝ちです。"); } } else{ if(com==1){ lb3.setText("あなたの勝ちです。"); } else if(com==2){ lb3.setText("あなたの負けです。"); } else{ lb3.setText("あいこです。"); } } } }

  • 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
  • 上手くいきません。

    なぜ上手くいきませんか??? http://image32.bannch.com/bbs/43055/img/0246328923.swf ↑のページを見てください。 var cnt:int=0; var myTimer:Timer=new Timer(2000,0); function moveMC(e:Event):void { if (mc.x<250) { if (mc.x<cnt*50) { mc.x+=10; } }else { mc.x=0; }} というコードで250まで行ったら0に戻すという処理をしていますが、0に戻ってからの動きにタイムイベントが反映されません。 どうしたらいいですか??

    • ベストアンサー
    • Flash
  • add( )メソッドについて

    import java.applet.Applet; import java.awt.*; /* <applet code="Sample" width=400 height=250> </applet> */ public class Sample extends Applet { public void init() { // ラベルを作成する。 Label lb = new Label("こんにちは。", Label.LEFT); add(lb); } } 上記のプログラムのadd(lb)について質問です。上記のadd()メソッドは、lbはLabelクラスなので、LabelクラスかComponentクラスのパラメーターを一つ持つvoid型のadd()メソッドとなると思うのですが、調べてもそのようなメソッドが存在しませんどうしてでしょうか?上述のことが間違っているのでしょうか?教えてください。

    • ベストアンサー
    • Java
  •  型Containerのメソッドadd(String ,Compone

     型Containerのメソッドadd(String ,Component)は引数(JPanel ,String)に適用できません。  今晩は、java初心者です、エラーの意味が理解出来ません、宜しくお願い致します。  《A》というプログラムと《B》というプログラムで、クラスを「class MyPanel extends JPanel」と「class JPanel」に変えただけで、「型Containerのメソッドadd(String ,Component)は引数(JPanel ,String)に適用できません。」というエラーがでます。  一体何が原因でしょうか。  JPanelを継承したクラス(MyPanel)を呼び出すのか、そのまま、JPanelクラスを呼び出すのかの違いだけで、内容的には何も変わるところがないはずだと思うのですが。  意味が理解出来ません。 ======================================================================== 《A》 public class Sample extends JFrame { Sample() { this.setSize(300,200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyPanel panel = new MyPanel(); this.add(panel , BorderLayout.CENTER); this.setVisible(true); } public static void main(String[] args) { new Sample(); } } class MyPanel extends JPanel { public void paintComponent(Graphics g) { g.setColor(Color.red); g.fillOval(50,50,100,100); } } ============================================================ 《B》 public class Sample extends JFrame { Sample() { this.setSize(300,200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); this.add(panel , BorderLayout.CENTER); this.setVisible(true); } public static void main(String[] args) { new Sample(); } } class JPanel { public void paintComponent(Graphics g) { g.setColor(Color.red); g.fillOval(50,50,100,100); } } =======================================================================

    • ベストアンサー
    • Java