在 java 中有没有办法使用图形按钮设置数组的值?

Is there a way to set the value of an array using graphic buttons in java?

我正在尝试根据按下的按钮为数组设置值(数组为 [4])

我已经尝试使用 for 循环并使用 ActionListener,但它只是第一个值的 return 4 倍,我想将它设置为 4 个不同的值

package Whosebug;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class mainStack extends JFrame implements ActionListener {

    JButton jbr,jbv,jbb,jbo,jbn,jbj; 
    JTextField l11b;
    String a;
        mainStack(){

            this.setLayout(null);
            jbr = new JButton("Rouge");
            jbr.setBounds(0,80,85,30);
            add(jbr);

            jbv = new JButton("Vert");
            jbv.setBounds(125, 80, 85, 30);
            add(jbv);

            jbb = new JButton("Bleu");
            jbb.setBounds(0, 120, 85, 30);
            add(jbb);

            jbj = new JButton("Jaune");
            jbj.setBounds(125, 120, 85, 30);
            add(jbj);

            jbo = new JButton("Orange");
            jbo.setBounds(0, 160, 85,30);
            add(jbo);

            jbn = new JButton("Noir");
            jbn.setBounds(125,160, 85,30);
            add(jbn);

            jbr.addActionListener(this);
            jbv.addActionListener(this);
            jbb.addActionListener(this);
            jbj.addActionListener(this);
            jbo.addActionListener(this);
            jbn.addActionListener(this);

            setLayout(null);
            setSize(800,800);
            setVisible(true);
        }

            public void actionPerformed(ActionEvent e) {

                int i,b = 0;
                int tabAnswer[]= new int [4];
                for(i=0;i<tabAnswer.length;i++) {
                    if(e.getSource().equals(jbr)) {
                        a ="R";
                    }
                    else if(e.getSource().equals(jbv)) {
                        a = "V";
                    }
                    else if(e.getSource().equals(jbj)) {
                        a = "J";
                    }
                    else if(e.getSource().equals(jbb)) {
                        a = "B";
                    }
                    else if(e.getSource().equals(jbo)) {
                        a = "O";
                    }
                    else if(e.getSource().equals(jbn)) {
                        a = "N";
                    }
                    else { }
                    if(a.contentEquals("R")) {
                        b=0;
                    }
                    if(a.contentEquals("V")) {
                        b=1;
                    }
                    if(a.contentEquals("J")) {
                        b=2;
                    }
                    if(a.contentEquals("B")){
                        b=3;
                    }
                    if(a.contentEquals("O")) {
                        b=4;
                    }
                    if(a.contentEquals("N")) {
                        b=5;
                    }
                    tabAnswer[i]=b;

                }
                for(i=0;i<tabAnswer.length;i++) {

                    System.out.println(tabAnswer[i]);
                }

        }

    public static void main(String[] args) {
        mainStack t= new mainStack();

    }
}

我想让它等我按下按钮 4 次,显示这个数组的第 4 个值,但它只显示一个

编辑:如您所见,我的数组只取六个可能值中的一个。我希望能够使用按钮将其设置为 {0,1,2,3}。我希望现在更好。

您应该将 tabAnswerindex 提取到字段中。每次按下按钮时,在数组中设置当前 index 并递增 index。如果数组已满 (index >= tabAnswer.length) 打印数组或做任何您想用它做的事。 (可选)重置索引以防止 IndexOutOfBoundException.

这是一个例子:

private int index = 0;
private int[] tabAnswer = new int[4];

public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(jbr)) {
        tabAnswer[index] = 0;
    } else if (e.getSource().equals(jbv)) {
        tabAnswer[index] = 1;
    } else if (e.getSource().equals(jbj)) {
        tabAnswer[index] = 2;
    } else if (e.getSource().equals(jbb)) {
        tabAnswer[index] = 3;
    } else if (e.getSource().equals(jbo)) {
        tabAnswer[index] = 4;
    } else if (e.getSource().equals(jbn)) {
        tabAnswer[index] = 5;
    }
    index++;
    if (index >= tabAnswer.length) {
        System.out.println(Arrays.toString(tabAnswer));
        index = 0;
    }
}