将ActionListeners与for循环一起使用

时间:2015-01-09 10:58:31

标签: java user-interface for-loop

在这段代码中,我想执行多个ActionListener,具体取决于在for循环中多次选择哪个按钮,但它似乎不起作用。我尝试过多种不同的方式,但我是初学者,所以我不确定我做得对。

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

        }
    });

0 个答案:

没有答案