Problem with multi-line JTextField

半世苍凉 提交于 2020-01-09 05:37:25

问题


I have a JTextField and need to have 3 lines in it, such as these:

HI
MY name
is mehdi

How can I set the text in a JTextField? I tried \n, but it didn't work.


回答1:


1 : JTextField does not support multiline. what you want is a JTextArea

2 : Newlines aren't the same on all systems, you might have to use "\r\n" if you are running Windows. Since Java 7, there is System.LineSeparator to solve this issue.

(see the Oracle documentation)




回答2:


You can't get multi-line behavior on single-line JTextField,

use JTextArea instead

info:

http://answers.yahoo.com/question/index?qid=20080405030738AAJcKjU
http://download.oracle.com/javase/6/docs/api/javax/swing/JTextArea.html




回答3:


JTextArea taText = new JTextArea();
taText.setText("\tHI\nMY name\nis Raku");


来源:https://stackoverflow.com/questions/6662029/problem-with-multi-line-jtextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!