java.lang.Object
java.awt.Component
java.awt.TextComponent
- 所有已实现的接口:
-
ImageObserver,MenuContainer,Serializable,Accessible
public sealed class TextComponent extends Component implements Accessible permits TextArea, TextField
TextComponent类是允许编辑某些文本的任何组件的超类。
文本组件包含一串文本。 TextComponent类定义了一组确定此文本是否可编辑的方法。如果组件是可编辑的,它定义了另一组支持文本插入插入符的方法。
此外,该类定义了用于维护从文本中选择的当前选择的方法。文本选择,组件文本的子字符串,是编辑操作的目标。它也被称为选定文本。
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected class此类为TextComponent类实现了辅助功能支持。Nested classes/interfaces declared in class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy -
Field Summary
FieldsFields declared in class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTFields declared in interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH -
Method Summary
Modifier and TypeMethodDescriptionvoid通过将其连接到本机屏幕资源,使此组件可显示。void将指定的文本事件侦听器添加到从此文本组件接收文本事件。voidenableInputMethods(boolean enable) 启用或禁用此文本组件的输入法支持。获取与此TextComponent关联的AccessibleContext。获取此文本组件的背景颜色。int返回文本插入插入符的位置。<T extends EventListener>
T[]getListeners(Class<T> listenerType) 返回当前在此TextComponent上注册为FooListener的所有对象的数组。返回由此文本组件呈现的文本中的选定文本。int获取此文本组件中选定文本的结束位置。int获取此文本组件中选定文本的起始位置。getText()返回由此文本组件呈现的文本。返回在此文本组件上注册的所有文本侦听器的数组。boolean指示此文本组件是否可编辑。protected String返回表示此TextComponent状态的字符串。protected void处理此文本组件上的事件。protected void通过将其分派到任何已注册的TextListener对象,处理在此文本组件上发生的文本事件。void删除TextComponent的对等体。void删除指定的文本事件侦听器,以便它不再从此文本组件接收文本事件。如果l为null,则不会抛出异常,也不会执行任何操作。voidselect(int selectionStart, int selectionEnd) 选择指定起始和结束位置之间的文本。void选择此文本组件中的所有文本。void设置此文本组件的背景颜色。voidsetCaretPosition(int position) 设置文本插入插入符的位置。voidsetEditable(boolean b) 设置确定此文本组件是否可编辑的标志。voidsetSelectionEnd(int selectionEnd) 将此文本组件的选择结束设置为指定位置。voidsetSelectionStart(int selectionStart) 将此文本组件的选择开始设置为指定位置。void将由此文本组件呈现的文本设置为指定的文本。Methods declared in class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, revalidate, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setMixingCutoutShape, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate
-
Field Details
-
textListener
将接收来自此对象的事件的侦听器列表。
-
-
Method Details
-
enableInputMethods
public void enableInputMethods(boolean enable) 启用或禁用此文本组件的输入法支持。如果启用了输入法支持,并且文本组件还处理键事件,则传入的事件将提供给当前输入法,并且只有在输入法不消耗它们时才会由组件处理或分派给其侦听器。文本组件的输入法支持是否以及如何默认启用或禁用取决于实现。- 覆盖:
-
enableInputMethods在类Component中 - 参数:
-
enable- true表示启用,false表示禁用 - 自版本:
- 1.2
- 参见:
-
addNotify
public void addNotify()通过将其连接到本机屏幕资源,使此组件可显示。此方法由工具包内部调用,不应直接由程序调用。 -
removeNotify
public void removeNotify()删除TextComponent的对等体。对等体允许我们修改TextComponent的外观而不更改其功能。- 覆盖:
-
removeNotify在类Component中 - 参见:
-
setText
将由此文本组件呈现的文本设置为指定的文本。- 参数:
-
t- 新文本;如果此参数为null,则文本将设置为空字符串"" - 参见:
-
getText
返回由此文本组件呈现的文本。默认情况下,这是一个空字符串。- 返回:
-
此
TextComponent的值 - 参见:
-
getSelectedText
返回由此文本组件呈现的文本中的选定文本。- 返回:
- 此文本组件的选定文本
- 参见:
-
isEditable
public boolean isEditable()指示此文本组件是否可编辑。- 返回:
-
如果此文本组件可编辑,则返回
true;否则返回false。 - 自版本:
- 1.0
- 参见:
-
setEditable
public void setEditable(boolean b) 设置确定此文本组件是否可编辑的标志。如果标志设置为
true,则此文本组件变为可编辑。如果标志设置为false,用户无法更改此文本组件的文本。默认情况下,不可编辑的文本组件具有SystemColor.control的背景颜色。可以通过调用setBackground来覆盖此默认值。- 参数:
-
b- 指示此文本组件是否可编辑的标志 - 自版本:
- 1.0
- 参见:
-
getBackground
获取此文本组件的背景颜色。默认情况下,不可编辑的文本组件具有SystemColor.control的背景颜色。可以通过调用setBackground来覆盖此默认值。- 覆盖:
-
getBackground在类Component中 - 返回:
- 此文本组件的背景颜色。如果此文本组件没有背景颜色,则返回其父级的背景颜色。
- 自版本:
- 1.0
- 参见:
-
setBackground
设置此文本组件的背景颜色。- 覆盖:
-
setBackground在类Component中 - 参数:
-
c- 要成为此文本组件颜色的颜色。如果此参数为null,则此文本组件将继承其父级的背景颜色。 - 自版本:
- 1.0
- 参见:
-
getSelectionStart
public int getSelectionStart()获取此文本组件中选定文本的起始位置。- 返回:
- 选定文本的起始位置
- 参见:
-
setSelectionStart
public void setSelectionStart(int selectionStart) 设置此文本组件的选择起始位置为指定位置。新的起始点受限于当前选择结束点或之前。它也不能设置为小于零,即组件文本的开头。如果调用者提供了超出范围的selectionStart值,则该方法会默默地强制执行这些约束,而不会失败。- 参数:
-
selectionStart- 所选文本的起始位置 - 自从:
- 1.1
- 另请参见:
-
getSelectionEnd
public int getSelectionEnd()获取此文本组件中所选文本的结束位置。- 返回:
- 所选文本的结束位置
- 另请参见:
-
setSelectionEnd
public void setSelectionEnd(int selectionEnd) 将此文本组件的选择结束位置设置为指定位置。新的结束点受限于当前选择起始点或之后。它也不能设置超出组件文本的末尾。如果调用者提供了超出范围的selectionEnd值,则该方法会默默地强制执行这些约束,而不会失败。- 参数:
-
selectionEnd- 所选文本的结束位置 - 自从:
- 1.1
- 另请参见:
-
select
public void select(int selectionStart, int selectionEnd) 选择指定起始和结束位置之间的文本。此方法设置所选文本的起始和结束位置,并强制执行限制条件,即起始位置必须大于或等于零。结束位置必须大于或等于起始位置,并且小于或等于文本组件文本的长度。字符位置从零开始索引。所选内容的长度为
endPosition-startPosition,因此不选择endPosition处的字符。如果所选文本的起始和结束位置相等,则取消选择所有文本。如果调用者提供的值不一致或超出范围,则该方法会默默地强制执行这些约束,而不会失败。具体来说,如果起始位置或结束位置大于文本长度,则将其重置为等于文本长度。如果起始位置小于零,则将其重置为零,如果结束位置小于起始位置,则将其重置为起始位置。
- 参数:
-
selectionStart- 要选择的第一个字符(char值)的从零开始的索引 -
selectionEnd- 要选择的文本的从零开始的结束位置;selectionEnd处的字符(char值)不会被选择 - 另请参见:
-
selectAll
public void selectAll()选择此文本组件中的所有文本。- 另请参见:
-
setCaretPosition
public void setCaretPosition(int position) 设置文本插入符的位置。插入符位置受限于0和文本的最后一个字符(包括在内)。如果传入的值大于此范围,则该值设置为最后一个字符(如果TextComponent不包含文本,则设置为0),并且不返回错误。如果传入的值小于0,则会抛出IllegalArgumentException。- 参数:
-
position- 文本插入符的位置 - 抛出:
-
IllegalArgumentException- 如果position小于零 - 自从:
- 1.1
-
getCaretPosition
public int getCaretPosition()返回文本插入符的位置。插入符位置受限于0和文本的最后一个字符(包括在内)。如果文本或插入符尚未设置,则默认插入符位置为0。- 返回:
- 文本插入符的位置
- 自从:
- 1.1
- 另请参见:
-
addTextListener
- 参数:
-
l- 文本事件侦听器 - 另请参见:
-
removeTextListener
- 参数:
-
l- 文本侦听器 - 自从:
- 1.1
- 另请参见:
-
getTextListeners
返回在此文本组件上注册的所有文本侦听器的数组。- 返回:
-
所有此文本组件的
TextListener或如果当前未注册任何文本侦听器,则返回空数组 - 自从:
- 1.4
- 另请参见:
-
getListeners
返回当前在此TextComponent上注册为FooListener的所有对象的数组。使用addFooListener方法注册FooListener。您可以使用类字面量(例如
FooListener.class)指定listenerType参数。例如,您可以使用以下代码查询TextComponent t的文本侦听器:TextListener[] tls = (TextListener[])(t.getListeners(TextListener.class));
如果不存在此类侦听器,则此方法返回一个空数组。- 覆盖:
-
getListeners在类Component中 - 类型参数:
-
T- 侦听器的类型 - 参数:
-
listenerType- 请求的侦听器类型;此参数应指定从java.util.EventListener继承的接口 - 返回:
-
在此文本组件上注册为
FooListener的所有对象的数组,如果尚未添加此类侦听器,则返回一个空数组 - 抛出:
-
ClassCastException- 如果listenerType未指定实现java.util.EventListener的类或接口 - 自从:
- 1.3
- 另请参见:
-
processEvent
处理此文本组件上的事件。如果事件是TextEvent,则调用processTextEvent方法,否则调用其超类的processEvent。请注意,如果事件参数为
null,则行为是未指定的,可能会导致异常。- 覆盖:
-
processEvent在类Component中 - 参数:
-
e- 事件 - 另请参见:
-
processTextEvent
处理在此文本组件上发生的文本事件,将其分派给任何已注册的TextListener对象。注意:除非为此组件启用了文本事件,否则不会调用此方法。当发生以下情况之一时会发生这种情况:
- 通过
addTextListener注册了一个TextListener对象 - 通过
enableEvents启用了文本事件
请注意,如果事件参数为
null,则行为是未指定的,可能会导致异常。- 参数:
-
e- 文本事件 - 参见:
- 通过
-
paramString
返回表示此TextComponent状态的字符串。此方法仅用于调试目的,返回的字符串的内容和格式可能因实现而异。返回的字符串可能为空,但不能为null。- 覆盖:
-
paramString在类Component - 返回:
- 此文本组件的参数字符串
-
getAccessibleContext
获取与此TextComponent关联的AccessibleContext。对于文本组件,AccessibleContext采用AccessibleAWTTextComponent的形式。如有必要,将创建一个新的AccessibleAWTTextComponent实例。- 指定者:
-
getAccessibleContext在接口Accessible - 覆盖:
-
getAccessibleContext在类Component - 返回:
- 作为此TextComponent的AccessibleContext的AccessibleAWTTextComponent
- 自版本:
- 1.3
-