Question

I have declared a global variable (int number) outside the main class:

public class BingoGUI {

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

    public static void main(String args[]) {

Then I need to use the variable number in an action listener class for a Jbutton I created:

    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);
        }
    }

The Purpose is every time I click on the button (that has theNextCall action listener), if number equals i in my main class, callButton[i] will change background colour to yellow. But for some reason I can't access number from the Action Listener class, the number in my main class keeps getting the initial value I declared in the beginning which is 0.

Inside my main class:

    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]);
        }

This is my entire code (unfinished bingo game gui) if you need the reference:

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);
        }
    }
}
Was it helpful?

Solution

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

In your for-loop, there is this, which should set the color. However, if i equals the number, and, say i is 2, after setting the background to yellow, the loop continues and resets the background color. You might want to break if the condition is true.

Then, in the ActionListener: maybe you want a method there that checks the number and sets the background if the condition is true.

The code in the main (the for loop containing the above statement) is only run once when you start the program (I suppose that's the part where you want to check for number and set the color).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top