所以我试图让我的程序读取txt文件中的行列表。然后将其显示在JTextArea中。用户可以使用JTextField输入数据,如果用户匹配JArea中的文本并且“错误!”,则目标是显示“Hooray”。如果他们不这样做。任何帮助表示赞赏。
public class TextArea1 {
JTextArea text;
JFrame frame;
JTextField textField;
public int k;
public ArrayList aList;
public String correctAnswer;
public static void main(String[] args) {
TextArea1 gui = new TextArea1();
gui.go();
}
private String textLine;
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
textField = new JTextField("");
textField.addActionListener(new startTextFieldListener("correct answer"));
JButton startButton = new JButton("Start!");
startButton.addActionListener(new startButtonListener(aList));
text = new JTextArea(30, 60);
text.setLineWrap(true);
JScrollPane scroller = new JScrollPane(text);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scroller);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.getContentPane().add(BorderLayout.WEST, startButton);
frame.getContentPane().add(BorderLayout.SOUTH, textField);
frame.setSize(350, 300);
frame.setVisible(true);
}
class startButtonListener implements ActionListener {
ArrayList aList;
startButtonListener(ArrayList passedInList) {
aList = passedInList;
}
@Override
public void actionPerformed(ActionEvent event) {
String fileName = "test.txt";
String line;
ArrayList aList = new ArrayList();
try {
try (BufferedReader input = new BufferedReader(new FileReader(fileName))) {
if (!input.ready()) {
throw new IOException();
}
while ((line = input.readLine()) != null) {
aList.add(line);
}
}
} catch (IOException e) {
System.out.println(e);
}
int sz = aList.size();
for (int k = 0; k < sz; k++) {
String correctAnswer = aList.get(k).toString();
text.append(aList.get(k).toString());
text.append("\n");
}
}
}
class startTextFieldListener implements ActionListener {
String correctAnswer;
startTextFieldListener(String answer) {
correctAnswer = answer;
}
@Override
public void actionPerformed(ActionEvent event) {
if (text.getText().equals(correctAnswer)) {
JOptionPane.showMessageDialog(null, "Hooray!");
} else {
JOptionPane.showMessageDialog(null, "Wrong!");
}
}
}
}
答案 0 :(得分:0)
每次附加到textArea时都添加了一个新行的小错误..为什么它不会因为额外的新行而进行比较
如果你的文件中有新行没有问题,那么当你解析文件时,arrayList已经有了它,所以不需要添加新行。
<强>溶液强>
public class TextArea1{
JTextArea text;
JFrame frame;
JTextField textField;
public int k;
public String correctAnswer;
public static void main (String [] args) {
TextArea1 gui = new TextArea1();
gui.go();
}
private String textLine;
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
textField = new JTextField("");
//textField.addActionListener(new startTextFieldListener("correct answer"));
JButton startButton = new JButton ("Start!");
startButton.addActionListener(new startButtonListener());
text = new JTextArea (30, 60);
text.setLineWrap(true);
JScrollPane scroller = new JScrollPane(text);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scroller);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.getContentPane().add(BorderLayout.WEST, startButton);
frame.getContentPane().add(BorderLayout.SOUTH, textField);
frame.setSize(350, 300);
frame.setVisible(true);
}
class startButtonListener implements ActionListener {
ArrayList aList;
startButtonListener()
{
}
public void actionPerformed(ActionEvent event) {
String fileName = "test.txt";
String line;
String string = "";
try {
try (BufferedReader input = new BufferedReader (new FileReader(fileName))) {
if (!input.ready()) {
throw new IOException();
}
while ((line = input.readLine()) !=null) {
string += line;
//aList.add(line);
}
System.out.println("List ready to check. values in list are :"+aList);
}
} catch (IOException e) {
System.out.println(e);
}
if (string.equals(textField.getText())) {
JOptionPane.showMessageDialog(null, "Hooray!");
}
else {
JOptionPane.showMessageDialog(null, "Wrong!");
}
}
}
}
答案 1 :(得分:0)
好吧,我刚刚修改了你的代码,以便在列表中包含值时获得正确的消息。相应地查看和修改。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class TextArea1{
JTextArea text;
JFrame frame;
JTextField textField;
public int k;
public String correctAnswer;
public static void main (String [] args) {
TextArea1 gui = new TextArea1();
gui.go();
}
private String textLine;
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
textField = new JTextField("");
//textField.addActionListener(new startTextFieldListener("correct answer"));
JButton startButton = new JButton ("Start!");
startButton.addActionListener(new startButtonListener());
text = new JTextArea (30, 60);
text.setLineWrap(true);
JScrollPane scroller = new JScrollPane(text);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scroller);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.getContentPane().add(BorderLayout.WEST, startButton);
frame.getContentPane().add(BorderLayout.SOUTH, textField);
frame.setSize(350, 300);
frame.setVisible(true);
}
class startButtonListener implements ActionListener {
ArrayList aList;
startButtonListener()
{
}
public void actionPerformed(ActionEvent event) {
String fileName = "test.txt";
String line;
ArrayList aList = new ArrayList();
try {
try (BufferedReader input = new BufferedReader (new FileReader(fileName))) {
if (!input.ready()) {
throw new IOException();
}
while ((line = input.readLine()) !=null) {
aList.add(line);
}
System.out.println("List ready to check. values in list are :"+aList);
}
} catch (IOException e) {
System.out.println(e);
}
if (aList.contains(text.getText())) {
JOptionPane.showMessageDialog(null, "Hooray!");
}
else {
JOptionPane.showMessageDialog(null, "Wrong!");
}
}
}
}
如果这是正确的,则将其标记为正确。
答案 2 :(得分:0)
好的尝试一下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class TextArea1{
JTextArea text;
JFrame frame;
JTextField textField;
public int k;
public ArrayList aList;
public String correctAnswer;
public static void main (String [] args) {
TextArea1 gui = new TextArea1();
gui.go();
}
private String textLine;
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
textField = new JTextField("");
JButton startButton = new JButton ("Start!");
startButton.addActionListener(new startButtonListener());
text = new JTextArea (30, 60);
text.setLineWrap(true);
JScrollPane scroller = new JScrollPane(text);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scroller);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.getContentPane().add(BorderLayout.WEST, startButton);
frame.getContentPane().add(BorderLayout.SOUTH, textField);
frame.setSize(350, 300);
frame.setVisible(true);
}
class startButtonListener implements ActionListener {
ArrayList aList;
public void actionPerformed(ActionEvent event) {
String fileName = "test.txt";
String line;
ArrayList <String>aList = new ArrayList<>();
try {
try (BufferedReader input = new BufferedReader (new FileReader(fileName))) {
if (!input.ready()) {
throw new IOException();
}
while ((line = input.readLine()) !=null) {
aList.add(line);
}
}
} catch (IOException e) {
System.out.println(e);
}
int sz = aList.size();
boolean result=false;
for(String t:aList){
if (t.equalsIgnoreCase(textField.getText())) {
JOptionPane.showMessageDialog(null, "Hooray! Loading File contents....");
int count=0;
for (int k = 0; k< sz; k++) {
text.append(aList.get(k).toString());
System.out.println(count);
count++;
// if(k<sz-1)
// text.append(", ");
text.append("\n");
}
result=true;
break;
}
else {
result=false;
}
}
if(!result){
JOptionPane.showMessageDialog(null, "Wrong!");
}
}
}
}
在此,它将在textfield中查找文本输入,如果匹配,则会逐行添加整个文件内容。我已经测试过了。但请记住我没有足够的时间,所以我没有进行正则表达式模式匹配,它与文本文件中的一行简单相等,文本框中输入的文字完全正确。