问题描述
所以我想有我的程序从一个txt文件中读取行的列表。这然后显示在JTextArea。用户可使用输入数据的JTextField,目标是要显示万岁,如果用户在JArea和文本匹配错!如果他们不。任何帮助是AP preciated。
公共类TextArea1 { JTextArea中的文本;
JFrame的框架;
JTextField的文本框;
公众诠释K表;
公众的ArrayList ALIST;
公共字符串correctAnswer; 公共静态无效的主要(字串[] args){
TextArea1 GUI =新TextArea1(); gui.go();
} 私人字符串TEXTLINE; 公共无效围棋(){
帧=新的JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
的JPanel面板=新JPanel();
文本框=新的JTextField();
textField.addActionListener(新startTextFieldListener(正确答案));
JButton的startButton =的新的JButton(开始!);
startButton.addActionListener(新startButtonListener(ALIST));
文本=新的JTextArea(30,60);
text.setLineWrap(真); JScrollPane的滚动=新JScrollPane的(文本);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); panel.add(滚动); 。frame.getContentPane()加(BorderLayout.CENTER,面板);
frame.getContentPane()加(BorderLayout.WEST,startButton)。
。frame.getContentPane()加(BorderLayout.SOUTH,文本框);
frame.setSize(350,300);
frame.setVisible(真);
}
类startButtonListener实现的ActionListener {
ArrayList的ALIST; startButtonListener(ArrayList的passedInList){
ALIST = passedInList;
} @覆盖
公共无效的actionPerformed(ActionEvent的事件){
字符串文件名=的test.txt;
串线;
ArrayList的ALIST =新的ArrayList(); 尝试{
尝试(输入的BufferedReader =新的BufferedReader(新的FileReader(文件名))){
如果(!input.ready()){
抛出新的IOException异常(); } 而((行= input.readLine())!= NULL){
aList.add(线);
}
}
}赶上(IOException异常五){
的System.out.println(E); } INT SZ = aList.size(); 对于(INT K = 0; K< SZ; k ++){ 字符串correctAnswer = aList.get(K)的ToString(); text.append(aList.get(k)的的ToString());
text.append(\\ n);
}
}
} 类startTextFieldListener实现的ActionListener {
串correctAnswer; startTextFieldListener(字符串回答){
correctAnswer =答案;
} @覆盖
公共无效的actionPerformed(ActionEvent的事件){
如果(text.getText()。等于(correctAnswer)){
JOptionPane.showMessageDialog(NULL,万岁!);
}其他{
JOptionPane.showMessageDialog(NULL,错了!);
} }
}
}
好吧试试这个:
进口的javax.swing *。
进口java.awt中的*。
java.awt.event中导入*。
进口java.io. *;
进口的java.util。*;公共类TextArea1 {
JTextArea中的文本;
JFrame的框架;
JTextField的文本框;
公众诠释K表;
公众的ArrayList ALIST;
公共字符串correctAnswer; 公共静态无效的主要(字串[] args){
TextArea1 GUI =新TextArea1(); gui.go();
}
私人字符串TEXTLINE; 公共无效围棋(){
帧=新的JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
的JPanel面板=新JPanel();
文本框=新的JTextField();
JButton的startButton =的新的JButton(开始!);
startButton.addActionListener(新startButtonListener());
文本=新的JTextArea(30,60);
text.setLineWrap(真); JScrollPane的滚动=新JScrollPane的(文本);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); panel.add(滚动); 。frame.getContentPane()加(BorderLayout.CENTER,面板);
frame.getContentPane()加(BorderLayout.WEST,startButton)。
。frame.getContentPane()加(BorderLayout.SOUTH,文本框);
frame.setSize(350,300);
frame.setVisible(真);
} 类startButtonListener实现的ActionListener {
ArrayList的ALIST;
公共无效的actionPerformed(ActionEvent的事件){
字符串文件名=的test.txt;
串线;
ArrayList的<串GT; ALIST =新的ArrayList<>(); 尝试{
尝试(输入的BufferedReader =新的BufferedReader(新的FileReader(文件名))){
如果(!input.ready()){
抛出新的IOException异常(); } 而((行= input.readLine())!= NULL){
aList.add(线);
}
}
}赶上(IOException异常五){
的System.out.println(E); }
INT SZ = aList.size();
布尔结果= FALSE;
对(串T:ALIST){
如果(t.equalsIgnoreCase(textField.getText())){
JOptionPane.showMessageDialog(NULL,万岁,加载文件内容....!);
诠释计数= 0;
对于(INT K = 0; K< SZ; k ++){
text.append(aList.get(k)的的ToString());
的System.out.println(计数);
算上++;
//如果(K< SZ-1)
// text.append(,);
text.append(\\ n);
}
结果=真;
打破;
} 其他{
结果= FALSE;
}
}
如果(!结果){
JOptionPane.showMessageDialog(NULL,错了!);
}
}
}
}
在此会找你在文字输入文本框,如果它匹配它将被行添加整个文件的内容一致。我已经测试。但是,请记住我不是有足够的时间,所以,我没有做过的正则表达式模式匹配是在完全相同的文本框输入在文本你的文本文件一行简单的等于比较。
so I am trying to have my program read a list of lines from a txt file. This is then displayed in a JTextArea. The user can input data using the JTextField and the goal is to display "Hooray" if the user matches the text in the JArea and "Wrong!" if they do not. Any help is appreciated.
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!");
}
}
}
}
Ok Try this one:
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!");
}
}
}
}
In this it will look for you text enter in textfield and if it matches it will add entire file content line by line. i have already tested. But remember i am not having sufficient time so ,i have not done regex pattern matching it is simple equal comparison with one line in you text file with text entered in textbox exactly.
这篇关于JTextArea中ArrayList的字符串数据和JTextField中输入数据不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!