Java 将ActionListeners与for循环一起使用

Java 将ActionListeners与for循环一起使用,java,user-interface,for-loop,Java,User Interface,For Loop,在这段代码中,我想根据在for循环中多次选择哪个按钮来执行多个ActionListener,但它似乎不起作用。我试过用很多不同的方式来表达,但我是一个初学者,所以我不确定我是否做对了 public void mainGame() { QnA.setVisible(true); setContentPane(QnA); Random r = new Random(); JOptionPane.showMessageDialog(CCRTry.this, "Inst

在这段代码中,我想根据在for循环中多次选择哪个按钮来执行多个ActionListener,但它似乎不起作用。我试过用很多不同的方式来表达,但我是一个初学者,所以我不确定我是否做对了

public void mainGame() {
    QnA.setVisible(true);
    setContentPane(QnA);

    Random r = new Random();
    JOptionPane.showMessageDialog(CCRTry.this, "Instructions: You must enter the corresponding symbol to the currency that is output."
            + "\nExample: For an output of 'Australian Dollar', the correct answer would be AUD(Caps don't matter)"
            + "\nFor every right answer you get 10 points"
            + "\nYou can choose to quit in the middle by typing 'exit', or save during the game by typing 'save'"
            + "\nThere are 115 currencies"
            + "\nGet ready..."
            + "\nStart!");

    for (j = 0; j < 114; j++) {
        if (j<114) {
            temp = names.get(r.nextInt(names.size()));
            questionTextField.setText(temp);
            if (j == 0) {
                JOptionPane.showMessageDialog(CCRTry.this, "Input the corresponding symbol!");
            }
            symb = pleaseInputYourAnswerTextField.getText();

            numberOfQuestionsTextField.setText(Integer.toString(j));

        }

    }
}
public CCRTry() {

    super("Welcome to the Currency Converter");
    setContentPane(panel1);
    QnA.setVisible(false);
    pack();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    Extra.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (Extra.getSelectedItem()=="Output Currency Rates for the Selected Currency"){
                Print.add(null, namedata[1], 0);
            }
            if (Extra.getSelectedItem()=="Play The Game"){

                setInputFile("/Users/Aadithya/IdeaProjects/Lesson/src/CurrencyConverterReader/currencysymbol.xls");
                try{
                    mainRead();
                }catch(IOException g){
                    g.printStackTrace();
                }
                mainGame();
            }
            if (Extra.getSelectedItem()=="Change the Currency Rate"){

            }
        }
    });

    exitButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int response = JOptionPane.showConfirmDialog(null, "Do you want to continue? All unsaved data will be lost", "\nConfirm",
                    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (response == JOptionPane.YES_OPTION) {
                System.out.println("Yes was clicked...");
                setContentPane(panel1);
                return;
                //Saving into Arrays
            }
        }
    });
    saveButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("SAVING...");
            savexchdata = exchdata;
            savenamedata = namedata;
            savedata[0] = p;
            savedata[1] = j;
            System.out.println("You can type in 'exit' to exit");
            j--;
        }
    });
    OKButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            for (int i = 0; i < namedata.length; i++) {
                if (namedata[i].equalsIgnoreCase(temp)) {
                    tempo = i;
                    break;
                }
            }

            if (symb.equalsIgnoreCase(exchdata[tempo])) {
                p++;
                exchs.remove(tempo);
                names.remove(tempo);
                points0TextField.setText("Points: " + p);
                //Exiting Code
            }
            else {
                JOptionPane.showMessageDialog(CCRTry.this, "Sorry that ain't true!"+ "\nThe Correct answer is: " + exchdata[tempo]);
                exchs.remove(tempo);
                names.remove(tempo);
            }

        }
    });

谢谢添加。但是我不明白你的问题是什么。在方法mainGame中,for循环意味着遍历包含114个元素的数组。这是随机输出的,并放置在JTextField questionTextField中。然后,用户在pleaseInputYourAnswerTextField中键入问题的答案,并将答案放入临时变量中进行检查。当用户单击“确定”按钮时,我希望它转到OKButton.actionListener并执行,但随后返回到for循环,再次遍历,其中questionTextField将更改为另一个问题,整个过程将重复。您仍然只显示部分代码。我的建议是:停止使用更高级的应用程序,先学习基础知识。您可能认为自己对基础知识了解得足够多,但我看到的代码如下:Extra.getSelectedItem==玩游戏返回大约四次。这不是比较对象的方式,可能会导致错误的结果。好的。我想我会尝试和实验同时学习。把它看作是建造房子:你有没有计划在建立坚实的基础之前支撑砖墙?