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



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

public class BingoGUI {
    private static Random thing = new Random();
    public static int number = 0;
    public static void main(String args[]) {

然后,我需要在我创建的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);
        }
    }

目的是每次我点击按钮(有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);
        }
    }
}
if (i == number){
    callButton[i].setBackground(Color.YELLOW);
}

在for循环中,有一个,它应该设置颜色。然而,如果i等于数字,并且假设i是2,则在将背景设置为黄色之后,循环继续并重置背景颜色。如果条件为true,则可能需要break

然后,在ActionListener中:如果条件为true,则可能需要一个检查数字并设置背景的方法。

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

相关内容

  • 没有找到相关文章

最新更新