JTextArea ArrayList 字符串数据和 JTextField 输入数据不匹配 [英] JTextArea ArrayList string data and JTextField input data not matching

查看:21
本文介绍了JTextArea ArrayList 字符串数据和 JTextField 输入数据不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图让我的程序从 txt 文件中读取行列表.然后将其显示在 JTextArea 中.用户可以使用 JTextField 输入数据,目标是在用户匹配 JAarea 中的文本和Wrong!"时显示Hooray".如果他们不这样做.任何帮助表示赞赏.

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!");
            }

        }
    }
}

推荐答案

好的 试试这个:

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 输入数据不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆