简体   繁体   English

如何在JTextArea中显示对象?

[英]How to display object in a JTextArea?

I have a Paper class that has 4 attributes. 我有一个具有4个属性的Paper类。

public class Paper(int id, String author, String title, int rank){

}

Then I have a LinkedList that contains the paper object. 然后,我有一个包含纸张对象的LinkedList。

I have a GUI that displays the paper object in the JTextArea. 我有一个GUI在JTextArea中显示纸张对象。 GUI has buttons that let users to add paper, delete and save the file. GUI具有让用户添加纸张,删除和保存文件的按钮。 When users press save button the Paper objects are saved in a txt file as follows: 当用户按下“保存”按钮时,“纸张”对象被保存在txt文件中,如下所示:

46,Evolutionary Comp,Michael Smith,12/01/10,4 
61,Fuzzy Logic and App,John Peterson,13/01/10,3 
118,Neural Networks,Arthur London,20/01/10,5 
200,Evolutionary Comp,Scott Jones,30/01/10,1 
210,Fuzzy Logic and App,Joe Wang,01/02/10,4 

How do I display the content of txt file in the the JTextArea. 如何在JTextArea中显示txt文件的内容。 Paper class has toString() method that prints the data of Paper Object. Paper类具有toString()方法,用于打印Paper Object的数据。

Thanks. 谢谢。 If the question is not very clear please make a comment and I will try and make it as clear as possible. 如果问题不是很清楚,请发表评论,我会尝试使其尽可能清楚。

I haven't actually tried this myself, but it would seem you could use JTextArea.append(String) or JTextArea.insert(String, int). 我实际上尚未亲自尝试过,但似乎可以使用JTextArea.append(String)或JTextArea.insert(String,int)。

Details: http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html 详细信息: http : //docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html

In general, if you want to know what you can do with a given type Foo, try googling "java Foo" and javadoc pages usually show up as the first few hits. 通常,如果您想知道使用给定类型Foo可以做什么,请尝试使用谷歌搜索“ java Foo”,并且javadoc页面通常显示为前几项。

JTextarea.append(string) worked perfectly for me. JTextarea.append(string)对我来说效果很好。 Thanks 谢谢

// addPaper() returns paper object
displayTxtArea.append(addPaper().toString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM