Documentation

The Java™ Tutorials
Hide TOC
The Text Component APIText组件API
Trail: Creating a GUI With Swing
Lesson: Using Swing Components
Section: Using Text Components

The Text Component API文本组件API

This section lists commonly used parts of the API that are shared by text components. 本节列出了文本组件共享的API常用部分。Much of this API is defined by the JTextComponent class. 该API的大部分由JTextComponent类定义。Text Component Features discusses how to use some of this API.文本组件功能讨论了如何使用一些API。

The JComponent ClassJComponent describes the API that text components inherit from JComponent. 描述文本组件从JComponent继承的API。For information about the API related to specific text components, see the how-to page for that component: text field, password field, formatted text field, text area, or editor pane and text pane.有关与特定文本组件相关的API的信息,请参阅该组件的操作指南页:文本字段密码字段格式化文本字段文本区域编辑器窗格和文本窗格

For complete details about the text API, see the API documentation for JTextComponent and for the various classes and interfaces in the text package.有关文本API的完整详细信息,请参阅JTextComponent的API文档以及文本包中的各种类和接口。

The API listed in this section includes the following categories:本节列出的API包括以下类别:

Setting Attributes设置属性
These methods are defined in the JTextComponent class.这些方法在JTextComponent类中定义。
Method方法 Description描述
void setEditable(boolean)
boolean isEditable()
Sets or indicates whether the user can edit the text in the text component.设置或指示用户是否可以编辑文本组件中的文本。
void setDragEnabled(boolean)
boolean getDragEnabled()
Sets or gets the dragEnabled property, which must be true to enable drag handling on this component. 设置或获取dragEnabled属性,该属性必须为true才能在此组件上启用拖动处理。The default value is false. 默认值为falseSee Drag and Drop and Data Transfer for more details.有关详细信息,请参阅拖放和数据传输
void setDisabledTextColor(Color)
Color getDisabledTextColor()
Sets or gets the color used to display text when the text component is disabled.设置或获取禁用文本组件时用于显示文本的颜色。
void setMargin(Insets)
Insets getMargin()
Sets or gets the margin between the text and the text component's border.设置或获取文本和文本组件边框之间的边距。
Manipulating the Selection操纵选区
These methods are defined in the JTextComponent class.这些方法在JTextComponent类中定义。
Method方法 Description描述
String getSelectedText() Gets the currently selected text.获取当前选定的文本。
void selectAll()
void select(int, int)
Selects all text or selects text within a start and end range.选择所有文本或选择开始和结束范围内的文本。
void setSelectionStart(int)
void setSelectionEnd(int)
int getSelectionStart()
int getSelectionEnd()
Sets or gets the extent of the current selection by index.按索引设置或获取当前选择的范围。
void setSelectedTextColor(Color)
Color getSelectedTextColor()
Sets or gets the color of selected text.设置或获取选定文本的颜色。
void setSelectionColor(Color)
Color getSelectionColor()
Sets or gets the background color of selected text.设置或获取选定文本的背景色。
Converting Positions Between the Model and the View转换模型和视图之间的位置
These methods are defined in the JTextComponent class.这些方法在JTextComponent类中定义。
Method方法 Description描述
int viewToModel(Point) Converts the specified point in the view coordinate system to a position within the text.将视图坐标系中的指定点转换为文字中的某个位置。
Rectangle modelToView(int) Converts the specified position within the text to a rectangle in the view coordinate system.将文本中的指定位置转换为视图坐标系中的矩形。
Text Editing Commands文本编辑命令
Class or Method类或方法 Description描述
void cut()
void copy()
void paste()
void replaceSelection(String)
(in JTextComponent)
Cuts, copies, and pastes text using the system clipboard, or replaces the selected text with the string specified by an argument, respectively.使用系统剪贴板剪切、复制和粘贴文本,或分别用参数指定的字符串替换选定文本。
EditorKit Provides a text component's view factory, document, caret, and actions, as well as reading and writing documents of a particular format.提供文本组件的视图工厂、文档、插入符号和操作,以及读取和写入特定格式的文档。
DefaultEditorKit A concrete subclass of EditorKit that provides the basic text editing capabilities.EditorKit的一个具体子类,提供基本的文本编辑功能。
StyledEditorKit A subclass of Default EditorKit that provides additional editing capabilities for styled text.默认EditorKit的子类,为样式化文本提供额外的编辑功能。
String xxxxAction
(in DefaultEditorKit)
The names of all the actions supported by the default editor kit. 默认编辑器工具包支持的所有操作的名称。See Associating Text Actions with Menus and Buttons.请参见将文本操作与菜单和按钮关联
BeepAction
CopyAction
CutAction
DefaultKeyTypedAction
InsertBreakAction
InsertContentAction
InsertTabAction
PasteAction
(in DefaultEditorKit)
Inner classes that implement various text editing commands.实现各种文本编辑命令的内部类。
AlignmentAction
BoldAction
FontFamilyAction
FontSizeAction
ForegroundAction
ItalicAction
StyledTextAction
UnderlineAction
(in StyledEditorKit)
Inner classes that implement various editing commands for styled text.实现样式化文本的各种编辑命令的内部类。
Action[] getActions()
(in JTextComponent)
Gets the actions supported by this component. 获取此组件支持的操作。This method gets the array of actions from the editor kit if one is used by the component.如果组件使用编辑器工具包,则此方法从编辑器工具包中获取操作数组。
InputMap getInputMap()
(in JComponent)
Gets the input map that binds key strokes to actions. 获取将按键笔划绑定到动作的输入映射。See Associating Text Actions with Key Strokes.请参见将文本动作与按键笔划关联
void put(KeyStroke, Object)
(in InputMap)
Binds the specified key to the specified action. 将指定的键绑定到指定的操作。You generally specify the action by its name, which for standard editing actions is represented by a string constant such as DefaultEditorKit.backwardAction.通常通过其名称指定操作,对于标准编辑操作,该名称由字符串常量表示,例如DefaultEditorKit.backwardAction
Classes and Interfaces That Represent Documents表示文档的类和接口
Interface or Class接口或类 Description描述
Document An interface that defines the API that must be implemented by all documents.定义所有文档必须实现的API的接口。
AbstractDocument An abstract superclass implementation of the Document interface. Document接口的抽象超类实现。This is the superclass for all documents provided by the Swing text package.这是Swing文本包提供的所有文档的超类。
PlainDocument A class that implements the Document interface. 实现Document接口的类。This is the default document for the plain text components (text field, password field, and text area). 这是纯文本组件(文本字段、密码字段和文本区域)的默认文档。Additionally, this class is used by the editor panes and text panes when loading plain text or text of an unknown format.此外,在加载纯文本或未知格式的文本时,编辑器窗格和文本窗格使用此类。
StyledDocument A Document subinterface. Document子接口。Defines the API that must be implemented by documents that support styled text. 定义支持样式化文本的文档必须实现的API。JTextPane requires that its document be of this type.JTextPane要求其文档为这种类型。
DefaultStyledDocument A class that implements the StyledDocument interface. 实现StyledDocument接口的类。The default document for JTextPane.JTextPane的默认文档。
Working With Documents处理文档
Class or Method类或方法 Description描述
DocumentFilter The superclass of all document filters. 所有文档筛选器的超类。You can use a document filter to change what gets inserted or removed from a document, without having to implement a document yourself. 您可以使用文档筛选器来更改从文档中插入或删除的内容,而无需自己实现文档。See Implementing a Document Filter.请参见实现文档筛选器
void setDocumentFilter(DocumentFilter)
(in AbstractDocument)
Sets the document filter.设置文档筛选器。
void setDocument(Document)
Document getDocument()
(in JTextComponent)
Sets or gets the document for a text component.设置或获取文本组件的文档。
Document createDefaultModel()
(in JTextField)
Creates a default PlainDocument model. 创建默认的明文文档模型。Override this method to create a custom document instead of the default PlainDocument.重写此方法以创建自定义文档,而不是默认的PlainDocument
void addDocumentListener(DocumentListener)
void removeDocumentListener(DocumentListener)
(in Document)
Adds or removes a document listener. 添加或删除文档侦听器。See Listening for Changes on a Document.请参见侦听文档上的更改
void addUndoableEditListener(UndoableEditListener)
void removeUndoableEditListener(UndoableEditlistener)
(in Document)
Adds or removes an undoable edit listener. 添加或删除可撤消的编辑侦听器。Undoable edit listeners are used in Implementing Undo and Redo.可撤消编辑侦听器用于实现撤消和重做
int getLength()
Position getStartPosition()
Position getEndPosition()
String getText(int, int)
(in Document)
Document methods that return various descriptive information about the document.返回有关文档的各种描述性信息的Document方法。
Object getProperty(Object)
void putProperty(Object, Object)
(in Document)
void setDocumentProperties(Dictionary)
Dictionary getDocumentProperties()
(in AbstractDocument)
A Document maintains a set of properties that you can manipulate with these methods.Document维护一组属性,您可以使用这些方法操作这些属性。
Manipulating Carets and Selection Highlighters操纵插入符号和选择高亮灯
These methods are defined in the JTextComponent class.这些方法在JTextComponent类中定义。
Interface, Class, or Method接口、类或方法 Description描述
Caret An interface that defines the API for objects that represent an insertion point within documents.为表示文档中插入点的对象定义API的接口。
DefaultCaret The default caret used by all text components.所有文本组件使用的默认插入符号。
void setCaret(Caret)
Caret getCaret()
Sets or gets the caret object used by a text component.设置或获取文本组件使用的插入符号对象。
void setCaretColor(Color)
Color getCaretColor()
Sets or gets the color of the caret.设置或获取插入符号的颜色。
void setCaretPosition(int)
void moveCaretPosition(int)
int getCaretPosition()
Sets or gets the current position of the caret within the document.设置或获取插入符号在文档中的当前位置。
void addCaretListener(CaretListener)
void removeCaretListener(CaretListener)
Adds or removes a caret listener from a text component.在文本组件中添加或删除插入符号侦听器。
NavigationFilter The superclass for all navigation filters. 所有导航筛选器的超类。A navigation filter lets you modify caret changes that are about to occur for a text component.导航筛选器允许您修改文本组件即将发生的插入符号更改。
void setNavigationFilter(NavigationFilter) Attaches a navigation filter to a text component.将导航筛选器附加到文本组件。
Highlighter An interface that defines the API for objects used to highlight the current selection.定义用于突出显示当前选择的对象的API的接口。
DefaultHighlighter The default highlighter used by all text components.所有文本组件使用的默认高亮灯。
void setHighlighter(Highlighter)
Highlighter getHighlighter()
Sets or gets the highlighter used by a text component.设置或获取文本组件使用的高亮灯。
Reading and Writing Text读写文本
Method方法 Description描述
void read(Reader, Object)
void write(Writer)
(in JTextComponent)
Reads or writes text.读或写文本。
void read(Reader, Document, int)
void read(InputStream, Document, int)
(in EditorKit)
Reads text from a stream into a document.将文本从流读取到文档中。
void write(Writer, Document, int, int)
void write(OutputStream, Document, int, int)
(in EditorKit)
Writes text from a document to a stream.将文档中的文本写入流。

Previous page: Text Component Features
Next page: How to Use Various Components