Java 2D数组未保存用户输入

问题描述 投票:2回答:1

我使用的是GUI,每次用户输入信息时,它都不会保存到2D数组中。

public String [][] arrayQues = new String[2][5];

textFieldAns = new JTextField();
textFieldAns.setBounds(127, 130, 272, 42);
textFieldAns.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String Ans = textFieldAns.getText();
        textFieldAns.selectAll();
        arrayQues[1][0] = Ans;
    }
});

我知道它不会保存到2D数组,因为在另一个继承上述类的类中,randQ标签显示为null

    Random r = new Random();
    int randAnswer = r.nextInt(arrayQues[1].length);
    int randQuestion = r.nextInt(arrayQues[0].length);
    String randQ = arrayQues[0][randQuestion];
    String randA = arrayQues[1][randQuestion];

    JLabel randomQuestion = new JLabel(randQ);
    randomQuestion.setBounds(112, 61, 269, frame.getContentPane().add(randomQuestion);

java windowbuilder
1个回答
1
投票

创建一个变量以知道,下一个问题/答案将在哪个索引存储放入arrayQues。

private int quesIndex;

您的方法actionPerformed,调用

String answer = textFieldAns.getText();
textFieldAns.selectAll();
arrayQues[1][quesIndex] = answer;

在回答问题之前,您的问题应相应插入

arrayQues[0][quesIndex] = "insert your question here";
© www.soinside.com 2019 - 2024. All rights reserved.