2013-11-25 99 views
0
SlotMachine.java:76: cannot find symbol 
symbol : variable slot 
location: class MyFrame.pullHandler 

代碼在Java中找不到符號。 [編譯錯誤]

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

    } 
} 



} 

我試圖用插槽[]數組,而不是R1/R2/R3在我pullhandler類。 我嘗試閱讀舊帖子,但無法找到任何與我的問題非常相似的內容。

+2

下去變量的作用域讀了。 –

+2

變量'slot'對於'MyFrame'構造函數是本地的,對於'pullHandler'來說是不知道的。 –

回答

1

超出構造函數的範圍,變量slot沒有意義。如果您希望以其他方法訪問它,請將slot移至字段級別,並使用JTextField變量。

+0

問題是,只要我將它移到構造函數之外,我就會得到:SlotMachine.java:63非法啓動類型 slot [2] = new JTextField(「---」,10); ^ SlotMachine.java:63: expected slot [2] = new JTextField(「---」,10); ^ SlotMachine.java:63:';'預期 slot [2] = new JTextField(「---」,10); ' – user3029599

+1

爲什麼不能將'JTextField [] slot = new JTextField [3];'移出構造函數?無論如何,這就是你*可以移動的全部。 – Makoto

+0

啊,我認爲那是我的問題!我不應該移動「slot [0] = new JTextfield(」---「,10);部分!感謝Makoto! – user3029599

0

JTextField [] slot = new JTextField[3];應外MyFrame()構造函數來創建

將其提供給pullHandler你也應該嘗試使slotstatic