[英]Java 2D array is not saving the user input
I am using a GUI and every time the user inputs the information, it won't save to the 2D array.我使用的是 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;
}
});
I know it does not save to the 2D array because in another class that inherits the above class, the randQ label shows up as null
我知道它不会保存到 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);
create a variable to know, at which index the next question/answer will be stored into arrayQues.创建一个变量以了解下一个问题/答案将在哪个索引处存储到 arrayQues 中。
private int quesIndex;
the in you method actionPerformed
, call在你的方法
actionPerformed
,调用
String answer = textFieldAns.getText();
textFieldAns.selectAll();
arrayQues[1][quesIndex] = answer;
your questions should be inserted accordingly before taking the answer你的问题应该在回答之前相应地插入
arrayQues[0][quesIndex] = "insert your question here";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.