简体   繁体   English

如何在JTextField中创建新行以键入?

[英]How can I create a new line in JTextField to type in?

I want to make a Swing program that writes updates on what you have done. 我想制作一个Swing程序,以编写有关您所做的更新。 It writes all the information on an uneditable JTextField . 它将所有信息写在不可编辑的JTextField

For example: 例如:

You have changed the text to BLUE.
You have changed the text to RED.

The problem is that I can't make the two lines separate. 问题是我无法将这两行分开。

What I get is this: 我得到的是:

You have changed the text to BLUE. You have changed the text to RED.

What I've tried is this: (This does not work) 我已经尝试过的是:(这不起作用)

TF_BetInfo.setText(TF_BetInfo.getText() + "\nYou have changed the text to BLUE.");
TF_BetInfo.setText(TF_BetInfo.getText() + "\nYou have changed the text to RED.");

you can't have multiple lines with JTextField , you can use JTextArea for that purpose , and the code wouldn't be much different too , you can still use the same code or you can just 您不能在JTextField中使用多行代码,可以使用JTextArea来实现此目的,并且代码也没有太大不同,您仍然可以使用相同的代码,也可以

TF_BetInfo.append("\nyou have ...... ");

where TF_Betinfo is a JTextArea rather than a JTextField . 其中TF_Betinfo是JTextArea而不是JTextField。

You can't use JTextField for this. 您不能为此使用JTextField JTextField is sinle-line edit control. JTextField是单行编辑控件。 That is why you got all your output in one line. 这就是为什么将所有输出都放在一行中的原因。

If you want several lines to be printed in edit use JTextArea . 如果要在编辑中打印多行,请使用JTextArea In your case you can use jTextArea.append(string) (note: jTextArea is an object of class JTextArea , and string is an object of class String ). 在您的情况下,可以使用jTextArea.append(string) (注意: jTextArea是类JTextArea的对象,而string是类String的对象)。

You can't actually use several lines in a JTextField , but what you can do, is using a JTextArea of the wanted size, and then use the following: 实际上不能在JTextField中使用几行 ,但是您可以做的是使用所需大小的JTextArea ,然后使用以下代码:

yourJTextArea.setLineWrap(true);

This will automatically detect when the JTextArea needs to use another line and add it, pretty cool, useful and you only need one code line to do it. 这将自动检测 JTextArea何时需要使用另一行并添加它,这非常酷,有用,并且您只需要一个代码行即可完成。

JTextArea is more flexible but if you really want flexibility read about JTextPane or JEditorPane , you can show URLS, internet pages and everything that comes to your mind. JTextArea更加灵活,但是如果您真的想了解JTextPaneJEdi​​torPane的灵活性,则可以显示URL,互联网页面以及您想到的所有内容。

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

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