在JAVA的官方Tutorials中有这么一段话:
The JTextArea
class provides a component that displays multiple lines of text and optionally allows the user to edit the text. If you need to obtain only one line of input from the user, you should use a text field. If you want the text area to display its text using multiple fonts or other styles, you should use an editor pane or text pane. If the displayed text has a limited length and is never edited by the user, use a label.
通过这段话,我们可知:对于要大段显示文字的应用场景,需要使用JTextArea,如果仅仅是为了获取一行文字,那么应给使用text field。如果想要实现多种字体和其他样式,那么就要使用editor pane 或者text pane。如果我们要显示的文字较短并且从来不用编辑,那么就直接用label就好了。
那么对于我的需求,我不需要花哨的样式,那么我仅仅使用JTextArea就好了。对于JTextArea的初始化,解释如下:
The following code creates and initializes the text area:
textArea = new JTextArea(5, 20); JScrollPane scrollPane = new JScrollPane(textArea); textArea.setEditable(false);
JTextArea
constructor are hints as to the number of rows and columns, respectively, that the text area should display. The scroll pane that contains the text area pays attention to these hints when determining how big the scroll pane should be.
从官方解释我们可以看出,通过定义显示文字的行数和列数是可以定义这个text area 的大小的。而包在外边的scrollPane是为了保证滚动显示的。
Text areas are editable by default. The code setEditable(false)
makes the text area uneditable. It is still selectable and the user can copy data from it, but the user cannot chan