[英]Java 2D array is not saving the user input
我使用的是 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);
创建一个变量以了解下一个问题/答案将在哪个索引处存储到 arrayQues 中。
private int quesIndex;
在你的方法actionPerformed
,调用
String answer = textFieldAns.getText();
textFieldAns.selectAll();
arrayQues[1][quesIndex] = answer;
你的问题应该在回答之前相应地插入
arrayQues[0][quesIndex] = "insert your question here";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.