- java.lang.Object
-
- java.awt.Component
-
- java.awt.TextComponent
-
- All Implemented Interfaces:
-
ImageObserver,MenuContainer,Serializable,Accessible
public class TextComponent extends Component implements Accessible
TextComponent类是允许编辑某些文本的任何组件的超类。文本组件体现了一串文本。
TextComponent类定义了一组确定此文本是否可编辑的方法。 如果组件是可编辑的,它定义了另一组支持文本插入插入符号的方法。此外,该类定义了用于从文本维护当前选择的方法。 文本选择,组件文本的子字符串,是编辑操作的目标。 它也被称为所选文本 。
- 从以下版本开始:
- 1.0
- 另请参见:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class 描述 protected classTextComponent.AccessibleAWTTextComponent该类实现TextComponent类的可访问性支持。-
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
-
Field Summary
Fields Modifier and Type Field 描述 protected TextListenertextListener将从此对象接收事件的侦听器列表。-
Fields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
-
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
-
方法摘要
所有方法 接口方法 具体的方法 Modifier and Type 方法 描述 voidaddNotify()通过将此组件连接到本机屏幕资源来使此组件可以显示。voidaddTextListener(TextListener l)添加指定的文本事件侦听器以从此文本组件接收文本事件。voidenableInputMethods(boolean enable)启用或禁用此文本组件的输入法支持。AccessibleContextgetAccessibleContext()获取与此TextComponent关联的AccessibleContext。ColorgetBackground()获取此文本组件的背景颜色。intgetCaretPosition()返回文本插入插入符的位置。InputMethodRequestsgetInputMethodRequests()获取支持该组件输入法的请求的输入法请求处理程序。<T extends EventListener>
T[]getListeners(Class<T> listenerType)返回此TextComponent当前注册为FooListener的所有对象的数组。StringgetSelectedText()从由此文本组件呈现的文本中返回所选文本。intgetSelectionEnd()获取此文本组件中所选文本的最终位置。intgetSelectionStart()获取此文本组件中所选文本的起始位置。StringgetText()返回此文本组件显示的文本。TextListener[]getTextListeners()返回在此文本组件上注册的所有文本侦听器的数组。booleanisEditable()指示此文本组件是否可编辑。protected StringparamString()返回表示TextComponent状态的字符串。protected voidprocessEvent(AWTEvent e)处理此文本组件上的事件。protected voidprocessTextEvent(TextEvent e)通过将文本事件发送到任何已注册的TextListener对象来处理在此文本组件上发生的文本事件。voidremoveNotify()删除TextComponent的同伴。voidremoveTextListener(TextListener l)删除指定的文本事件侦听器,使其不再从此文本组件接收文本事件。如果l为null,则不会抛出任何异常,也不会执行任何操作。voidselect(int selectionStart, int selectionEnd)选择指定的开始和结束位置之间的文本。voidselectAll()选择此文本组件中的所有文本。voidsetBackground(Color c)设置此文本组件的背景颜色。voidsetCaretPosition(int position)设置文本插入插入符的位置。voidsetEditable(boolean b)设置确定此文本组件是否可编辑的标志。voidsetSelectionEnd(int selectionEnd)将此文本组件的选择结束设置为指定位置。voidsetSelectionStart(int selectionStart)将此文本组件的选择开始设置为指定位置。voidsetText(String t)将此文本组件呈现的文本设置为指定的文本。-
Methods inherited from 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, 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
-
-
-
-
字段详细信息
-
textListener
protected transient TextListener textListener
将从此对象接收事件的侦听器列表。
-
-
方法详细信息
-
enableInputMethods
public void enableInputMethods(boolean enable)
启用或禁用此文本组件的输入法支持。 如果启用了输入法支持,并且文本组件也处理关键事件,则传入事件将提供给当前输入方法,并且只有由组件处理或者如果输入方法不使用它们则调度到其侦听器。 默认情况下,启用或禁用此文本组件的输入方法和输入方式是否依赖于实现。- 重写:
-
enableInputMethods在Component - 参数
-
enable- true启用,false禁用 - 从以下版本开始:
- 1.2
- 另请参见:
-
Component.processKeyEvent(java.awt.event.KeyEvent)
-
getInputMethodRequests
public InputMethodRequests getInputMethodRequests()
描述从类别复制:Component获取支持该组件输入法的请求的输入法请求处理程序。 支持现场文本输入的组件必须覆盖此方法以返回InputMethodRequests实例。 同时,它也必须处理输入法事件。- 重写:
-
getInputMethodRequests在Component - 结果
-
该组件的输入法请求处理程序默认为
null - 另请参见:
-
Component.addInputMethodListener(java.awt.event.InputMethodListener)
-
addNotify
public void addNotify()
通过将此组件连接到本机屏幕资源来使此组件可以显示。 该方法由工具包内部调用,不应由程序直接调用。- 重写:
-
addNotify在类Component - 另请参见:
-
removeNotify()
-
removeNotify
public void removeNotify()
删除TextComponent的同行。 对等体允许我们修改TextComponent的外观而不改变它的功能。
-
setText
public void setText(String t)
将此文本组件呈现的文本设置为指定的文本。- 参数
-
t- 新文本; 如果此参数为null则文本设置为空字符串“” - 另请参见:
-
getText()
-
getText
public String getText()
返回此文本组件显示的文本。 默认情况下,这是一个空字符串。- 结果
-
这个价值
TextComponent - 另请参见:
-
setText(java.lang.String)
-
getSelectedText
public String getSelectedText()
从由此文本组件呈现的文本中返回所选文本。- 结果
- 该文本组件的选定文本
- 另请参见:
-
select(int, int)
-
isEditable
public boolean isEditable()
指示此文本组件是否可编辑。- 结果
-
true如果这个文本组件是可编辑的;false否则。 - 从以下版本开始:
- 1.0
- 另请参见:
-
setEditable(boolean)
-
setEditable
public void setEditable(boolean b)
设置确定此文本组件是否可编辑的标志。如果标志设置为
true,则该文本组件将成为用户可编辑的。 如果标志设置为false,则用户无法更改此文本组件的文本。 默认情况下,不可编辑的文本组件具有SystemColor.control的背景颜色。 这个默认值可以通过调用setBackground来重写。- 参数
-
b- 指示此文本组件是否可用户可编辑的标志。 - 从以下版本开始:
- 1.0
- 另请参见:
-
isEditable()
-
getBackground
public Color getBackground()
获取此文本组件的背景颜色。 默认情况下,不可编辑的文本组件具有SystemColor.control的背景颜色。 这个默认值可以通过调用setBackground来重写。- 重写:
-
getBackground在Component - 结果
- 这个文本组件的背景颜色。 如果此文本组件没有背景颜色,则返回其父项的背景颜色。
- 从以下版本开始:
- 1.0
- 另请参见:
-
setBackground(Color)
-
setBackground
public void setBackground(Color c)
设置此文本组件的背景颜色。- 重写:
-
setBackground在Component - 参数
-
c- 成为此文本组件颜色的颜色。 如果此参数为null,则此文本组件将继承其父级的背景颜色。 - 从以下版本开始:
- 1.0
- 另请参见:
-
getBackground()
-
getSelectionStart
public int getSelectionStart()
获取此文本组件中所选文本的起始位置。- 结果
- 所选文本的起始位置
- 另请参见:
-
setSelectionStart(int),getSelectionEnd()
-
setSelectionStart
public void setSelectionStart(int selectionStart)
将此文本组件的选择开始设置为指定位置。 新的起点被限制在当前选择结束之前或之前。 它也不能设置为小于零,组件文本的开头。 如果调用者提供超出范围的值为selectionStart的值,该方法将静默地强制执行这些约束,并且不会失败。- 参数
-
selectionStart- 所选文本的开始位置 - 从以下版本开始:
- 1.1
- 另请参见:
-
getSelectionStart(),setSelectionEnd(int)
-
getSelectionEnd
public int getSelectionEnd()
获取此文本组件中所选文本的最终位置。- 结果
- 所选文本的结束位置
- 另请参见:
-
setSelectionEnd(int),getSelectionStart()
-
setSelectionEnd
public void setSelectionEnd(int selectionEnd)
将此文本组件的选择结束设置为指定位置。 新的终点被限制在当前选择开始之后或之后。 它也不能超出组件文本的结尾。 如果调用者提供超出范围的值为selectionEnd,则该方法将静默地强制执行这些约束,并且不会失败。- 参数
-
selectionEnd- 所选文本的结束位置 - 从以下版本开始:
- 1.1
- 另请参见:
-
getSelectionEnd(),setSelectionStart(int)
-
select
public void select(int selectionStart, int selectionEnd)选择指定的开始和结束位置之间的文本。该方法设置所选文本的开始和结束位置,强制开始位置必须大于或等于零的限制。 结束位置必须大于或等于起始位置,小于或等于文本组件文本的长度。 字符位置从零开始索引。 选择的长度为
endPosition-startPosition,因此未选择endPosition处的字符。 如果所选文本的开始和结束位置相等,则取消选择所有文本。如果调用者提供不一致或超出范围的值,则该方法将静默地强制执行这些约束,并且不会失败。 具体来说,如果起始位置或结束位置大于文本的长度,则将其重置为等于文本长度。 如果起始位置小于零,则将其复位为零,如果最终位置小于起始位置,则将其复位到起始位置。
- 参数
-
selectionStart- 要选择的第一个字符的零selectionStart引(char值) -
selectionEnd- 要选择的文本的从零开始的最终位置; 字符(char在值)selectionEnd未选中 - 另请参见:
-
setSelectionStart(int),setSelectionEnd(int),selectAll()
-
selectAll
public void selectAll()
选择此文本组件中的所有文本。- 另请参见:
-
select(int, int)
-
setCaretPosition
public void setCaretPosition(int position)
设置文本插入插入符的位置。 插入位置被限制在0和文本的最后一个字符之间(包括)。 如果传入值大于此范围,则该值将设置为最后一个字符(如果TextComponent包含文本,TextComponent0),并且不返回错误。 如果传入值小于0,则抛出IllegalArgumentException。- 参数
-
position- 文本插入插入符的位置 - 异常
-
IllegalArgumentException- 如果position小于零 - 从以下版本开始:
- 1.1
-
getCaretPosition
public int getCaretPosition()
返回文本插入插入符的位置。 插入位置被限制在0和文本的最后一个字符之间(包括)。 如果文本或插入符号未设置,默认插入位置为0。- 结果
- 文本插入插入符的位置
- 从以下版本开始:
- 1.1
- 另请参见:
-
setCaretPosition(int)
-
addTextListener
public void addTextListener(TextListener l)
- 参数
-
l- 文本事件侦听器 - 另请参见:
-
removeTextListener(java.awt.event.TextListener),getTextListeners(),TextListener
-
removeTextListener
public void removeTextListener(TextListener l)
删除指定的文本事件侦听器,使其不再从此文本组件接收文本事件。如果l为null,则不会抛出任何异常,也不会执行任何操作。有关AWT线程模型的详细信息,请参阅AWT Threading Issues 。
- 参数
-
l- 文本侦听器 - 从以下版本开始:
- 1.1
- 另请参见:
-
addTextListener(java.awt.event.TextListener),getTextListeners(),TextListener
-
getTextListeners
public TextListener[] getTextListeners()
返回在此文本组件上注册的所有文本侦听器的数组。- 结果
-
所有这些文本组件的
TextListeners或一个空数组,如果没有文本侦听器当前注册 - 从以下版本开始:
- 1.4
- 另请参见:
-
addTextListener(java.awt.event.TextListener),removeTextListener(java.awt.event.TextListener)
-
getListeners
public <T extends EventListener> T[] getListeners(Class<T> listenerType)
返回此TextComponent当前注册为FooListener的所有对象的数组。FooListeners是使用addFooListener方法注册的。您可以使用类文字指定
listenerType参数,如FooListener.class。 例如,您可以使用以下代码查询TextComponent t的文本侦听器:TextListener[] tls = (TextListener[])(t.getListeners(TextListener.class));如果没有这样的侦听器存在,这个方法返回一个空数组。- 重写:
-
getListeners在Component - 参数类型
-
T- 听众的类型 - 参数
-
listenerType- 所请求的听众的类型; 此参数应指定从java.util.EventListener下降的接口 - 结果
-
在此文本组件上注册为
FooListener的所有对象的数组,如果未添加此类侦听器,则为空数组 - 异常
-
ClassCastException- 如果listenerType没有指定实现java.util.EventListener的类或接口 - 从以下版本开始:
- 1.3
- 另请参见:
-
getTextListeners()
-
processEvent
protected void processEvent(AWTEvent e)
处理此文本组件上的事件。 如果事件是一个TextEvent,它将调用processTextEvent方法,否则它调用其超类的processEvent。请注意,如果事件参数为
null该行为是未指定的,可能会导致异常。- 重写:
-
processEvent在Component - 参数
-
e- 事件 - 另请参见:
-
Component.processComponentEvent(java.awt.event.ComponentEvent),Component.processFocusEvent(java.awt.event.FocusEvent),Component.processKeyEvent(java.awt.event.KeyEvent),Component.processMouseEvent(java.awt.event.MouseEvent),Component.processMouseMotionEvent(java.awt.event.MouseEvent),Component.processInputMethodEvent(java.awt.event.InputMethodEvent),Component.processHierarchyEvent(java.awt.event.HierarchyEvent),Component.processMouseWheelEvent(java.awt.event.MouseWheelEvent)
-
processTextEvent
protected void processTextEvent(TextEvent e)
通过将文本事件发送到任何已注册的TextListener对象来处理此文本组件上发生的文本事件。注意:除非为此组件启用文本事件,否则不会调用此方法。 发生以下情况之一时:
- A
TextListener对象通过addTextListener注册 - 文本事件通过
enableEvents启用
请注意,如果事件参数为
null则行为未指定,可能会导致异常。- 参数
-
e- 文本事件 - 另请参见:
-
Component.enableEvents(long)
- A
-
paramString
protected String paramString()
返回表示TextComponent状态的字符串。 该方法仅用于调试目的,并且返回的字符串的内容和格式可能因实现而异。 返回的字符串可能为空,但可能不是null。- 重写:
-
paramString在Component - 结果
- 此文本组件的参数字符串
-
getAccessibleContext
public AccessibleContext getAccessibleContext()
获取与此TextComponent关联的AccessibleContext。 对于文本组件,AccessibleContext采用AccessibleAWTTextComponent的形式。 如有必要,将创建一个新的AccessibleAWTTextComponent实例。- Specified by:
-
getAccessibleContext在接口Accessible - 重写:
-
getAccessibleContext在Component - 结果
- 作为此TextComponent的AccessibleContext的AccessibleAWTTextComponent
- 从以下版本开始:
- 1.3
-
-