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);
每当用户在文本字段中键入一个键时,一个或多个键事件将发送到文本字段。 A KeyEvent可以是以下三种类型之一:keyPressed,keyReleased或keyTyped。 密钥事件的属性指示它们中的哪一种,以及关于事件的附加信息,例如什么修饰符应用于密钥事件和事件发生的时间。
关键事件传递给使用该组件的addKeyListener方法注册接收此类事件的每个KeyListener或KeyAdapter对象。 ( KeyAdapter对象实现KeyListener界面。)
也可以发射ActionEvent 。 如果为文本字段启用了操作事件,可能会按Return键触发。
TextField类的processEvent方法检查动作事件并将其传递给processActionEvent 。 后一种方法将事件ActionListener到已注册接收此文本字段生成的操作事件的任何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()
获取与此TextField关联的AccessibleContext。
|
ActionListener[] |
getActionListeners()
返回在此文本域中注册的所有操作侦听器的数组。
|
int |
getColumns()
获取此文本字段中的列数。
|
char |
getEchoChar()
获取要用于回显的字符。
|
<T extends EventListener> |
getListeners(类<T> listenerType)
返回当前注册的所有对象的数组
FooListener在这个S
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平台实现可能只支持有限的非空的echo字符集。 此函数返回最初通过setEchoChar()请求的回显字符。 TextField实现中实际使用的回音字符可能不同。
echoCharIsSet() ,
setEchoChar(char)
public void setEchoChar(char c)
回声字符对于用户输入不应该回显到屏幕的文本字段是有用的,如在输入密码的文本字段的情况下。 设置echoChar = 0允许用户输入再次回到屏幕。
Java平台实现可能只支持有限的非空的echo字符集。 尝试设置不支持的回音字符将导致使用默认回音字符。 对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)
l - 动作侦听器。
removeActionListener(java.awt.event.ActionListener) , getActionListeners() , ActionListener
public void removeActionListener(ActionListener l)
l - 动作监听器。
addActionListener(java.awt.event.ActionListener) , getActionListeners() , ActionListener
public ActionListener[] getActionListeners()
ActionListener s或一个空数组,如果没有动作侦听器当前注册
addActionListener(java.awt.event.ActionListener) , removeActionListener(java.awt.event.ActionListener) , ActionListener
public <T extends EventListener> T[] getListeners(类<T> listenerType)
FooListener的所有对象的数组,在此TextField 。
FooListener使用addFooListener方法注册。
您可以使用类文字指定listenerType参数,如FooListener.class 。 例如,您可以使用以下代码查询TextField t其操作侦听器:
ActionListener[] als = (ActionListener[])(t.getListeners(ActionListener.class));
如果没有这样的侦听器存在,这个方法返回一个空数组。
getListeners在
TextComponent
listenerType - 所请求的听众的类型;
此参数应指定从java.util.EventListener下降的java.util.EventListener
FooListener的所有对象的数组,如果没有添加此类侦听器,则为空数组
ClassCastException - 如果
listenerType没有指定实现java.util.EventListener的类或
java.util.EventListener
getActionListeners()
protected void processEvent(AWTEvent e)
ActionEvent一个实例,它调用processActionEvent方法。
否则,它将在processEvent上调用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.