gpt4 book ai didi

java - 从缓冲读取器保存到 JTextArea

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:31 25 4
gpt4 key购买 nike

我想让缓冲阅读器的结果出现在文本区域中,但这对我不起作用。

我希望文本区域得到与系统输出完全一样的结果,它不止一行,我尝试用字符串 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);
}

最佳答案

我看到你说你尝试设置文本,但 setText 方法实际上用新文本替换了整个当前文本:

JTextComponent @1669:
((AbstractDocument)doc).replace(0, doc.getLength(), t,null);

你应该使用 insertappend方法:

替换

System.out.println(s); 

textArea.append(s);

此外,检查以下问题以获得更好的方法:

Opening, Editing and Saving text in JTextArea to .txt file

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();
}

关于java - 从缓冲读取器保存到 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35998304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com