简体   繁体   English

多行 JTextField 的问题

[英]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 文档

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

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

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