我想创建一个程序来玩抽认卡。程序随机给出抽认卡一侧的内容,并要求用户输入另一侧。我希望该程序能够实时检查,而不是需要一个按钮来提交答案。据我了解,我需要一个 DocumentListener 来做到这一点。但是,在编码方面非常新,我在实现它时遇到了麻烦。 该程序运行,但每当我按下一个键时,它都会在控制台中出现以下错误:
<块引用>线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException:无法调用“javax.swing.JTextField.getText()”,因为“Play.Answer”为空
关注
<块引用>在 Play$MyDocumentListener.answer(Play.java:75)
<块引用>在 Play$MyDocumentListener.insertUpdate(Play.java:60)
<块引用>[..]
<块引用>在 java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
<块引用>在 java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
这是代码:
import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Play {
public static JFrame frame;
public static JPanel panel;
public static JLabel givenSide; //side of the flashcard the player gets
public static JTextField Answer; //player inputs answer here
static String lineToPrint; //this string contains the info the player gets
//static String lineGiven; //input given by player
static String correctAnswer; //this string contains the correct answer
public static void PlayGUI() throws IOException {
frame = new JFrame();
frame.setSize(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
panel = new JPanel();
panel.setSize(100, 100);
panel.setBackground(Color.WHITE);
panel.setVisible(true);
frame.add(panel);
panel.setLayout(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
printGivenSide();
JTextField Answer = new JTextField(20);
Answer.setBounds(10, 50, 100, 25);
panel.add(Answer);
Answer.getDocument().addDocumentListener(new MyDocumentListener());
Answer.setVisible(true);
}
static class MyDocumentListener implements DocumentListener {
@Override
public void insertUpdate(DocumentEvent arg0) {
answer();
}
@Override
public void removeUpdate(DocumentEvent arg0) {
answer();
}
@Override
public void changedUpdate(DocumentEvent arg0) {
answer();
}
public void answer() {
//lineGiven = Answer.getText();
if (Answer.getText().equals(correctAnswer)) {correct();}
}
}
public static void printGivenSide() throws IOException {
File file = new File("C:\\Users\\39340\\Desktop\\storageapp\\Flashcard0.txt");
//counts lines in the txt document
int lines = 0;
BufferedReader br = new BufferedReader(new FileReader(file));
while (br.readLine() != null) {
lines++;
}
//randomly selects a line to read
int lineToRead = 0 + (int)(Math.random() * ((lines - 1) + 1));
//reads lineToRead
lineToPrint = Files.readAllLines(Paths.get("C:\\Users\\39340\\Desktop\\storageapp\\Flashcard0.txt")).get(lineToRead);
if (lineToRead == 1) {
correctAnswer = Files.readAllLines(Paths.get("C:\\Users\\39340\\Desktop\\storageapp\\Flashcard0.txt")).get(0);
} else correctAnswer = Files.readAllLines(Paths.get("C:\\Users\\39340\\Desktop\\storageapp\\Flashcard0.txt")).get(1);
System.out.println("linea mostrata: " + lineToPrint);
System.out.println("risposta corretta: " + correctAnswer);
//System.out.println(lineToPrint);
//System.out.println("ho generato "+lineToPrint +" e le linee sono " + lines);
//creates a label to display one side of the flashcard
JLabel givenSide = new JLabel(lineToPrint);
givenSide.setBounds(10, 20, 100, 25);
panel.add(givenSide);
givenSide.setVisible(true);
}
public static void correct() {
JLabel correct = new JLabel ("correct");
correct.setBounds(10, 75, 100, 25);
panel.add(correct);
correct.setVisible(true);
}
public static void main(String[] args) throws IOException {
PlayGUI();
}
}