public class JTextField extends JTextComponent implements SwingConstants
JTextField
是轻量级的,允许编辑单行文本。信息和实例使用的文本字段,在看到
How to Use Text Fields java教程。
JTextField
是源java.awt.TextField
,这样做是合理的兼容。该组件已在java.awt.TextField
类没有发现能力。父类应该对额外功能咨询。
JTextField
具有建立作为被解雇的诉讼事件的命令字符串的字符串的方法。的java.awt.TextField
应用领域的文本为ActionEvent
命令字符串。JTextField
如果不null
使用方法与setActionCommand
命令字符串,否则将使用领域的文本作为一个兼容java.awt.TextField
。
和getEchoChar
方法setEchoChar
并不直接提供避免新的可拔插实现感觉无意间暴露的密码字符。提供服务密码像一个单独的类JPasswordField
延伸JTextField
与独立可插拔的外观和感觉提供这种服务。
的java.awt.TextField
可以监测变化通过增加基础元件的JTextComponent
为TextEvent
的TextListener
,改变广播模型通过对DocumentListeners
DocumentEvent
。的DocumentEvent
给出和变化的一种变化的位置,如果需要的话。代码片段可能看起来像是:
DocumentListener myListener = ??;
JTextField myArea = ??;
myArea.getDocument().addDocumentListener(myListener);
对JTextField
水平对齐方式可以设置为左对齐,居中,右对齐,领先的道理,或尾随的理由。如果字段文本的所需大小小于分配给它的大小,则右/后对齐是有用的。这是由setHorizontalAlignment
和getHorizontalAlignment
方法确定。默认是领先的有道理的。
如何在文本字段中消耗vk_enter事件取决于文本字段有任何行动的听众。如果是这样的话,在听众中获得一个ActionEvent然后vk_enter结果,和vk_enter事件消耗。这是如何处理vk_enter事件兼容AWT文本字段。如果文本字段没有行动的听众,然后为V 1.3的vk_enter事件不消耗。相反,祖先组件绑定处理,使JFC/Swing默认按钮功能的工作。
自定义字段可以很容易地创建通过扩展模型和改变提供的默认模型。例如,下面的代码将创建一个只保留大写字符的字段。它将工作,即使文本粘贴到剪贴板或它是通过改变方案的变化。
public class UpperCaseField extends JTextField {
public UpperCaseField(int cols) {
super(cols);
}
protected Document createDefaultModel() {
return new UpperCaseDocument();
}
static class UpperCaseDocument extends PlainDocument {
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if (str == null) {
return;
}
char[] upper = str.toCharArray();
for (int i = 0; i < upper.length; i++) {
upper[i] = Character.toUpperCase(upper[i]);
}
super.insertString(offs, new String(upper), a);
}
}
}
警告: Swing是线程不安全的。更多信息见Swing's Threading Policy。
警告:序列化该类的对象与以后的Swing版本不兼容。当前的序列化支持适用于短期贮藏或RMI运行相同Swing版本的应用程序之间。为1.4,为所有JavaBeans™长期存储的支持已被添加到java.beans
包。请看XMLEncoder
。
Modifier and Type | Class and Description |
---|---|
protected class |
JTextField.AccessibleJTextField
这个类实现了对
JTextField 类可访问性支持。
|
JTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, JTextComponent.KeyBinding
JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
Modifier and Type | Field and Description |
---|---|
static String |
notifyAction
发送通知字段的内容已被接受的操作的名称。
|
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
JTextField()
构建了一种新的
TextField 。
|
JTextField(Document doc, String text, int columns)
构建了一种新的
JTextField 使用给定的文本存储模型和特定的列数。
|
JTextField(int columns)
构建了一个新的空
TextField 与指定的列数。
|
JTextField(String text)
构建了一种新的
TextField 具有指定文本初始化。
|
JTextField(String text, int columns)
构建了一种新的
TextField 具有指定文本和列初始化。
|
Modifier and Type | Method and Description |
---|---|
protected void |
actionPropertyChanged(Action action, String propertyName)
更新的状态响应性能变化相关的动作。
|
void |
addActionListener(ActionListener l)
添加指定的监听行动从文本接受的动作事件。
|
protected void |
configurePropertiesFromAction(Action a)
在这个文本框的属性设置为与指定的
Action 。
|
protected PropertyChangeListener |
createActionPropertyChangeListener(Action a)
创建并返回一个
PropertyChangeListener 负责侦听来自指定的
Action 变化和更新相应的属性。
|
protected Document |
createDefaultModel()
创建的模型的默认实现,如果一个没有明确给出,在建设中使用的模型。
|
protected void |
fireActionPerformed()
通知所有的听众,关于这一事件的类型通知已注册的兴趣。
|
AccessibleContext |
getAccessibleContext()
获取与此相关的
AccessibleContext
JTextField 。
|
Action |
getAction()
返回当前设置为这个
ActionEvent 源
Action ,或
null 如果没有
Action 设置。
|
ActionListener[] |
getActionListeners()
返回所有的
ActionListener s数组添加到这个文本输入框和addactionlistener()。
|
Action[] |
getActions()
获取编辑命令列表。
|
int |
getColumns()
在这
TextField 返回的列数。
|
protected int |
getColumnWidth()
返回列的宽度。
|
int |
getHorizontalAlignment()
返回文本的水平对齐方式。
|
BoundedRangeModel |
getHorizontalVisibility()
获取文本字段的可见性。
|
Dimension |
getPreferredSize()
返回此
TextField 需要首选大小
Dimensions 。
|
int |
getScrollOffset()
获取滚动偏移量,以像素为单位。
|
String |
getUIClassID()
获取用户界面的类标识。
|
boolean |
isValidateRoot()
电话
revalidate 来自内在的本身将通过验证的处理,除非文本包含在一个
JViewport ,在这种情况下,返回false。
|
protected String |
paramString()
返回该
JTextField 字符串表示形式。
|
void |
postActionEvent()
过程行动事件发生在这个文本框派遣他们任何注册
ActionListener 对象。
|
void |
removeActionListener(ActionListener l)
移除指定的监听行动使它不再接收动作事件从这个文本框。
|
void |
scrollRectToVisible(Rectangle r)
向左或向右滚动字段。
|
void |
setAction(Action a)
集
Action 为
ActionEvent 源。
|
void |
setActionCommand(String command)
设置用于操作事件的命令字符串。
|
void |
setColumns(int columns)
套在这
TextField 列数,然后无效化布局。
|
void |
setDocument(Document doc)
将编辑器与文本文档关联。
|
void |
setFont(Font f)
设置当前字体。
|
void |
setHorizontalAlignment(int alignment)
设置文本的水平对齐方式。
|
void |
setScrollOffset(int scrollOffset)
设置滚动偏移量,以像素为单位。
|
addCaretListener, addInputMethodListener, addKeymap, copy, cut, fireCaretUpdate, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getDropLocation, getDropMode, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getPrintable, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, moveCaretPosition, paste, print, print, print, processInputMethodEvent, read, removeCaretListener, removeKeymap, removeNotify, replaceSelection, restoreComposedText, saveComposedText, select, selectAll, setCaret, setCaretColor, setCaretPosition, setComponentOrientation, setDisabledTextColor, setDragEnabled, setDropMode, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setText, setUI, updateUI, viewToModel, write
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
public static final String notifyAction
public JTextField()
TextField
。创建一个默认的模型,初始字符串
null
,和列数设置为0。
public JTextField(String text)
TextField
具有指定文本初始化。创建一个默认模型,列的数目为0。
text
-要显示的文本,或
null
public JTextField(int columns)
TextField
与指定的列数。默认的模型的建立和初始字符串设置为
null
。
columns
-列数用于计算首选宽度;如果列被设置为零,首选的宽度将任何自然结果从组件实现
public JTextField(String text, int columns)
TextField
具有指定文本和列初始化。创建了一个默认模型。
text
-要显示的文本,或
null
columns
-列数用于计算首选宽度;如果列被设置为零,首选的宽度将任何自然结果从组件实现
public JTextField(Document doc, String text, int columns)
JTextField
使用给定的文本存储模型和特定的列数。这是其他构造函数的构造函数的构造函数。如果该文件是
null
,创建一个默认的模型。
doc
-文本存储使用;如果这是
null
,默认将通过调用
createDefaultModel
方法提供
text
-显示初始字符串,或
null
columns
-列数用于计算首选宽度> = 0;如果
columns
设置为零,首选的宽度将任何自然结果从组件实现
IllegalArgumentException
-如果
columns
<0
public String getUIClassID()
getUIClassID
方法重写,继承类
JComponent
JComponent.getUIClassID()
,
UIDefaults.getUI(javax.swing.JComponent)
public void setDocument(Document doc)
setDocument
方法重写,继承类
JTextComponent
doc
-文档显示/编辑
JTextComponent.getDocument()
public boolean isValidateRoot()
revalidate
来自内在的本身将通过验证的处理,除非文本包含在一个
JViewport
,在这种情况下,返回false。
isValidateRoot
方法重写,继承类
JComponent
JViewPort
返回false,否则返回true
JComponent.revalidate()
,
JComponent.isValidateRoot()
,
Container.isValidateRoot()
public int getHorizontalAlignment()
JTextField.LEFT
JTextField.CENTER
JTextField.RIGHT
JTextField.LEADING
JTextField.TRAILING
public void setHorizontalAlignment(int alignment)
JTextField.LEFT
JTextField.CENTER
JTextField.RIGHT
JTextField.LEADING
JTextField.TRAILING
invalidate
和
repaint
叫做当对准,和
PropertyChange
事件(“线形设计”)发射。
alignment
的对齐
IllegalArgumentException
-如果
alignment
不是有效的关键
protected Document createDefaultModel()
PlainDocument
返回的实例。
public int getColumns()
TextField
返回的列数。
public void setColumns(int columns)
TextField
列数,然后无效化布局。
columns
-列> = 0号
IllegalArgumentException
-如果
columns
小于0
protected int getColumnWidth()
public Dimension getPreferredSize()
TextField
需要首选大小
Dimensions
。如果设置了一个非零的列,则将将该列的宽度设置为乘以列宽度的列。
getPreferredSize
方法重写,继承类
JComponent
JComponent.setPreferredSize(java.awt.Dimension)
,
ComponentUI
public void setFont(Font f)
revalidate
是设置后的字体称为。
setFont
方法重写,继承类
JComponent
f
-新的字体
Component.getFont()
public void addActionListener(ActionListener l)
l
-要添加的监听行动
public void removeActionListener(ActionListener l)
l
-要移除的监听行动
public ActionListener[] getActionListeners()
ActionListener
s数组添加到这个文本输入框和addactionlistener()。
ActionListener
s说如果没有听众已添加一个空数组
protected void fireActionPerformed()
EventListenerList
public void setActionCommand(String command)
command
-命令字符串
public void setAction(Action a)
ActionEvent
源
Action
。新
Action
取代任何先前设定的
Action
但不影响
ActionListeners
独立加
addActionListener
。如果
Action
已经注册
ActionListener
为
ActionEvent
源,不重新登记。
立即改变所有的属性描述 Swing Components Supporting Action
设置Action
结果。随后,TextField的性质为Action
特性改变的自动更新。
该方法采用其他三种方法来设置和帮助跟踪Action
的属性值。它使用configurePropertiesFromAction
法立即改变文本的属性。跟踪在Action
的属性值的变化,该方法返回的PropertyChangeListener
createActionPropertyChangeListener
寄存器。默认PropertyChangeListener
调用actionPropertyChanged
方法在Action
变化特性。
a
为
JTextField
的
Action
,或
null
Action
,
getAction()
,
configurePropertiesFromAction(javax.swing.Action)
,
createActionPropertyChangeListener(javax.swing.Action)
,
actionPropertyChanged(javax.swing.Action, java.lang.String)
public Action getAction()
ActionEvent
源
Action
,或
null
如果没有
Action
设置。
ActionEvent
源
Action
,或
null
Action
,
setAction(javax.swing.Action)
protected void configurePropertiesFromAction(Action a)
Action
。参考
Swing Components Supporting Action
为更多的细节,这一集的性质。
a
-从中得到性能的
Action
,或
null
Action
,
setAction(javax.swing.Action)
protected void actionPropertyChanged(Action action, String propertyName)
PropertyChangeListener
createActionPropertyChangeListener
。子类通常不需要调用这个。子类,支持额外的
Action
性能应重写此和
configurePropertiesFromAction
。
引用该表在属性的列表方法 Swing Components Supporting Action
集。
action
-这个文本相关的
Action
propertyName
-改变属性的名称
Action
,
configurePropertiesFromAction(javax.swing.Action)
protected PropertyChangeListener createActionPropertyChangeListener(Action a)
PropertyChangeListener
负责侦听来自指定的
Action
变化和更新相应的属性。
警告:如果子类不创建一个匿名内部类。如果你的寿命将是联系在一起的Action
。
a
- TextField的行动
Action
,
setAction(javax.swing.Action)
public Action[] getActions()
getActions
方法重写,继承类
JTextComponent
public void postActionEvent()
ActionListener
对象。这通常是调用的TextField注册控制器。
public BoundedRangeModel getHorizontalVisibility()
田野的外观和感觉的实施管理的最小、最大程度的价值,并对BoundedRangeModel
性质。
BoundedRangeModel
public int getScrollOffset()
public void setScrollOffset(int scrollOffset)
scrollOffset
-偏移> = 0
public void scrollRectToVisible(Rectangle r)
scrollRectToVisible
方法重写,继承类
JComponent
r
-区域滚动
JViewport
protected String paramString()
JTextField
字符串表示形式。此方法仅用于调试目的,返回的字符串的内容和格式可能会在实现过程中有所不同。返回的字符串是空的但可能不
null
。
paramString
方法重写,继承类
JTextComponent
JTextField
字符串表示形式
public AccessibleContext getAccessibleContext()
AccessibleContext
JTextField
,
JTextFields
,的
AccessibleContext
以一
AccessibleJTextField
形式。一个新的
AccessibleJTextField
实例被创建时。
getAccessibleContext
接口
Accessible
getAccessibleContext
方法重写,继承类
JTextComponent
AccessibleJTextField
作为本
JTextField
的
AccessibleContext
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.