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

vin*_*tes 1 java swing multiline jtextfield

我想制作一个 Swing 程序来更新你所做的事情。它将所有信息写在一个不可编辑的JTextField.

例如:

You have changed the text to BLUE.
You have changed the text to RED.
Run Code Online (Sandbox Code Playgroud)

问题是我不能把两条线分开。

我得到的是这样的:

You have changed the text to BLUE. You have changed the text to RED.
Run Code Online (Sandbox Code Playgroud)

我试过的是这样的:(这不起作用)

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.");
Run Code Online (Sandbox Code Playgroud)

vla*_*lka 5

您不能使用 JTextField 有多行,您可以为此使用 JTextArea,并且代码也不会有太大不同,您仍然可以使用相同的代码,或者您可以

TF_BetInfo.append("\nyou have ...... ");
Run Code Online (Sandbox Code Playgroud)

其中 TF_Betinfo 是 JTextArea 而不是 JTextField 。