public class TextField extends TextComponent
TextField对象是一个文本组件,允许一个单行文本的编辑。
例如,下面的图像描述了一个有四个不同宽度的文本字段的框架。这两个文本字段显示预定义的文本"Hello"。

这里是产生这四个文本字段的代码:
TextField tf1, tf2, tf3, tf4;
// a blank text field
tf1 = new TextField();
// blank field of 20 columns
tf2 = new TextField("", 20);
// predefined text displayed
tf3 = new TextField("Hello!");
// predefined text in 30 columns
tf4 = new TextField("Hello", 30);
每当用户在文本字段中键入一个键时,一个或多个键事件被发送到文本字段。一个KeyEvent可能是三种类型之一:keyPressed,keyreleased,或keytyped。一个键事件的属性表明,这些类型的属性,以及事件的附加信息,如什么改性应用于关键事件和事件发生的时间。
关键事件传递给每一KeyListener或KeyAdapter对象注册到使用组件的addKeyListener方法接收这样的事件。(KeyAdapter对象实现了KeyListener接口。)
它也有可能火ActionEvent。如果动作事件的文本域启用,可按Return键发射。
的TextField类的processEvent方法考察了行为事件并传递给processActionEvent。后者的方法将事件已注册接收动作事件的文本字段生成的任何ActionListener对象。
| Modifier and Type | Class and Description |
|---|---|
protected class |
TextField.AccessibleAWTTextField
这个类实现了对
TextField类可访问性支持。
|
TextComponent.AccessibleAWTTextComponentComponent.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategytextListeneraccessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH| Constructor and Description |
|---|
TextField()
构建一个新的文本字段。
|
TextField(int columns)
用指定列数构造一个新的空文本字段。
|
TextField(String text)
用指定的文本构造一个新的文本字段。
|
TextField(String text, int columns)
构造一个与指定要显示的指定文本初始化的新文本字段,并足够大以保持指定的列数。
|
| Modifier and Type | Method and Description |
|---|---|
void |
addActionListener(ActionListener l)
添加指定的操作侦听器从该文本字段接收动作事件。
|
void |
addNotify()
创建TextField的同伴。
|
boolean |
echoCharIsSet()
指示此文本字段是否具有一个用于呼应的字符集。
|
AccessibleContext |
getAccessibleContext()
获取与此相关的accessiblecontext TextField。
|
ActionListener[] |
getActionListeners()
返回所有的动作监听器注册这个TextField的数组。
|
int |
getColumns()
获取此文本字段中的列数。
|
char |
getEchoChar()
获取用于呼应的字符。
|
<T extends EventListener> |
getListeners(类<T> listenerType)
返回对象的当前注册为
FooListeners在这
TextField数组。
|
Dimension |
getMinimumSize()
获取此文本字段的最小尺寸。
|
Dimension |
getMinimumSize(int columns)
获取具有指定列数的文本字段的最小尺寸。
|
Dimension |
getPreferredSize()
获取此文本字段的首选大小。
|
Dimension |
getPreferredSize(int columns)
获取此文本字段的首选大小与指定的列数。
|
Dimension |
minimumSize()
过时的。
作为JDK 1.1版本,取而代之的
getMinimumSize()。
|
Dimension |
minimumSize(int columns)
过时的。
作为JDK 1.1版本,取而代之的
getMinimumSize(int)。
|
protected String |
paramString()
返回一个字符串,表示此
TextField状态。
|
Dimension |
preferredSize()
过时的。
作为JDK 1.1版本,取而代之的
getPreferredSize()。
|
Dimension |
preferredSize(int columns)
过时的。
作为JDK 1.1版本,取而代之的
getPreferredSize(int)。
|
protected void |
processActionEvent(ActionEvent e)
过程行动事件发生在这个文本字段的分派他们到任何注册
ActionListener对象。
|
protected void |
processEvent(AWTEvent e)
在这个文本字段上处理事件。
|
void |
removeActionListener(ActionListener l)
移除指定的动作侦听器,以便它不再接收来自该文本字段的动作事件。
|
void |
setColumns(int columns)
在该文本字段中设置列的数目。
|
void |
setEchoChar(char c)
设置此文本字段的“回声”字符。
|
void |
setEchoCharacter(char c)
过时的。
作为JDK 1.1版本,取而代之的
setEchoChar(char)。
|
void |
setText(String t)
将由该文本组件呈现的文本设置为指定的文本。
|
addTextListener, enableInputMethods, getBackground, getCaretPosition, getInputMethodRequests, getSelectedText, getSelectionEnd, getSelectionStart, getText, getTextListeners, isEditable, processTextEvent, removeNotify, removeTextListener, select, selectAll, setBackground, setCaretPosition, setEditable, setSelectionEnd, setSelectionStartaction, 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, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, 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, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, postEvent, 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, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, revalidate, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validatepublic TextField()
throws HeadlessException
HeadlessException -如果graphicsenvironment isheadless()返回true。
GraphicsEnvironment.isHeadless()
public TextField(String text) throws HeadlessException
text -要显示的文本。如果
text是
null,空字符串将显示
""。
HeadlessException -如果graphicsenvironment isheadless()返回true。
GraphicsEnvironment.isHeadless()
public TextField(int columns)
throws HeadlessException
columns -列数。如果
columns小于
0,
columns设置
0。
HeadlessException -如果graphicsenvironment isheadless()返回true。
GraphicsEnvironment.isHeadless()
public TextField(String text, int columns) throws HeadlessException
text -要显示的文本。如果
text是
null,空字符串将显示
""。
columns -列数。如果
columns小于
0,
columns设置
0。
HeadlessException -如果graphicsenvironment isheadless()返回true。
GraphicsEnvironment.isHeadless()
public void addNotify()
addNotify 方法重写,继承类
TextComponent
TextComponent.removeNotify()
public char getEchoChar()
一个回波字符是有用的文本字段,用户输入不应该响应屏幕,如在一个文本字段输入密码的情况下。如果echoChar = 0,用户输入在屏幕不变。
一个java平台的实现可能只支持有限,回波特征非空集合。这个函数返回的回波特征最初要求通过setechochar()。实际使用的TextField实现回波特征可能不同。
echoCharIsSet(),
setEchoChar(char)
public void setEchoChar(char c)
一个回波字符是有用的文本字段,用户输入不应该响应屏幕,如在一个文本字段输入密码的情况下。设置echoChar = 0允许用户输入要呼应到屏幕再次。
一个java平台的实现可能只支持有限,回波特征非空集合。试图设置一个不支持的回声字符将导致默认的回声字符,而不是使用。随后调用getechochar()将返回的回波特征的最初要求。这可能是也可能不是真正的TextField实现回波特征相同。
c -这个文本字段的回波特征。
echoCharIsSet(),
getEchoChar()
@Deprecated public void setEchoCharacter(char c)
setEchoChar(char)。
public void setText(String t)
setText 方法重写,继承类
TextComponent
t -新文本。
TextComponent.getText()
public boolean echoCharIsSet()
一个回波字符是有用的文本字段,用户输入不应该响应屏幕,如在一个文本字段输入密码的情况下。
true如果此文本字段有一个字符集
false否则呼应。
setEchoChar(char),
getEchoChar()
public int getColumns()
setColumns(int)
public void setColumns(int columns)
columns -列数。
IllegalArgumentException如果提供
columns值小于
0。
getColumns()
public Dimension getPreferredSize(int columns)
columns -这个文本字段的列数。
@Deprecated public Dimension preferredSize(int columns)
getPreferredSize(int)。
public Dimension getPreferredSize()
getPreferredSize 方法重写,继承类
Component
Component.getMinimumSize(),
LayoutManager
@Deprecated public Dimension preferredSize()
getPreferredSize()。
preferredSize 方法重写,继承类
Component
public Dimension getMinimumSize(int columns)
columns -这个文本字段的列数。
@Deprecated public Dimension minimumSize(int columns)
getMinimumSize(int)。
public Dimension getMinimumSize()
getMinimumSize 方法重写,继承类
Component
Component.getPreferredSize(),
LayoutManager
@Deprecated public Dimension minimumSize()
getMinimumSize()。
minimumSize 方法重写,继承类
Component
public void addActionListener(ActionListener l)
参考AWT Threading Issues对AWT的线程模型的细节。
l的监听行动。
removeActionListener(java.awt.event.ActionListener),
getActionListeners(),
ActionListener
public void removeActionListener(ActionListener l)
参考AWT Threading Issues对AWT的线程模型的细节。
l的监听行动。
addActionListener(java.awt.event.ActionListener),
getActionListeners(),
ActionListener
public ActionListener[] getActionListeners()
ActionListeners或空数组如果没有行动的听众正在注册
addActionListener(java.awt.event.ActionListener),
removeActionListener(java.awt.event.ActionListener),
ActionListener
public <T extends EventListener> T[] getListeners(类<T> listenerType)
FooListeners在这
TextField数组。
FooListeners使用
addFooListener方法注册。如果没有这样
您可以指定一个类的listenerType字面的说法,如FooListener.class。例如,你可以查询其监听行动用下面的代码TextField t:
ALS功能[ ] =(action [ ])(t.getlisteners(action。类));听众的存在,此方法将返回空数组。
getListeners 方法重写,继承类
TextComponent
listenerType -听众的类型要求;这个参数应该指定一个接口,从
java.util.EventListener
FooListeners数组,或一个空数组如果没有这样的听众已添加
ClassCastException -如果
listenerType不指定一个类或接口实现
java.util.EventListener
getActionListeners()
protected void processEvent(AWTEvent e)
ActionEvent实例,它调用
processActionEvent方法。否则,它调用父类的
processEvent上。
注意,如果参数是null行为是不可能导致异常。
processEvent 方法重写,继承类
TextComponent
e -事件
ActionEvent,
processActionEvent(java.awt.event.ActionEvent)
protected void processActionEvent(ActionEvent e)
ActionListener对象。
此方法不被调用,除非此组件启用了动作事件。当下列情况之一发生时,启用了动作事件:
ActionListener对象是通过addActionListener注册。enableEvents。注意,如果参数是null行为是不可能导致异常。
e -动作事件
ActionListener,
addActionListener(java.awt.event.ActionListener),
Component.enableEvents(long)
protected String paramString()
TextField状态。这种方法的目的是用于调试目的,其含量和返回的字符串格式不同的实现可能会有所不同。返回的字符串是空的但可能不
null。
paramString 方法重写,继承类
TextComponent
public AccessibleContext getAccessibleContext()
getAccessibleContext 接口
Accessible
getAccessibleContext 方法重写,继承类
TextComponent
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.