JAVA学习:文本框的设计与实现

本文介绍了JAVA中用于显示多行文本的JTextArea组件,对比了它与textField、editor pane、text pane及label的区别。JTextArea可通过设定行数和列数调整大小,通常默认可编辑,但可通过setEditable(false)设置为只读。此外,还讨论了如何设置字体、颜色、自动换行和缩进,以及启用拖放功能。文章提供了API的分类,包括内容设置、外观调整和功能实现。
摘要由CSDN通过智能技术生成

在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);
The two arguments to the  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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值