• ベストアンサー

シンボルが見つかりませんというエラーが理解できません。

以下のようなじゃんけんゲームのプログラムを書いたのですが、「シンボルが見つかりません。」というエラーが表示されるのですが、エラーの意味が理解できず、解決できません。どこが間違っているのか教えていただけませんか。 import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.io.File; public class janken extends Applet implements Runnable, ActionListener { private static final int EXTERNAL_BUFFER_SIZE = 128000; Image image[] = new Image[3]; Thread t; int index1 = 0; int index2 = 0; String msg = ""; String msg1 = ""; boolean state = false; Button b1 = new Button("ぐー"); Button b2 = new Button("ちょき"); Button b3 = new Button("ぱー"); public void init(){ for(int i = 0; i<=2; i++){ img[i] = getImage(getDocumentBase(),"hanabi" + (i+1) + ".JPG"); } add(b1); add(b2); add(b3); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); msg1 = "結果は・・"; } public void paint(Graphics g){ g.drawImage(img[index1],350,30,this); g.drawImage(img[index2],695,30,this); g.drawString("コンピューター",420,300); g.drawString("あなた",800,300); g.drawString(msg,630,320); g.drawString(msg1,550,320); } public void start(){ state = true; t = new Thread(this); t.start(); } public void run(){ while(state){ index1++; if(index1 == 3){ index1 = 0; } index2++; if(index2 == 3){ index2 = 0; } repaint(); try { Thread.sleep(60); }catch(InterruptedException e) { } } } public void actionPerformed(ActionEvent e){ if(state == false) { start(); return; } state = false; if(e.getSource() == b1) { msg = "ぐー"; index2 = 0; } else if(e.getSource() == b2){ msg = "ちょき"; index2 = 1; } else if(e.getSource() == b3){ msg = "ぱー"; index2 = 2; } check(); repaint(); } public void check() { if(index1 == index2) msg ="あいこ"; else if (index1 == 0) { if(index2 == 2) msg="あなたの勝ち"; else msg ="あなたの負け"; } else if(index1 == 1) { if(index2 == 0) msg="あなたの勝ち"; else msg="あなたの負け"; } else if(index1 == 2) { if(index2 == 1) msg="あなたの勝ち"; else msg="あなたの負け"; } } }

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

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

  • ベストアンサー
  • grinmame
  • ベストアンサー率100% (1/1)
回答No.2

「シンボルを見つけられません。」というエラーの下に何か表示がありませんでしたか?そこにヒントがあると考えられます。 シンボルを見つけられませんといエラーが表示される主な理由は4つあります。 (1)クラス、メソッド、変数などの綴りミスや定義していない変数を使用している可能性がある。 (2)コンストラクタを呼び出すときに、newを忘れている可能性がある。(3)公開されていないメンバーを呼び出している可能性がある。 (4)必要なimport文を記述し忘れている可能性がある。 ここでのあなたのエラーは(1)番ではないでしょうか?上記ではimageとなっている変数がimgになっていますね。 これはエラー表示をよく見ることで意外と簡単に解決できるのです。 ゆっくり丁寧にエラー表示を見るように心がけることが大事ですよ。

atomseijin
質問者

お礼

ありがとうございます。 非常にわかりやすく、じゃんけんゲームがちゃんと動くようになりました。丁寧な回答ありがとうございます!

その他の回答 (1)

  • OKbokuzyo
  • ベストアンサー率43% (130/296)
回答No.1

「シンボルが見つかりません。」 これはjavacがコンパイル時にクラスやメソッドを見つけられなかった場合に表示されます。 エラーとともに見つけられなかったシンボルの行番号とシンボル名が表示されているはずですので それを見ればどのシンボルが解決できないのかわかるはずです。 ちなみにシンボルを解決できない理由は主に下記のような原因が考えられます。 ・必要なクラスがインポートされていない。 ・クラスパスが通っていない。 ・クラスやメソッド、変数の綴りが間違っている。 などです。 質問するとき、何かエラーが出ているならそれをすべて書き記すのが最良です。 あなたには分からなくても、分かる人には一番良く分かる情報ですので。

関連するQ&A

  • 音声ファイルの入れ方

    下記ようなじゃんけんゲームでボタンを押した際にwav拡張子の音声ファイルを再生したいのですがどのようにすればよいでしょうか import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.io.File; import javax.sound.sampled.*; public class janken extends Applet implements Runnable, ActionListener { private static final int EXTERNAL_BUFFER_SIZE = 128000; Image image[] = new Image[3]; Thread t; int index1 = 0; int index2 = 0; String msg = ""; String msg1 = ""; boolean state = false; Button b1 = new Button("ぐー"); Button b2 = new Button("ちょき"); Button b3 = new Button("ぱー"); public void init(){ for(int i = 0; i<=2; i++){ image[i] = getImage(getDocumentBase(),"hanabi" + (i+1) + ".JPG"); } add(b1); add(b2); add(b3); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); msg1 = "結果は・・"; } public void paint(Graphics g){ g.drawImage(image[index1],350,30,this); g.drawImage(image[index2],745,30,this); g.drawString("わたし",420,300); g.drawString("あなた",820,300); g.drawString(msg,630,320); g.drawString(msg1,550,320); }  public void start(){  state = true;  t = new Thread(this); t.start(); } public void run(){ while(state){ index1++; if(index1 == 3){ index1 = 0; } index2++; if(index2 == 3){ index2 = 0; } repaint(); try { Thread.sleep(60); }catch(InterruptedException e) { } } } public void actionPerformed(ActionEvent e){ if(state == false) { start(); return; } state = false; if(e.getSource() == b1) { msg = "ぐー"; index2 = 0; } else if(e.getSource() == b2){ msg = "ちょき"; index2 = 1; } else if(e.getSource() == b3){ msg = "ぱー"; index2 = 2; } check(); repaint(); } public void check() { if(index1 == index2) msg ="あいこ"; else if (index1 == 0) { if(index2 == 2) msg="あなたの勝ち"; else msg ="あなたの負け"; } else if(index1 == 1) { if(index2 == 0) msg="あなたの勝ち"; else msg="あなたの負け"; } else if(index1 == 2) { if(index2 == 1) msg="あなたの勝ち"; else msg="あなたの負け"; } } }

  • JAVAのプロフラミングについて

    □、○、△のボタンがあり、それぞれのボタンについて 一回押すと、その押したボタンの図形を2個描き、 押すたびに永久的に増えていくというようなプログラム を作りたいのですが、ここからどのようにすればいいのか教えてください。 import java.awt.*; import java.applet.*; import java.awt.event.*; public class kadaiApplet232 extends Applet implements ActionListener { int square = 0; int circle = 0; int triangle = 0; Button squareButton, circleButton,triangleButton; public void init(){ squareButton = new Button("□"); squareButton.addActionListener(this); add(squareButton); circleButton = new Button("○"); circleButton.addActionListener(this); add(circleButton); triangleButton = new Button("△"); triangleButton.addActionListener(this); add(triangleButton); } public void paint(Graphics g){ if(){ g.drawRect(30, 30, square, square); } if(){ g.drawOval(60,30,circle,circle); } if(){ g.drawLine(90+(int)(triangle/2),30,90,30+triangle); g.drawLine(90,30+triangle,90+triangle,30+triangle); g.drawLine(90+triangle,30+triangle,90+(int)(triangle/2),30); } } public void actionPerformed(ActionEvent e){ if ( e.getSource() == squareButton){ square = 30; } else if ( e.getSource() == circleButton){ circle = 30; } else if (e.getSource() == triangleButton){ triangle = 30; } repaint(); } }

    • ベストアンサー
    • Java
  • jcpad

    『長方形をあらかじめ置き、、縮小、拡大のボタンを押すことで  縦横を1/1.05倍に縮小できるようにする』 という問題が教科書にあり、拡大、縮小ボタンを作るまではできたのですがそれ以降がうまく考えることができません。 //<applet code= "Counter3.class" width=300 height=150></applet> import java.awt.*; import java.applet.*; import java.awt.event.*; public class Counter3 extends Applet implements ActionListener { Font font; Button kakudaiButton, syukushoButton; public void init() { font = new Font("Serif", Font.BOLD, 72); kakudaiButton = new Button("拡大"); kakudaiButton.addActionListener(this); add(kakudaiButton); syukushoButton = new Button("縮小"); syukushoButton.addActionListener(this); add(syukushoButton); } public void paint(Graphics g) { g.drawOval(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == kakudaiButton) { ; } else if (e.getSource() == syukushoButton) { ; } repaint(); } }

  • シンボルを解決できません

    サンプルプログラムをjavacでコンパイルすると 以下のようなエラーが出ます。 ----------------------------- C:\MyJava>javac menudemo.java menudemo.java:57: シンボルを解決できません。 シンボル: コンストラクタ Menu (java.lang.String) 場所 : Menu の クラス menu1 = new Menu("ファイル"); ^ menudemo.java:70: シンボルを解決できません。 シンボル: メソッド add (java.awt.MenuItem) 場所 : Menu の クラス menu1.add(menuitem1); ^ menudemo.java:71: シンボルを解決できません。 シンボル: メソッド add (java.awt.MenuItem) 場所 : Menu の クラス menu1.add(menuitem2); ^ menudemo.java:72: シンボルを解決できません。 シンボル: メソッド add (java.awt.MenuItem) 場所 : Menu の クラス menu1.add(menuitem3); ^ menudemo.java:75: java.awt.MenuBar の add(java.awt.Menu) は (Menu) に適用できません。 menubar1.add(menu1); ^ エラー 5 個 --------------------------------------------- サンプルプログラムは以下の通りです。 --------------------------------------------- import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class menudemo extends Applet implements ActionListener { Button button1, button2; MenuFrame menuWindow; public void init() { button1 = new Button("ウィンドウの表示"); add(button1); button1.addActionListener(this); button2 = new Button("ウィンドウの非表示"); add(button2); button2.addActionListener(this); menuWindow = new MenuFrame("デモメニュー"); menuWindow.setSize(100, 100); } public void actionPerformed(ActionEvent event) { if (event.getSource() == button1) { menuWindow.setVisible(true); } if (event.getSource() == button2) { menuWindow.setVisible(false); } } } class MenuFrame extends Frame implements ActionListener { MenuBar menubar1; Menu menu1; MenuItem menuitem1, menuitem2, menuitem3; TextField text1; MenuFrame(String title) { super(title); setLayout(new GridLayout(1, 1)); text1 = new TextField(""); add(text1); //メニューバーの生成 menubar1 = new MenuBar(); //メニューの生成 menu1 = new Menu("ファイル"); //メニュー項目の生成 menuitem1 = new MenuItem("Javaの"); menuitem2 = new MenuItem("世界へ"); menuitem3 = new MenuItem("ようこそ"); //イベントリスナーの登録 menuitem1.addActionListener(this); menuitem2.addActionListener(this); menuitem3.addActionListener(this); //メニューへの追加 menu1.add(menuitem1); menu1.add(menuitem2); menu1.add(menuitem3); //メニューバーへの追加 menubar1.add(menu1); //メニューバーを追加 setMenuBar(menubar1); } public void actionPerformed(ActionEvent event) { if (event.getSource() == menuitem1) { text1.setText("Javaの"); } if (event.getSource() == menuitem2) { text1.setText("世界へ"); } if (event.getSource() == menuitem3) { text1.setText("ようこそ"); } } } --------------------------------------------- テキストどおりに入力しているのですが、 いろいろ調べてみても どこがおかしいか自分では分かりませんでした。 どのようにすれば解決するかお教えください。 よろしくお願いいたします。

    • ベストアンサー
    • Java
  • return;

    retrun;を取ると動作がおかしくなるんですが return;にはどういう働きがあるんでしょうか? お願いします。 import java.applet.Applet; import java.awt.*; import java.awt.event.*; //<applet code = "a.class" width = "300" height = "300"></applet> public class a extends Applet implements ActionListener{ Dialog d; Frame f; public void init() { setLayout( new GridLayout( 1, 1 ) ); Button b = ( Button )add( new Button( "frame" ) ); b.addActionListener( this ); } public void actionPerformed( ActionEvent e ){ if( e.getActionCommand() == "frame" ){ if ( f == null ){ f = new Frame( "Kitty on your lap" ); Button fb = (Button)f.add( new Button( "Kitty " ) ); fb.addActionListener( this ); f.setSize( 200 , 200 ); f.setVisible( true ); }else if( d == null ){ f.dispose(); f = null; } return; } if( e.getActionCommand() == "OK" ) { d.dispose(); d = null; return; } d = new Dialog( f, "Kitty", true ); d.setLayout( new GridLayout( 2, 1 ) ); d.setResizable( false ); d.add( new Label( "Kitty on your lap" ) ); Button b = (Button)d.add( new Button( "OK" ) ); b.addActionListener( this ); d.setSize( 400 , 200 ); d.setVisible( true ); } }

    • ベストアンサー
    • Java
  • HTMLファイルを開きたい

    こんにちは、いつもお世話になっています。 質問があります。 アプレットでボタンを押すと、 HTMLファイル「c:/test_folder/test.html」 を開きたいのですが、 下記コードの(☆) のところに何を書いていいかわかりません。 どなたかご存知でしたら教えていただけないでしょうか? よろしくお願いします。 ============================================================= import javax.swing.*; import java.awt.*; import java.applet.Applet; import java.awt.event.*; import java.net.*; public class situmon extends Applet implements ActionListener { JPanel panel; JButton button; public void init() { panel=new JPanel(); button=new JButton("次へ"); button.addActionListener(this); panel.add(button); this.add(panel); } public void actionPerformed(ActionEvent e) { if(e.getSource()==button) { (☆); } } }

    • ベストアンサー
    • Java
  • 下のソースを参考書をそのまま書いたのですが

    下のソースを参考書をそのまま書いたのですが eclipse環境だと f.show(); のところが推薦されない使われ方 として実行できません 無理やり動作させたところ、エラーが返ってきました どうすれば、f.show(); が有効にできるでしょうか? import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class window { public static void main( String[] args ) { AppFrame f = new AppFrame(); f.setSize( 200, 200 ); f.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e) { System.exit(0); } } ); f.show(); } } class AppFrame extends Frame implements ActionListener { Button b1, b2; labelWindow window; AppFrame() { setLayout( new FlowLayout() ); b1 = new Button( "Display the window" ); add(b1); b1.addActionListener( this ); b2 = new Button( "Hide the window" ); add(b2); b2.addActionListener( this ); window = new labelWindow(this); window.setSize( 300,200 ); window.setLocation( 300, 300 ); } public void actionPerformed( ActionEvent event ) { if( event.getSource() == b1 ) { window.setVisible(true); } if( event.getSource() == b2 ) { window.setVisible(false); } } } class labelWindow extends Window { Label label; labelWindow( AppFrame af ) { super( af ); setLayout( new FlowLayout() ); label = new Label( "Hello from Java!" ); add( label ); } public void paint( Graphics g ) { int width = getSize().width; int heigth = getSize().heigth; g.drawRect( 0, 0, --width, --heigth ); } }

    • ベストアンサー
    • Java
  • 画像処理についておしえてください!

    ここに新たに表示した画像を消去するボタンを 追加したいので教えてください! import java.awt.*; import java.awt.event.*; import java.io.*; import java.applet.Applet; public class k202 extends Frame implements ActionListener { MyCanvas c1; Button b1; Button e1; Image img; Button btnClear; int flag; public k202() { super(); setTitle("Hello"); setSize(500,500); setLayout(null); c1 = new MyCanvas(); c1.setBounds(25,25,250,150); this.add(c1); b1 = new Button("Read"); b1.setBounds(25,200,100,25); b1.addActionListener(this); this.add(b1); e1 = new Button("Clear"); e1.setBounds(50,250,150,50); //ボタンのイベント処理メソッドを定義 e1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ flag = 0; repaint(); } }); } public static void main (String args []) { new k202().show(); } public void actionPerformed(ActionEvent ev) { if (ev.getSource() == b1) { this.readImageFromFile(); } } void readImageFromFile() { try { FileDialog fd = new FileDialog(this,"Select Image File.",FileDialog.LOAD); fd.setVisible(true); String fname = fd.getDirectory() + fd.getFile(); Toolkit tk = Toolkit.getDefaultToolkit(); img = tk.getImage(fname); c1.repaint(); } catch(Exception e) { System.out.println(e); } } class MyCanvas extends Canvas { public void paint(Graphics g) { if (img != null) { g.drawImage(img,0,0,this); } } } }

  • アクションリスナー(初心者です)

    JAVAでボタンが押されたらすでに描かれている線に さらに線が加わって描かれるというプログラムを作ろうとしているのですが、アクションリスナーを受け取ったメソッドの中身をどうしたらいいのか行き詰まりました・・・作りかけのプログラムを載せてみますので アドバイスいただければと思います よろしくお願いします import java.applet.Applet; import java.awt.Graphics.*; import java.awt.event.*; import java.awt.*; public class file928 extends Applet implements ActionListener { Button botan; public void init() { botan=new Button("選択"); add(botan); botan.addActionListener(this); } public void paint(Graphics g) { g.drawString("L.A.Airport",100,300);           ~中略~ g.drawLine(405,420,655,240); } public void actionPerformed(ActionEvent x) { ーここをどうしたらいいかわかりません・・・ー } }

    • ベストアンサー
    • Java
  • JAVAのプログラミングについて

    色変更というボタンを押すたびに、正方形の中の色が緑から青、青から緑というように繰り返し色が変わるプログラムを作りたいのですが、途中までは、完成できたのですが、ここからどうすればいいかわかりません。教えてください。 import java.awt.*; import java.applet.*; import java.awt.event.*; public class kadai3Applet extends Applet implements ActionListener { int i = 1; Button collor; public void init(){ collor = new Button("色変更"); collor.addActionListener(this); add(collor); } public void paint(Graphics g){ g.setColor(Color.green); g.fillRect(50,50,50,50); } public void actionPerformed(ActionEvent e){ if(e.getSource() == collor){ } repaint(); } }

専門家に質問してみよう