Documentation

The Java™ Tutorials
Hide TOC
Using Text Components使用Text组件
Trail: Creating a GUI With Swing
Lesson: Using Swing Components

Using Text Components使用文本组件

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, JTextComponent, which provides a highly-configurable and powerful foundation for text manipulation.尽管使用和功能不同,所有Swing文本组件都继承自同一个超类JTextComponent,它为文本操作提供了高度可配置和强大的基础。

The following figure shows the JTextComponent hierarchy.下图显示了JTextComponent层次结构。

Swing's hierarchy of text components

The following picture shows an application called TextSamplerDemo that uses each Swing text component.下图显示了一个名为TextSamplerDemo的应用程序,它使用每个Swing文本组件。

An application that provides a sample of each Swing text component

Try this: 
  1. Click the Launch button to run TextSamplerDemo using Java™ Web Start (download JDK 7 or later). 单击启动按钮,使用Java™Web启动运行TextSamplerDemo(下载JDK 7或更高版本)。Alternatively, to compile and run the example yourself, consult the example index.或者,要自己编译和运行示例,请参考示例索引Launches the TextSamplerDemo Application
  2. Type some text in the text field and press Enter. Do the same in the password field. 在文本字段中键入一些文本,然后按Enter键。在密码字段中执行相同操作。The label beneath the fields is updated when you press Enter.Enter键时,字段下方的标签将更新。
  3. Try entering valid and invalid dates into the formatted text field. Note that when you press Enter the label beneath the fields is updated only if the date is valid.尝试在格式化文本字段中输入有效和无效日期。请注意,按Enter键时,仅当日期有效时,字段下方的标签才会更新。
  4. Select and edit text in the text area and the text pane. 在文本区域和文本窗格中选择并编辑文本。Use keyboard bindings, Ctrl-X, Ctrl-C, and Ctrl-V, to cut, copy, and paste text, respectively.使用键盘绑定Ctrl-XCtrl-CCtrl-V分别剪切、复制和粘贴文本。
  5. Try to edit the text in the editor pane, which has been made uneditable with a call to setEditable.尝试在编辑器窗格中编辑文本,通过调用setEditable,该文本已变得不可编辑。
  6. Look in the text pane to find an example of an embedded component and an embedded icon.在文本窗格中查找嵌入式组件和嵌入式图标的示例。

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.下表详细介绍了如何处理每种文本组件。

Group Description描述 Swing ClassesSwing类
Text Controls文本控件 Also known simply as text fields, text controls can display only one line of editable text. 也称为文本字段,文本控件只能显示一行可编辑文本。Like buttons, they generate action events. 与按钮一样,它们生成动作事件。Use them to get a small amount of textual information from the user and perform an action after the text entry is complete.使用它们从用户处获取少量文本信息,并在文本输入完成后执行操作。 JTextField and its subclasses 及其子类JPasswordField and JFormattedTextField
Plain Text Areas纯文本区域 JTextArea can display multiple lines of editable text. 可以显示多行可编辑文本。Although a text area can display text in any font, all of the text is in the same font. 尽管文本区域可以以任何字体显示文本,但所有文本都是相同的字体。Use a text area to allow the user to enter unformatted text of any length or to display unformatted help information.使用文本区域允许用户输入任意长度的未格式化文本或显示未格式化的帮助信息。 JTextArea
Styled Text Areas样式化文本区域 A styled text component can display editable text using more than one font. 样式化文本组件可以使用多种字体显示可编辑文本。Some styled text components allow embedded images and even embedded components. 一些样式化的文本组件允许嵌入图像甚至嵌入组件。Styled text components are powerful and multi-faceted components suitable for high-end needs, and offer more avenues for customization than the other text components. 样式化文本组件是功能强大、多方面的组件,适合高端需求,并提供了比其他文本组件更多的定制途径。
Because they are so powerful and flexible, styled text components typically require more initial programming to set up and use. 由于它们功能强大且灵活,样式化文本组件通常需要更多的初始编程来设置和使用。One exception is that editor panes can be easily loaded with formatted text from a URL, which makes them useful for displaying uneditable help information.一个例外是编辑器窗格可以很容易地从URL加载格式化文本,这使得它们对于显示不可编辑的帮助信息非常有用。
JEditorPane
and its subclass及其子类
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控件:文本字段教程。


Previous page: The JComponent Class
Next page: Text Component Features