new line in TextBox (JavaFX 2.0)

2019-04-13 23:46发布

问题:

I'm trying to create TextBox with JavaFX 2.0. My source is following:

TextBox textBox = new TextBox();
textBox.setPrefSize(150, 600);
textBox.setText("Hello\n world!");

Result is:

How could I create new line in TextBox?

回答1:

Creating a multilined TextBox is a JavaFX 1.3 feature. In JavaFX 2.0 you have to use a TextArea.

TextArea textArea = new TextArea();
textArea.setPrefRowCount(2);            
textArea.setText("Hello\nworld!");

The JavaFX UI Controls tutorial does not mention the TextArea control. Maybe they missed something. As you can see in this JavaFX 1.3 TextBox tutorial the TextBox had a 'multiline' and a 'lines' property. JavaFX 1.3 did not have a TextArea.