将缓冲读取器中的内容保存到JTextArea

3

我希望我的缓冲读取器的结果显示在文本区域中,但是它并没有起作用。

我希望文本区域能够像系统输出一样准确地获取结果,这是为了多行输出。我尝试用字符串s设置文本区域,但没有成功,只给了我一行的结果。

以下是我的代码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JTextArea;

import java.awt.ScrollPane;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Window extends JFrame {

/**
 * Launch the application.
 * @throws FileNotFoundException 
 */
public static void main(String[] args) {

    Window frame = new Window();
    frame.setTitle("SWMA Extractor");
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setBounds(50, 50, 665, 550);
    //frame.setLocation(500, 300);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.getContentPane().setLayout(null);

}

/**
 * Create the frame.
 */
public Window() {
    setResizable(false);

    JPanel contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel path = new JLabel("File Location");
    path.setHorizontalAlignment(SwingConstants.CENTER);
    path.setBounds(20, 11, 74, 23);
    contentPane.add(path);

    final JTextField location = new JTextField();
    location.setBounds(104, 12, 306, 20);
    contentPane.add(location);
    location.setColumns(10);

    final JTextArea textArea = new JTextArea();

    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setBounds(20, 80, 605, 430);
    contentPane.add(scrollPane);
    scrollPane.add(textArea);


    JButton btn = new JButton("Get Info.");
    btn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            chooser.showOpenDialog(null);
            File f = chooser.getSelectedFile();
            File output = null;
            try {
                FileReader fr = new FileReader(f);
                BufferedReader br = new BufferedReader(fr);
                String s;
                int lineNumber = 1;
                while((s = br.readLine()) != null) {
                    if (s.contains("System")) {
                        System.out.println(s);
                        String nextLine = br.readLine();
                        System.out.println(nextLine);
                        String nextLine1 = br.readLine();
                        System.out.println(nextLine1);
                        String nextLine2 = br.readLine();
                        System.out.println(nextLine2);
                        String nextLine3 = br.readLine();
                        System.out.println(nextLine3);
                        System.out.println();

                    }
               }
                lineNumber++; 

            } catch (IOException e2) {
                e2.printStackTrace();
            }
        } 
    });
    btn.setBounds(433, 11, 192, 23);
    contentPane.add(btn);

    JButton clr = new JButton("Clear");
    clr.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String getLocation = location.getText();
            String s;
            try {
                textArea.setText("");
                location.setText("");
            } catch (Exception e1) {

            }
        }
    });
    clr.setBounds(20, 45, 605, 23);
    contentPane.add(clr);
}

}


1
第一个问题 -> frame.getContentPane().setLayout(null);,还要看一下JTextArea#read。你为什么在尝试从f读取时写入它? - MadProgrammer
@MadProgrammer 这是一个错误 :) - Aboelmagd Saad
@slartidan 它已经在while循环中了 :) while((s = br.readLine()) != null) - Aboelmagd Saad
三种方法来实现这个。一次读取每行并追加到文本区域中。或者直接读取文件的每一行到末尾,然后追加到文本区域中。或者从文件中读取每一行,然后使用 StringBuilder 来构建文本区域的全部内容,并将文本区域的内容设置为 StringBuilder 对象的 toString() 方法的返回值。 - ManoDestra
1
是的,这是意外的,抱歉,现在已经更正了 ;) - Aboelmagd Saad
显示剩余2条评论
1个回答

1
我看到你说你尝试设置文本,但是setText方法实际上会用新的文本替换整个当前文本:
JTextComponent @1669:
((AbstractDocument)doc).replace(0, doc.getLength(), t,null);

你应该使用插入追加方法:
替换
System.out.println(s); 

使用

textArea.append(s);

此外,查看以下问题以了解更好的实现方式:

在JTextArea中打开、编辑和保存文本到.txt文件

private void fileRead(){
    try{    
        FileReader read = new FileReader("filepath");
        Scanner scan = new Scanner(read);
            while(scan.hasNextLine()){
            String temp = scan.nextLine() + System.lineSeparator();
            storeAllString = storeAllString + temp;
        }
    }
    catch (Exception exception) {
        exception.printStackTrace();
    }
}      

@Hovercraft的建议非常好。如果您不想以任何方式处理文件,则可以直接将其读入JTextArea:

try {
    FileReader fr = new FileReader(f);
    BufferedReader br = new BufferedReader(fr);
    textArea.read(br, "Stream description");
} catch (IOException e2) {
    e2.printStackTrace();
}

非常感谢,这对我很有效 :) - Aboelmagd Saad
1
最好使用JTextArea的read(...)方法和BufferedReader直接读取文本内容。 - Hovercraft Full Of Eels

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接