gpt4 book ai didi

java - 在 Java 中找不到符号。 [编译错误]

转载 作者:行者123 更新时间:2023-12-01 13:48:10 26 4
gpt4 key购买 nike

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 。我尝试阅读旧帖子,但找不到任何与我的问题非常相似的内容。

最佳答案

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

关于java - 在 Java 中找不到符号。 [编译错误],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20183237/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com