简体   繁体   English

如何将Vector输出到JFrame中的JTextArea?

[英]How to output a Vector into a JTextArea in a JFrame?

Let's just say i have a Vector of Strings, and I want to output them to a JTextArea. 让我们说我有一个字符串向量,我想将它们输出到JTextArea。 What methods should I use? 我应该使用哪些方法?

I was thinking of using a for loop with: 我正在考虑使用for循环:

Vector temp = new Vector();
String temp = list.getText(i) + '\n';
textArea.setText(temp);

Which I know does not work. 我所知道的不起作用。 I heard of append() doing something related, but wasn't sure what to do. 我听说append()做了一些相关的事情,但不知道该怎么做。 Any tips? 有小费吗?

well, you're close. 好吧,你很亲密。 I think what you meant was... 我想你的意思是......

Vector<String> list;
...
String tmp = "";
for( int i = 0 ; i < list.size(); i++ )
{
   tmp = tmp + list.get(i) + "\n"; 
}
textArea.setText( tmp );

And in regard to your other comment, yes, whenever running a loop that appends a string value, you're going to want to use StringBuffer instead of string... 关于你的其他评论,是的,每当运行一个追加字符串值的循环时,你就会想要使用StringBuffer而不是string ...

Vector<String> list;
...
StringBuffer tmp = new StringBuffer();
for( int i = 0 ; i < list.size(); i++ )
{
    tmp.append( list.get(i) + "\n");
}
textArea.setText( tmp.toString() );

Firstly, Vector is a synchronized container. 首先, Vector是一个同步容器。 What this means is that it's thread safe. 这意味着它是线程安全的。 Unless you are planning to access it using multiple threads, there's little point and you're better off just using an ArrayList . 除非您计划使用多个线程访问它,否则您最好只使用ArrayList

Secondly, unless you're using a really ancient version of Java, you'll want to make sure you're using a parametrised container, ie: 其次,除非你使用的是古老版本的Java,否则你需要确保使用的是参数化容器,即:

List<String> list = new ArrayList<String>();

To check for methods on a JTextArea, the Java API is your friend. 要检查JTextArea上的方法, Java API是您的朋友。

You'll also need to loop over your container to append all its elements as well. 您还需要遍历容器以附加其所有元素。

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

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