如何从我的主类中使用的Action Listener类中的For循环获取值(int)?

如何从我的主类中使用的Action Listener类中的For循环获取值(int)?

问题描述:

我已经宣布的主类之外的全局变量(int number):如何从我的主类中使用的Action Listener类中的For循环获取值(int)?

public class BingoGUI { 

    private static Random thing = new Random(); 
    public static int number = 0; 

    public static void main(String args[]) { 

然后,我需要使用变量number在动作监听器类为Jbutton我创建:

public static class theNextCall implements ActionListener { 

     public void actionPerformed(ActionEvent e) {  

      number = thing.nextInt(75) + 1; 
      if (number < 16) { 
       currentCall = "B" + number; 
      } else if (15 < number && number < 31) { 
       currentCall = "I" + number; 
      } else if (30 < number && number < 46) { 
       currentCall = "N" + number; 
      } else if (45 < number && number < 61) { 
       currentCall = "G" + number; 
      } else { 
       currentCall = "O" + number; 
      } 
      call.setText("the current call is: " + currentCall); 
     } 
    } 

的目的是我点击按钮(即具有theNextCall动作监听者),如果number在我的主类等于icallButton[i]将改变背景颜色为黄色每次。但由于某些原因,我不能从Action Listener类在我的主类访问number,该number状态越来越我是0

在我的主类开始申报初始值:

public static void main(String args[]){ 

     for (int i = 1; i < 76; i++) { 
      callButton[i] = new JButton(Integer.toString(i)); 
      callButton[i].setFont(new Font(Integer.toString(i), Font.BOLD, 9)); 
      callButton[i].setForeground(Color.BLUE); 
      callButton[i].setBackground(Color.WHITE); 
      if (i == number){ 
       callButton[i].setBackground(Color.YELLOW); 
      } 
     callBoard.add(callButton[i]); 
     } 

这是我的整个代码(未完宾果游戏GUI)如果您需要参考:

import java.util.Random; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.event.*; 
import com.sun.speech.freetts.Voice; 
import com.sun.speech.freetts.VoiceManager; 

public class BingoGUI { 

    private static int d = 0; 
    //options panel 
    private static String currentCall = "";    
    private static JLabel call = new JLabel(""); 
    private static JButton nextCall = new JButton("Next call"); 
    private static JButton bingo = new JButton ("Call Bingo!"); 
    private static Random thing = new Random(); 
    public static int number = 0; 
    private static JButton[] callButton = new JButton[76]; 
    //playerCard 
    private static int[] number2 = new int[76]; 
    private static JButton[] playerButton = new JButton[51]; 
    //cpuCard 
    private static int number3 = 0; 
    private static JButton[] cpuNum = new JButton[51]; 
    private static JPanel playerCard = new JPanel(); 
    //voice 
    private static Voice v; 
    private static VoiceManager vm = VoiceManager.getInstance(); 

    public static void main(String args[]){ 

     JFrame frame = new JFrame("BINGO!"); 
     JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 
     JPanel callBoard = new JPanel(); 
     JPanel cpuCard = new JPanel(); 
     JPanel options = new JPanel(); 
     JLabel test = new JLabel(""); 

     frame.setVisible(true); 
     frame.setResizable(false); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(1000,800); 
     mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     //callBoard layout 
     for (int i = 1; i < 76; i++) { 
      callButton[i] = new JButton(Integer.toString(i)); 
      callButton[i].setFont(new Font(Integer.toString(i), Font.BOLD, 9)); 
      callButton[i].setForeground(Color.BLUE); 
      callButton[i].setBackground(Color.WHITE); 
      if (i == number){ 
       callButton[i].setBackground(Color.YELLOW); 
      } 
     callBoard.add(callButton[i]); 
     } 
     //playerCard layout 
     for (int a = 1; a < 51; a++) { 
      if (a == 1 || a == 11 || a == 21 || a == 31 || a == 41 || a == 6 || a == 16 || a == 26 || a == 36 || a == 46){ 
      number2[a] = 1+thing.nextInt(15); 
      } else if (a == 2 || a == 12 || a == 22 || a == 32 || a == 42 || a == 7 || a == 17 || a == 27 || a == 37 || a == 47) { 
      number2[a] = 16+thing.nextInt(15); 
      } else if (a == 3 || a == 13 || a == 23 || a == 33 || a == 43 || a == 8 || a == 18 || a == 28 || a == 38 || a == 48) { 
      number2[a] = 31+thing.nextInt(15); 
      } else if (a == 4 || a == 14 || a == 24 || a == 34 || a == 44 || a == 9 || a == 19 || a == 29 || a == 39 || a == 49) { 
      number2[a] = 46+thing.nextInt(15); 
      } else { 
      number2[a] = 61+thing.nextInt(15); 
      } 
      playerButton[a] = new JButton(Integer.toString(number2[a])); 
      playerButton[a].setFont(new Font(Integer.toString(a), Font.BOLD, 9)); 
      playerButton[a].addActionListener(new daub()); 
      playerButton[a].setBackground(Color.WHITE); 
      if (a == 23 || a == 28) { 
      playerButton[a] = new JButton(""); 
      playerButton[a].setBackground(Color.RED); 
     } 
      playerCard.add(playerButton[a]); 
      if (a == 5 || a == 15 || a == 25 || a == 35 || a == 45){ 
       playerCard.add(Box.createRigidArea(new Dimension(10, 0))); 
      } 
     } 
     //loop for random number in cpuCard 
     for (int b = 1; b < 51; b++) { 
      if (b == 1 || b == 11 || b == 21 || b == 31 || b == 41 || b == 16 || b == 26 || b == 36 || b == 46){ 
      number3 = 1+thing.nextInt(15); 
      } else if (b == 2 || b == 12 || b == 22 || b == 32 || b == 42 || b == 7 || b == 17 || b == 27 || b == 37 || b == 47) { 
      number3 = 16+thing.nextInt(15); 
      } else if (b == 3 || b == 13 || b == 23 || b == 33 || b == 43 || b == 8 || b == 18 || b == 28 || b == 38 || b == 48) { 
      number3 = 31+thing.nextInt(15); 
      } else if (b == 4 || b == 14 || b == 24 || b == 34 || b == 44 || b == 9 || b == 19 || b == 29 || b == 39 || b == 49) { 
      number3 = 46+thing.nextInt(15); 
      } else { 
      number3 = 61+thing.nextInt(15); 
      } 
      cpuNum[b] = new JButton(Integer.toString(number3)); 
      cpuNum[b].setFont(new Font(Integer.toString(b), Font.BOLD, 9)); 
      cpuNum[b].setBackground(Color.WHITE); 
      if (b == 23 || b == 28) { 
      cpuNum[b] = new JButton(""); 
      cpuNum[b].setBackground(Color.BLUE); 
      } 
      if (number3 == number){ 
       cpuNum[b].setBackground(Color.BLUE); 
      } 
      cpuCard.add(cpuNum[b]); 
      if (b == 5 || b == 15 || b == 25 || b == 35 || b == 45){ 
       cpuCard.add(Box.createRigidArea(new Dimension(10, 0))); 
      } 
     } 
     callBoard.setLayout(new GridLayout(5,15)); 
     playerCard.setLayout(new GridLayout(5,10)); 
     cpuCard.setLayout(new GridLayout(5,10)); 
     callBoard.setPreferredSize(new Dimension(700, 250)); 
     playerCard.setPreferredSize(new Dimension(500, 250)); 
     cpuCard.setPreferredSize(new Dimension(500,250)); 
     options.add(test); 
     options.add(call); 
     nextCall.addActionListener(new theNextCall()); 
     options.add(nextCall); 
     options.add(bingo); 
     mainPanel.add(callBoard); 
     mainPanel.add(cpuCard); 
     mainPanel.add(Box.createRigidArea(new Dimension(100, 0))); 
     mainPanel.add(playerCard); 
     mainPanel.add(options); 
     mainPanel.setPreferredSize(new Dimension(1200, 600)); 
     frame.setContentPane(mainPanel); 
     frame.pack(); 
    } 

    public static class theNextCall implements ActionListener { 

     public void actionPerformed(ActionEvent e) {  
      v = vm.getVoice("kevin16"); 
      number = thing.nextInt(75) + 1; 
      if (number < 16) { 
       currentCall = "B" + number; 
      } else if (15 < number && number < 31) { 
       currentCall = "I" + number; 
      } else if (30 < number && number < 46) { 
       currentCall = "N" + number; 
      } else if (45 < number && number < 61) { 
       currentCall = "G" + number; 
      } else { 
       currentCall = "O" + number; 
      } 
      call.setText("the current call is: " + currentCall); 
      v.allocate(); 
      v.speak(currentCall); 
     } 
    } 
    private static class daub implements ActionListener { 

     public void actionPerformed(ActionEvent e) {  

      JButton daub = (JButton)e.getSource(); 
      daub.setBackground(Color.RED); 
     } 
    } 
} 
+0

如果你已经找到了这个问题的答案,请添加它作为一个答案。 [回答你自己的问题](http://meta.stackoverflow.com/help/self-answer)受到高度鼓励! –

+0

@The家伙的帽子我... ...有一个答案楼下有标记,我更新了我的代码,甚至可以说,这是固定的版本..仔细看看吧。 – HayleyL

if (i == number){ 
    callButton[i].setBackground(Color.YELLOW); 
} 

在你的for循环,还有这个,应该设置颜色。但是,如果我等于数字,并说,我是2,在将背景设置为黄色后,循环继续并重置背景颜色。如果条件为真,您可能需要break

然后,在ActionListener:也许你想要一个方法,检查数字并设置背景,如果条件是true

main(包含上述语句的for循环)中的代码仅在启动程序时运行一次(我想这是您要检查number并设置颜色的部分)。

+0

好的我添加到我的ActionListener类这种'INT Y =的Integer.parseInt(callButton.getText()); \t如果(数== y)的{ \t callButton.setBackground(Color.YELLOW); \t}'和删除主类相同的代码,但它仍然是行不通的,错误是'找不到符号callButton' – HayleyL

+0

你的ActionListener封装的东西。所以'callButton'可能在里面不可见。您可能想要编写另一个从监听器中调用的方法,因为在方法内(在该类的范围内),callButton将是可见的。 – blueygh2

+0

非常感谢!我被困在这个问题上2天,你真的帮了我。现在问题解决> HayleyL