【问题标题】:Cannot find symbol in Java. [Compiler error]在 Java 中找不到符号。 [编译器错误]
【发布时间】:2013-11-25 00:58:55
【问题描述】:
SlotMachine.java:76: cannot find symbol
symbol  : variable slot
location: class MyFrame.pullHandler

代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

class SlotMachine
{
    public static void main(String [] args)
    { 
       MyFrame f = new MyFrame();
    }
}

class MyFrame extends JFrame
{
    JTextField r1 = new JTextField("---",10);
    JTextField r2 = new JTextField("---",10);
    JTextField r3 = new JTextField("---",10);



    JButton pull = new JButton("Pull");
    JLabel result = new JLabel("Not Played Yet");

public MyFrame()
{   

  JTextField [] slot = new JTextField[3];
  slot[0] = new JTextField("---",10);
  slot[1] = new JTextField("---",10);
  slot[2] = new JTextField("---",10);

    JPanel panel = new JPanel();
    setVisible(true);
    setSize(400, 400);  //replace with pack();?
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Slot Machine - By: ");

    add(panel);
    panel.add(slot[0]);
    panel.add(slot[1]);
    panel.add(slot[2]);
    panel.add(pull);
    panel.add(result);

    pull.addActionListener(new pullHandler());

}

class pullHandler implements ActionListener
{
    public void actionPerformed(ActionEvent pull)
    {
        int ban = 0;
        int cher = 0; 
        int mel = 0;
        int plays = 0;

        for(int count=0; count< 3; count++) //repeats three times, giving three random values
        {
            Random rand = new Random(); 
            int numRoll = rand.nextInt(3);  //0,1,2 values



        if  (numRoll==0)
            {
                //Bannana
                slot[0].setBackground(Color.yellow);    //I want to replace the 1 with the counts, so if it is the second loop, it would set it for the second box.
                ban++;
                /*slot[count]*/r1.setText("Banana");        
            }

            if (numRoll==1)
            {   
                //cherry
                r2.setBackground(Color.red);
                cher++;
                r2.setText("Cherry");
            }

            if (numRoll==2)
            {
                //Melon
                r3.setBackground(Color.green);
                mel++;
                r3.setText("Melon");  
            }
        }

        plays++;
        result.setText("Played " + plays);  //why don't I keep getting new values when I click "Pull"?

    }
}



}

我正在尝试在我的 pullhandler 类中使用 slot[] 数组而不是 r1/r2/r3。 我尝试阅读旧帖子,但找不到任何与我的问题非常相似的内容。

【问题讨论】:

  • 继续阅读变量范围。
  • 变量slotMyFrame 构造函数的本地变量,pullHandler 不知道。

标签: java symbols


【解决方案1】:

在构造函数作用域之外,变量slot没有任何意义。如果您希望在其他方法中访问它,请将slot 移动到字段级别,并使用JTextField 变量。

【讨论】:

  • 问题是每当我将它移到构造函数之外时,我都会得到这个:SlotMachine.java:63: illegal start of type slot[2] = new JTextField("---",10); ^ SlotMachine.java:63: &lt;identifier&gt; expected slot[2] = new JTextField("---",10); ^ SlotMachine.java:63: ';' expected slot[2] = new JTextField("---",10);
  • 为什么不能将JTextField [] slot = new JTextField[3];移出构造函数?无论如何,这就是你可以移动的全部。
  • 啊,我想那是我的问题!我不应该移动 "slot[0] = new JTextfield("---",10); 部分!谢谢 Makoto!
【解决方案2】:

JTextField [] slot = new JTextField[3]; 应该在 MyFrame() 构造函数之外创建

要使其对pullHandler 可用,您还应该尝试将slot 设为static

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 2019-05-31
    • 2013-12-06
    • 2013-10-04
    • 2012-08-27
    相关资源
    最近更新 更多