在actionListener里面的hashMap中添加项目

时间:2018-05-13 18:19:50

标签: java swing hashmap

我试图制作一个简单的GUI程序,可用于在游戏中做出多种选择。我知道我必须采取所有问题,他们的选择和正确的答案。我的问题是,当我尝试在hashmap中收集正确的答案时,我只得到最后一个真正的答案,并且它在hashmap中重复。另外,我在收集选项时收到了一个问题并用一个不寻常的for循环解决了这个问题,但不幸的是我无法用答案做到这一点。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;

public class ExamFrame extends JFrame {

    private ExamPanel examPanel;
    private static int count = 1;
    static HashMap<Integer,ArrayList<String>> choicesMap = new HashMap<>();
    static ArrayList<String> choicesList = new ArrayList<>();
    static HashMap<Integer,ArrayList<String>> truesMap = new HashMap<>();
    static ArrayList<String> truesList = new ArrayList<>();
    static ArrayList<String> prompts = new ArrayList<>();

    public ExamFrame() {
        super("Making exam");
        examPanel = new ExamPanel();
        setContentPane(examPanel);
        setSize(700,600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    class ExamPanel extends JPanel implements ActionListener{
        private JLabel quLabel;
        private JTextField questionField;
        private ArrayList<JCheckBox> jCheckBoxes;
        private ArrayList<JTextField> respnsesFields;
        private JButton suivant,terminer;
        private JPanel centerPanel;

        public ExamPanel() {
            centerPanel = new JPanel(new BorderLayout());
            suivant = new JButton("next");
            suivant.addActionListener(this);
            terminer = new JButton("Finish");
            terminer.setEnabled(false);
            terminer.addActionListener(this);
            layoutComponents();
        }

        public void layoutComponents() {
            setBackground(Color.green);
            setLayout(new BorderLayout());
            JPanel prince = new JPanel(new GridBagLayout());
            GridBagConstraints gc = new GridBagConstraints();
            gc.fill = GridBagConstraints.NONE;
            prince.setBackground(Color.CYAN);
            gc.gridy = 0;
            gc.weightx = 1;
            gc.weighty = 0.1;
            gc.insets = new Insets(0, 0, 0, 5);
            gc.anchor = GridBagConstraints.LINE_END;
            gc.gridx = 0;
            quLabel = new JLabel("Question: ");
            prince.add(quLabel,gc);
            gc.gridx = 1;
            gc.insets = new Insets(0, 0, 0, 0);
            gc.anchor = GridBagConstraints.LINE_START;
            questionField = new JTextField(45);
            prince.add(questionField, gc);
            jCheckBoxes = new ArrayList<>();
            respnsesFields = new ArrayList<>();

            for (int j = 0; j<2; j++) {
                JCheckBox jCheckBox= new JCheckBox("Reponse "+(j+1));
                jCheckBoxes.add(jCheckBox);
                gc.gridy++;
                gc.weightx = 1;
                gc.weighty = 0.1;
                gc.insets = new Insets(0, 0, 0, 5);
                gc.anchor = GridBagConstraints.LINE_END;
                gc.gridx=0;
                prince.add(jCheckBox,gc);
                JTextField jTextField = new JTextField(40);
                respnsesFields.add(jTextField);
                gc.gridx=1;
                gc.insets = new Insets(0, 0, 0, 0);
                gc.anchor = GridBagConstraints.LINE_START;
                prince.add(jTextField,gc);
            }
            gc.gridy++;
            gc.weightx = 1;
            gc.weighty = 0.1;
            gc.insets = new Insets(0, 0, 0, 5);
            gc.anchor = GridBagConstraints.LINE_END;
            gc.gridx=0;
            prince.add(suivant,gc);
            gc.gridx=1;
            gc.insets = new Insets(0, 0, 0, 0);
            gc.anchor = GridBagConstraints.LINE_START;
            prince.add(terminer,gc);
            centerPanel.add(prince,BorderLayout.CENTER);
            add(centerPanel,BorderLayout.CENTER);
        }

    public void actionPerformed(ActionEvent e) {
        suivant.setEnabled(false);
        if (count<=3) { //3 == Number of questions
            suivant.setEnabled(true);
            if (e.getSource() == suivant) {
                String prompt = questionField.getText();
                prompts.add(prompt);
                ExamPanel.this.setVisible(false);
                ExamFrame.this.setContentPane(new ExamPanel());
                for (int i=0;i<2;i++) { //2 == Number of choices
                    choicesList.add(respnsesFields.get(i).getText());
                }
                for (int i=0;i<2;i++) {
                    if (jCheckBoxes.get(i).isSelected()) {
                        truesList.add(respnsesFields.get(i).getText());
                    }
                }
                truesMap.put(count,truesList);
                truesList.clear();
                count++;
            }
        }
        else {
            terminer.setEnabled(true);
            questionField.setEnabled(false);
            for (JTextField jTextField:respnsesFields) {
                jTextField.setEnabled(false);
            }
            suivant.setEnabled(false);
            if (e.getSource() == terminer) {
                System.out.println(choicesList);
                for (int i=0;i<3;i++) {
                    ArrayList<String> arrayList = new ArrayList<>();
                    for (int j=(i*2);j<(i+1)*2;j++) {
                        arrayList.add(choicesList.get(j));
                    }
                    choicesMap.put(i,arrayList);
                }
                System.out.println(choicesMap);
                System.out.println(truesMap);
                System.exit(0);
            }
        }
    }
}

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ExamFrame();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

您需要为放置在Map中的每个不同项目创建 new ArrayList。您的程序只有一个所有Map项共享的truesList ArrayList实例。在ActionListener的actionPerformed方法内部,创建新的ArrayList(如果你需要一个 - 如果你要创建一个新项目放入Map中),并将它与Integer键一起放入Map中。

如,

if (e.getSource() == suivant) {
    truesList = new ArrayList<>(); // !!  ADDED *****
    String prompt = questionField.getText();
    prompts.add(prompt);
    ExamPanel.this.setVisible(false);
    ExamFrame.this.setContentPane(new ExamPanel());
    for (int i = 0; i < 2; i++) { // 2 == Number of choices
        choicesList.add(respnsesFields.get(i).getText());
    }
    for (int i = 0; i < 2; i++) {
        if (jCheckBoxes.get(i).isSelected()) {
            truesList.add(respnsesFields.get(i).getText());
        }
    }
    truesMap.put(count, truesList);
    // !! truesList.clear();  // !! REMOVED ****
    count++;
}

旁注:如果您使用的是Map<Integer, List<String>>,并且整数键字符串随着您的情况单调增加,为什么要使用地图呢?为什么不简单地使用嵌套列表?

List<List<String>> truesList = new ArrayList<>();

外部列表的索引与HashMap的Integer键相同。