Literals 元件 API

本节列出了文本组件共享的 API 的常用部分。该 API 的大部分由JTextComponent类定义。 Literals 组件功能讨论如何使用其中一些 API。

JComponent 类描述了文本组件从JComponent继承的 API。有关与特定文本组件相关的 API 的信息,请参见该组件的使用方法页面:text fieldpassword field格式化文本字段text area编辑器窗格和文本窗格

有关文本 API 的完整详细信息,请参阅JTextComponent的 API 文档以及text package中的各种类和interface。

本节中列出的 API 包括以下类别:

设置属性这些方法在 JTextComponent 类中定义。

MethodDescription
void setEditable(boolean)

boolean isEditable()
设置或指示用户是否可以编辑文本组件中的文本。
void setDragEnabled(boolean)
boolean getDragEnabled()
设置或获取dragEnabled属性,该属性必须为 true 以启用对此组件的拖动处理。默认值为 false。有关更多详细信息,请参见拖放和数据传输
void setDisabledTextColor(Color)
Color getDisabledTextColor()
设置或获取禁用文本组件时用于显示文本的颜色。
void setMargin(Insets)
Insets getMargin()
设置或获取文本与文本组件边界之间的边距。

处理选择
这些方法在JTextComponent类中定义.

MethodDescription
String getSelectedText()获取当前选择的文本。
void selectAll()

void select(int,int)
选择所有文本或选择开始和结束范围内的文本。
void setSelectionStart(int)
void setSelectionEnd(int)
int getSelectionStart()
int getSelectionEnd()
按索引设置或获取当前选择的范围。
void setSelectedTextColor(Color)
Color getSelectedTextColor()
设置或获取所选文本的颜色。
void setSelectionColor(Color)
Color getSelectionColor()
设置或获取所选文本的背景颜色。

在模型和视图之间转换位置
这些方法在JTextComponent类中定义.

MethodDescription
int viewToModel(Point)将视图坐标系中的指定点转换为文本中的位置。
Rectangle modelToView(int)将文本内的指定位置转换为视图坐标系中的矩形。

Literals 编辑命令

类或方法Description
void cut()

void copy()
void paste()
void replaceSelection(String)
(在JTextComponent中)使用系统剪贴板剪切,复制和粘贴文本,或者分别用参数指定的字符串 替换所选文本。
EditorKit提供文本组件的视图工厂,文档,插入符号和操作,以及读写特定格式的文档。
DefaultEditorKit提供基本的文本编辑功能的EditorKit的具体子类。
StyledEditorKitDefault EditorKit的子类,为样式化文本提供其他编辑功能。
String xxxxAction
(在DefaultEditorKit中)
默认编辑器工具包支持的所有操作的名称。参见将文本操作与菜单和按钮关联
BeepAction
CopyAction
CutAction
DefaultKeyTypedAction
InsertBreakAction
InsertContentAction
InsertTabAction
PasteAction
(在DefaultEditorKit中)
实现各种文本编辑命令的内部类。
AlignmentAction
BoldAction
FontFamilyAction
FontSizeAction
ForegroundAction
ItalicAction
StyledTextAction
UnderlineAction
(在StyledEditorKit中)
实现样式化文本的各种编辑命令的内部类。
Action[] getActions()
(在JTextComponent中)
获取此组件支持的操作。如果组件使用了一个方法,则此方法将从编辑器工具箱中获取一系列操作。
InputMap getInputMap()
(在JComponent中)
获取将击键绑定到动作的 ImportingMap。参见将文本动作与按键关联
无效 put(KeyStroke,Object)
(在InputMap中)
将指定的键绑定到指定的操作。通常,您通过操作的名称来指定操作,对于标准的编辑操作,该操作由诸如DefaultEditorKit.backwardAction的字符串 常量表示。

代表文档的类和interface

interface或类Description
Document定义必须由所有文档实现的 API 的interface。
AbstractDocumentDocumentinterface的抽象超类实现。这是 Swing 文本包提供的所有文档的超类。
PlainDocument实现Documentinterface的类。这是纯文本组件(文本字段,密码字段和文本区域)的默认文档。此外,在加载纯文本或未知格式的文本时,编辑器窗格和文本窗格会使用此类。
StyledDocumentDocument子interface。定义必须由支持样式文本的文档实现的 API。 JTextPane要求其文档属于此类型。
DefaultStyledDocument实现StyledDocumentinterface的类。 JTextPane的默认文档。

处理文档

类或方法Description
DocumentFilter所有文档过滤器的超类。您可以使用文档过滤器来更改从文档中插入或删除的内容,而无需自己实现文档。参见实现文档过滤器
void setDocumentFilter(DocumentFilter)

(在AbstractDocument中)
设置文档过滤器。
void setDocument(Document)
Document getDocument()
(在JTextComponent中)
设置或获取文本组件的文档。
Document createDefaultModel()
(在JTextField中)
创建默认的 PlainDocument 模型。覆盖此方法以创建自定义文档,而不是默认的PlainDocument
void addDocumentListener(DocumentListener)
void removeDocumentListener(DocumentListener)
(在Document中)
添加或删除文档监听器。参见聆听文档中的更改
void addUndoableEditListener(UndoableEditListener)
void removeUndoableEditListener(UndoableEditlistener)
(在Document中)
添加或删除不可撤消的编辑监听器。 实现撤消和重做中使用了不可编辑的监听器。
int getLength()
Position getStartPosition()
Position getEndPosition()
字符串getText(int,int)
(在Document中)
Document方法返回有关文档的各种描述性信息。
Object getProperty(Object)
void putProperty(Object,Object)
(在Document中)
void setDocumentProperties(Dictionary)
Dictionary getDocumentProperties()
(在AbstractDocument中)
A Document维护了一组可以使用这些方法操作的属性。

处理 Caret 和选择荧光笔
这些方法在JTextComponent类中定义.

interface,类或方法Description
Caret一个interface,为代表文档内插入点的对象定义 API。
DefaultCaret所有文本组件使用的默认插入记号。
void setCaret(Caret)

Caret getCaret()
设置或获取文本组件使用的插入符号对象。
void setCaretColor(Color)
Color getCaretColor()
设置或获取插入符号的颜色。
void setCaretPosition(int)
void moveCaretPosition(int)
int getCaretPosition()
设置或获取插入符在文档中的当前位置。
void addCaretListener(CaretListener)
void removeCaretListener(CaretListener)
从文本组件中添加或删除插入符号侦听器。
NavigationFilter所有导航过滤器的超类。导航过滤器使您可以修改文本组件即将发生的插入符号更改。
void setNavigationFilter(NavigationFilter)将导航过滤器附加到文本组件。
Highlighter为用于突出显示当前选择的对象定义 API 的interface。
DefaultHighlighter所有文本组件使用的默认荧光笔。
void setHighlighter(Highlighter)
Highlighter getHighlighter()
设置或获取文本组件使用的荧光笔。

读写 Literals

MethodDescription
无效的 read(Reader,对象)

void write(Writer)
(在JTextComponent中)
读取或写入文本。
无效读取(阅读器,文档,诠释)
无效读取(InputStream,Document,int)
(在EditorKit中)
将流中的文本读取到文档中。
无效的 write(Writer,Document,int,int)
void write(OutputStream,Document,int,int)
(在EditorKit中)
将文本从文档写入流中。