0
SlotMachine.java:76: cannot find symbol symbol : variable slot location: class MyFrame.pullHandler 

Code

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"? } } } 

I'm trying to use the slot[] array instead of r1/r2/r3 in my pullhandler class. I tried reading through old posts, but could not find anything that was quite like my problem.

2
  • 2
    Go read up on variable scope. Commented Nov 25, 2013 at 0:59
  • 2
    The variable slot is local to the MyFrame constructor, and isn't known to pullHandler. Commented Nov 25, 2013 at 1:01

2 Answers 2

1

Outside of the scope of the constructor, the variable slot has no meaning. If you want it to be accessed in other methods, move slot to the field level, with the JTextField variables.

Sign up to request clarification or add additional context in comments.

3 Comments

the problem Is that whenever I move it outside the constructor I get this: SlotMachine.java:63: illegal start of type slot[2] = new JTextField("---",10); ^ SlotMachine.java:63: <identifier> expected slot[2] = new JTextField("---",10); ^ SlotMachine.java:63: ';' expected slot[2] = new JTextField("---",10);
Why can't you move JTextField [] slot = new JTextField[3]; out of the constructor? That's all you can move, anyway.
Ahhh, I think that was my problem! I shouldn't have been moving the "slot[0] = new JTextfield("---",10); part! Thanks Makoto!
0

JTextField [] slot = new JTextField[3]; should be created outside MyFrame() constructor

To make it available to pullHandler you should also try making slot as static

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.