簡體   English   中英

如何從Action Listener類中的For循環中獲取值(int)以在主類中使用?

[英]How to get the value(int) from a For Loop inside a Action Listener class to use in my main class?

我已經在主類之外聲明了一個全局變量( 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等於i ,則callButton[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,則將背景設置為黃色后,循環將繼續並重置背景顏色。 如果條件為真,則可能要break

然后,在ActionListener :也許您想要一個方法來檢查數字並在條件為true時設置背景。

當您啟動程序時, main的代碼(包含上述語句的for循環)僅運行一次(我想那是您要檢查number並設置顏色的部分)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM