简体   繁体   English

如何在Java NetBeans中使用jtextarea进行多行输入?

[英]How to take multi line input using jtextarea in java netbeans?

What i want to do is take input from the user in multi lines, suppose user inputs some details in multi line textarea control 我想做的是从用户那里多行输入,假设用户在多行textarea控件中输入了一些细节
================ ================
Sarah 莎拉
Jones 琼斯
Chris 克里斯
Samantha 萨曼莎(Samantha)
================ ================
Now i want to insert these lines into an array, modify the details a bit 现在我想将这些行插入到数组中,稍微修改一下细节
then show them in a second textarea or label. 然后在第二个文本区域或标签中显示它们。
I want the output something like this 我想要这样的输出
================ ================
Welcome sarah 欢迎萨拉
welcome jones 欢迎琼斯
welcome chris 欢迎克里斯
welcome samantha 欢迎萨曼莎
================ ================

I heard we can do this using split method but it is not giving me the result 我听说我们可以使用split方法做到这一点,但它没有给我结果
i want. 我想要。 This is the code i prepared so far. 这是我到目前为止编写的代码。


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){                                         
       String[] names = jTextArea1.getText().split("\\.");
       for(int i=0;i<names.length;i++)
       {
        jTextArea2.setText("welcome "+names[i]);
       }
}

output is 输出是
============== ==============
welcome sarah 欢迎莎拉
jones 琼斯
chris 克里斯
samantha 萨曼莎
============== ==============
welcome is only printed once, what am i doing wrong? 欢迎只打印一次,我在做什么错?

private void jButton1ActionPerformed (ActionEvent evt){
   // are you sure that this split returns the names? Maybe you should split by \\n
   String[] names = jTextArea1.getText().split("\\n");
   // build the text to set into textarea2
   String text = "";
   for(int i=0;i<names.length;i++)
   {
      text += "welcome "+names[i]+"\n";
   }

   jTextArea2.setText(text);
}

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

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