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

Java 如何从Action Listener类中的For循环中获取值(int)以在我的主类中使用?,java,user-interface,actionlistener,Java,User Interface,Actionlistener,我在主类之外声明了一个全局变量(int number): public class BingoGUI { private static Random thing = new Random(); public static int number = 0; public static void main(String args[]) { public static void main(String args[]){ for (int i = 1

我在主类之外声明了一个全局变量(
int number
):

public class BingoGUI {

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

    public static void main(String args[]) {
    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]);
        }
然后,我需要在操作侦听器类中为我创建的
Jbutton
使用变量
number

    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);
        }
    }
这是我的全部代码(未完成的宾果游戏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);
        }
    }
}
import java.util.Random;
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.event.*;
导入com.sun.speech.freetts.Voice;
导入com.sun.speech.freetts.VoiceManager;
公共类BingoGUI{
私有静态int d=0;
//选项面板
私有静态字符串currentCall=“”;
私有静态JLabel调用=新JLabel(“”);
私有静态JButton nextCall=newjbutton(“下一次调用”);
私有静态JButton bingo=新JButton(“调用bingo!”);
私有静态随机事物=新随机();
公共静态整数=0;
私有静态JButton[]callButton=newjbutton[76];
//玩家卡
私有静态int[]number2=新int[76];
私有静态JButton[]playerButton=newjbutton[51];
//cpuCard
私有静态整数3=0;
私有静态JButton[]cpuNum=newjbutton[51];
私有静态JPanel playerCard=新JPanel();
//声音
私人静态语音v;
私有静态VoiceManager vm=VoiceManager.getInstance();
公共静态void main(字符串参数[]){
JFrame=newjframe(“宾果!”);
JPanel主面板=新的JPanel(新的FlowLayout(FlowLayout.LEFT));
JPanel callBoard=新的JPanel();
JPanel cpuCard=新的JPanel();
JPanel options=新的JPanel();
JLabel测试=新的JLabel(“”);
frame.setVisible(true);
frame.setresizeable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架设置尺寸(1000800);
mainPanel.setBorder(BorderFactory.createEmptyByOrder(5,5,5,5));
//呼叫板布局
对于(int i=1;i<76;i++){
callButton[i]=新的JButton(Integer.toString(i));
callButton[i].setFont(新字体(Integer.toString(i),Font.BOLD,9));
callButton[i].setForeground(颜色.蓝色);
callButton[i].setBackground(颜色.白色);
如果(i==数字){
callButton[i].setBackground(颜色.黄色);
}
callBoard.add(callButton[i]);
}
//游戏卡布局
对于(int a=1;a<51;a++){
如果(a==1 | a==11 | a==21 | a==31 | a==41 | a==6 | a==16 | a==26 | a==36 | a==46){
数字2[a]=1+事物下一个(15);
}如果(a==2 | | a==12 | | a==22 | | a==32 | | a==42 | | a==7 | a==17 | | a==27 | a==37 | a==47){
数字2[a]=16+事物下一个(15);
}如果(a==3 | | a==13 | | a==23 | | a==33 | | a==43 | | a==8 | | a==28 | a==38 | a==48){
数字2[a]=31+事物下一个(15);
}如果(a==4 | | a==14 | | a==24 | | a==34 | | a==44 | | a==9 | | a==29 | a==39 | a==49){
数字2[a]=46+事物。下一个(15);
}否则{
数字2[a]=61+事物下一个(15);
}
playerButton[a]=新的JButton(Integer.toString(number2[a]);
playerButton[a].setFont(新字体(Integer.toString(a),Font.BOLD,9));
playerButton[a].addActionListener(新的涂抹());
playerButton[a].后退背景(颜色.白色);
如果(a==23 | | a==28){
playerButton[a]=新的JButton(“”);
playerButton[a]。后退背景(颜色。红色);
} 
playerCard.add(playerButton[a]);
如果(a==5 | | a==15 | | a==25 | | a==35 | | a==45){
playerCard.add(Box.createRigidArea)(新维度(10,0));
}
}
//cpuCard中随机数的循环
对于(intb=1;b<51;b++){
如果(b==1 | b==11 | b==21 | b==31 | b==41 | b==16 | b==26 | b==36 | b==46){
数字3=1+事物下一个(15);
}如果(b==2 | b==12 | b==22 | b==32 | b==42 | b==7 | b==17 | b==27 | b==37 | b==47){
数字3=16+下一个(15);
}如果(b==3 | | b==13 | | b==23 | | b==33 | | b==43 | | b==8 | b==18 | | b==28 | b==38 | b==48){
数字3=31+下一个(15);
}如果(b==4 | b==14 | b==24 | b==34 | b==44 | b==9 | b==19 | b==29 | b==39 | b==49){
数字3=46+事物下一个(15);
}否则{
数字3=61+下一个(15);
}
cpuNum[b]=新的JButton(Integer.toString(number3));
cpuNum[b].setFont(新字体(Integer.toString(b),Font.BOLD,9));
cpuNum[b].挫折背景(颜色.白色);
如果(b==23 | | b==28){
cpuNum[b]=新的JButton(“”);
cpuNum[b].挫折背景(颜色.蓝色);
} 
如果(数字3==数字){
cpuNum[b].挫折背景(颜色.蓝色);
}
cpuCard.add(cpuNum[b]);
如果(b==5 | | b==15 | | b==25 | | b==35 | | b==45){
cpuCard.add(Box.createRigidArea)(新维度(10,0));
}
}
callBoard.setLayout(新的GridLayout(5,15));
playerCard.setLayout(新网格布局(5,10));
设置布局(新网格布局(5,10));
callBoard.setPreferredSize(新尺寸(700250));
playerCard.setPreferredSize(新尺寸(500250));
cpuCard.setPreferredSize(新尺寸(500250));
选项。添加(测试);
期权。添加(看涨期权);
addActionListener(新建theNextCall());
选项。添加(nextCall);
选项。添加(宾果游戏);
主面板。添加(呼叫板);
主面板。添加(cpuCard);
if (i == number){
    callButton[i].setBackground(Color.YELLOW);
}