java 如何使文本区域文本在行完成后自动转到下一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15306373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:10:49  来源:igfitidea点击:

How to make text area text automatically go to the next line when line is finished?

javaswingjtextareaword-wrap

提问by akki0996

This is my code for text field. It is not changing the line when the line ends, but continues to go on, making the frame look weird.

这是我的文本字段代码。行结束时并没有改变行,而是继续前进,使框架看起来很奇怪。

I tried to use a scroll pane, but it did not help me.

我尝试使用滚动窗格,但它没有帮助我。

JTextArea TextArea = new JTextArea(7,10);
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
TextPanel.add(TextArea, gridConstraints);
TextArea.setEditable(true); 

回答by Reimeus

Without line & word wrapping, the size of a JTextAreawill grow beyond its original size. You could call:

如果没有换行和自动换行,a 的大小JTextArea将超过其原始大小。你可以打电话:

textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);


Side notes:

边注:

  • JTextAreaare typically placed in JScollPanecontainers to facilitate scrolling.
  • Java naming conventions indicate that variables start with a lowercaseletter, making TextAreatextArea
  • JTextArea通常放置在JScollPane容器中以方便滚动。
  • Java 命名约定表明变量以小写字母开头,使得TextAreatextArea

回答by OtenMoten

I can provide a solution if you are using SceneBuilder.

如果您使用 SceneBuilder,我可以提供解决方案。

Select the TextArea-element from the hierarchy-tree, go to "Inspector", select the tab "Properties" and activate "Wrap Text".

从层级树中选择 TextArea 元素,转到“检查器”,选择“属性”选项卡并激活“换行文本”。

Then, every text within the TextArrea-element will have a return at the end of the line.

然后,TextArrea 元素中的每个文本都将在行尾返回。

Screenshot

截屏

Cheers

干杯