[英]Problem with multi-line JTextField
I have a JTextField
and need to have 3 lines in it, such as these:我有一个
JTextField
并且需要在其中包含 3 行,例如:
HI
MY name
is mehdi
How can I set the text in a JTextField
?如何在
JTextField
中设置文本? I tried \n
, but it didn't work.我试过
\n
,但它没有用。
1: JTextField does not support multiline. 1:JTextField 不支持多行。 what you want is a JTextArea
你想要的是一个 JTextArea
2: Newlines aren't the same on all systems, you might have to use "\r\n"
if you are running Windows. 2:所有系统上的换行符都不相同,如果您正在运行 Windows,则可能必须使用
"\r\n"
。 Since Java 7, there is System.LineSeparator
to solve this issue.从 Java 7 开始,就有了
System.LineSeparator
来解决这个问题。
(see the Oracle documentation ) (参见Oracle 文档)
You can't get multi-line behavior on single-line JTextField,您无法在单行 JTextField 上获得多行行为,
use JTextArea instead改用 JTextArea
info:信息:
http://answers.yahoo.com/question/index?qid=20080405030738AAJcKjU http://answers.yahoo.com/question/index?qid=20080405030738AAJcKjU
http://download.oracle.com/javase/6/docs/api/javax/swing/JTextArea.html http://download.oracle.com/javase/6/docs/api/javax/swing/JTextArea.html
JTextArea taText = new JTextArea();
taText.setText("\tHI\nMY name\nis Raku");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.