Text
组件The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
This section provides background information you might need when using Swing text components. 本节提供了使用Swing文本组件时可能需要的背景信息。If you intend to use an unstyled text component a text field, password field, formatted text field, or text area go to its how-to page and return here only if necessary. 如果您打算使用非样式文本组件文本字段、密码字段、格式化文本字段或文本区域转到它的“操作”页面,仅在必要时返回此处。If you intend to use a styled text component, see How to Use Editor Panes and Text Panes, and read this section as well. 如果您打算使用样式化文本组件,请参阅如何使用编辑器窗格和文本窗格,并阅读本节。If you do not know which component you need, read on.如果您不知道需要哪个组件,请继续阅读。
Swing text components display text and optionally allow the user to edit the text. Swing文本组件显示文本,并允许用户编辑文本。Programs need text components for tasks ranging from the straightforward (enter a word and press Enter) to the complex (display and edit styled text with embedded images in an Asian language).程序需要文本组件来完成从简单(输入单词并按Enter键)到复杂(显示和编辑带有亚洲语言嵌入图像的样式文本)的任务。
Swing provides six text components, along with supporting classes and interfaces that meet even the most complex text requirements. Swing提供了六个文本组件,以及支持类和接口,它们甚至可以满足最复杂的文本需求。In spite of their different uses and capabilities, all Swing text components inherit from the same superclass, 尽管使用和功能不同,所有Swing文本组件都继承自同一个超类JTextComponent
, which provides a highly-configurable and powerful foundation for text manipulation.JTextComponent
,它为文本操作提供了高度可配置和强大的基础。
The following figure shows the 下图显示了JTextComponent
hierarchy.JTextComponent
层次结构。
The following picture shows an application called 下图显示了一个名为TextSamplerDemo
that uses each Swing text component.TextSamplerDemo
的应用程序,它使用每个Swing文本组件。
setEditable
.setEditable
,该文本已变得不可编辑。The TextSamplerDemo
example uses the text components in very basic ways. TextSamplerDemo
示例以非常基本的方式使用文本组件。The following table tells you more about what you can do with each kind of text component.下表详细介绍了如何处理每种文本组件。
JTextField JPasswordField JFormattedTextField
| ||
JTextArea |
JTextArea | |
JEditorPane JTextPane |
This Tutorial provides information about the foundation laid by the 本教程提供了有关JTextComponent
class and tells you how to accomplish some common text-related tasks.JTextComponent
类奠定的基础的信息,并告诉您如何完成一些常见的文本相关任务。
To learn more about text components in JavaFX, see the Using Text and Text Effects in JavaFX and Using JavaFX UI Controls: Text Field tutorials.要了解有关JavaFX中文本组件的更多信息,请参阅使用JavaFX中的文本和文本效果和使用JavaFXUI控件:文本字段教程。