JavaTM 2 Platform
Standard Ed. 6

javax.swing
类 JFormattedTextField

java.lang.Object
  继承者 java.awt.Component
      继承者 java.awt.Container
          继承者 javax.swing.JComponent
              继承者 javax.swing.text.JTextComponent
                  继承者 javax.swing.JTextField
                      继承者 javax.swing.JFormattedTextField
所有已实现的接口:
ImageObserver, MenuContainer, Serializable, Accessible, Scrollable, SwingConstants

public class JFormattedTextField
extends JTextField

JFormattedTextField 扩展了 JTextField,添加了对格式化任意值的支持,以及一旦用户编辑了文本就检索特定对象的支持。以下代码说明了如何配置 JFormattedTextField 来编辑日期:

   JFormattedTextField ftf = new JFormattedTextField();
   ftf.setValue(new Date());
 

一旦创建了 JFormattedTextField,就可以通过添加 PropertyChangeListener 的方式来侦听编辑更改,并且使用属性名 value 来侦听 PropertyChangeEvent

JFormattedTextField 允许配置在失去焦点时应该采取的措施。可能的值为:

描述

JFormattedTextField.REVERT 恢复显示以匹配 getValue,这可能丢失当前的编辑内容。
JFormattedTextField.COMMIT 提交当前值。如果 AbstractFormatter 不认为所编辑的值是合法值,则抛出 ParseException,然后不更改该值并保留已编辑的值。
JFormattedTextField.COMMIT_OR_REVERT COMMIT 类似,但是如果该值不是合法的,则其行为类似于 REVERT
JFormattedTextField.PERSIST 不执行任何操作,不获取新的 AbstractFormatter 也不更新该值。
默认值为 JFormattedTextField.COMMIT_OR_REVERT,有关此内容的更多信息,请参阅 setFocusLostBehavior(int)

JFormattedTextField 允许焦点离开,即使当前编辑的值是无效的也是如此。要在 JFormattedTextField 处于无效编辑状态时锁定焦点,可以附加一个 InputVerifier。以下代码片断显示了这种 InputVerifier 的可能实现:

 public class FormattedTextFieldVerifier extends InputVerifier {
     public boolean verify(JComponent input) {
         if (input instanceof JFormattedTextField) {
             JFormattedTextField ftf = (JFormattedTextField)input;
             AbstractFormatter formatter = ftf.getFormatter();
             if (formatter != null) {
                 String text = ftf.getText();
                 try {
                      formatter.stringToValue(text);
return true;
                  } catch (ParseException pe) {
                      return false;
                  }
              }
          }
return true;
      }
      public boolean shouldYieldFocus(JComponent input) {
          return verify(input);
      }
  }
 

或者,也可以调用 commitEdit,这也会提交该值。

JFormattedTextField 本身不执行格式化,而是通过从 JFormattedTextField.AbstractFormatterFactory 实例获得的 JFormattedTextField.AbstractFormatter 实例完成格式化。通过 install 方法的方式使 JFormattedTextField.AbstractFormatter 的实例处于激活状态时会通知它们,此时 JFormattedTextField.AbstractFormatter 可以安装其所需的任何内容,通常是 DocumentFilter。类似地,当 JFormattedTextField 不再需要 AbstractFormatter 时,它会调用 uninstall

JFormattedTextField 通常在获得或丢失焦点时查询 AbstractFormatterFactory 以找到 AbstractFormat。但根据焦点丢失策略也可以更改此行为。如果焦点丢失策略是 JFormattedTextField.PERSIST 并且已编辑了 JFormattedTextField,则在值提交之前不会查询 AbstractFormatterFactory。类似地,如果焦点丢失策略是 JFormattedTextField.COMMIT 并且从 stringToValue 中抛出异常,则丢失或获得焦点时不查询 AbstractFormatterFactory

JFormattedTextField.AbstractFormatter 还负责确定何时将值提交给 JFormattedTextField。某些 JFormattedTextField.AbstractFormatter 在每次编辑时都提供新值,而其他的则从不提交该值。可以强行从当前的 JFormattedTextField.AbstractFormatter 获得当前值,方法是调用 commitEdit。每当在 JFormattedTextField 中按下了 return 键,就调用 commitEdit

如果尚未显式地设置 AbstractFormatterFactory,则在调用 setValue 后(假定 value 是非 null 的),根据 value 类型的 Class 设置该值。例如,在以下代码中将创建一个合适的 AbstractFormatterFactoryAbstractFormatter,以处理数字的格式化:

   JFormattedTextField tf = new JFormattedTextField();
   tf.setValue(new Number(100));
 

警告:由于 AbstractFormatter 通常在 Document 上安装一个 DocumentFilter,在 JFormattedTextField 上安装一个 NavigationFilter,所以您不应安装自己的过滤器。如果您安装了,则会看到奇怪的行为,因为将无法强制执行 AbstractFormatter 的编辑策略。

警告:Swing 不是线程安全的。有关更多信息,请参阅 Swing's Threading Policy

警告:此类的序列化对象与以后的 Swing 版本不兼容。当前序列化支持适用于短期存储,或适用于在运行相同 Swing 版本的应用程序之间进行 RMI(Remote Method Invocation,远程方法调用)。从 1.4 版本开始,已在 java.beans 包中添加了支持所有 JavaBeansTM 长期存储的功能。请参见 XMLEncoder

从以下版本开始:
1.4

嵌套类摘要
static class JFormattedTextField.AbstractFormatter
           JFormattedTextField 所使用的 AbstractFormatter 实例,用于处理从 Object 到 String 和从 String 到 Object 的转换。
static class JFormattedTextField.AbstractFormatterFactory
          JFormattedTextField 使用的 AbstractFormatterFactory 实例,用来获得 AbstractFormatter 实例,依次使用该实例格式化各个值。
 
从类 javax.swing.JTextField 继承的嵌套类/接口
JTextField.AccessibleJTextField
 
从类 javax.swing.text.JTextComponent 继承的嵌套类/接口
JTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, JTextComponent.KeyBinding
 
从类 javax.swing.JComponent 继承的嵌套类/接口
JComponent.AccessibleJComponent
 
从类 java.awt.Container 继承的嵌套类/接口
Container.AccessibleAWTContainer
 
从类 java.awt.Component 继承的嵌套类/接口
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
字段摘要
static int COMMIT
          标识丢失焦点时,应该调用 commitEdit 的常量。
static int COMMIT_OR_REVERT
          标识丢失焦点时,应该调用 commitEdit 的常量。
static int PERSIST
          标识丢失焦点时,应该保留编辑值的常量。
static int REVERT
          标识丢失焦点时,应该将编辑的值恢复为 JFormattedTextField 上所设当前值的常量。
 
从类 javax.swing.JTextField 继承的字段
notifyAction
 
从类 javax.swing.text.JTextComponent 继承的字段
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
 
从类 javax.swing.JComponent 继承的字段
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
从类 java.awt.Component 继承的字段
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
从接口 javax.swing.SwingConstants 继承的字段
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
从接口 java.awt.image.ImageObserver 继承的字段
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
构造方法摘要
JFormattedTextField()
          创建一个没有 AbstractFormatterFactoryJFormattedTextField
JFormattedTextField(Format format)
          创建一个 JFormattedTextField
JFormattedTextField(JFormattedTextField.AbstractFormatter formatter)
          创建一个具有指定 AbstractFormatterJFormattedTextField
JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory)
          创建一个具有指定 AbstractFormatterFactoryJFormattedTextField
JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory, Object currentValue)
          创建一个具有指定 AbstractFormatterFactory 和初始值的 JFormattedTextField
JFormattedTextField(Object value)
          创建一个具有指定值的 JFormattedTextField。
 
方法摘要
 void commitEdit()
          强制从 AbstractFormatter 获得当前值并将其设置为当前值。
 Action[] getActions()
          获取编辑器的命令列表。
 int getFocusLostBehavior()
          返回丢失焦点时的行为。
 JFormattedTextField.AbstractFormatter getFormatter()
          返回用于格式化和分析当前值的 AbstractFormatter
 JFormattedTextField.AbstractFormatterFactory getFormatterFactory()
          返回当前的 AbstractFormatterFactory
 String getUIClassID()
          获得 UI 的类 ID。
 Object getValue()
          返回最后一个有效值。
protected  void invalidEdit()
          用户输入无效值时调用。
 boolean isEditValid()
          如果所编辑的当前值有效,则返回 true。
protected  void processFocusEvent(FocusEvent e)
          处理所有的焦点事件,如 FocusEvent.FOCUS_GAINEDFocusEvent.FOCUS_LOST
protected  void processInputMethodEvent(InputMethodEvent e)
          处理所有的输入法事件,如 InputMethodEvent.INPUT_METHOD_TEXT_CHANGEDInputMethodEvent.CARET_POSITION_CHANGED
 void setDocument(Document doc)
          将该编辑器与某个文本文档关联。
 void setFocusLostBehavior(int behavior)
          设置丢失焦点时的行为。
protected  void setFormatter(JFormattedTextField.AbstractFormatter format)
          设置当前的 AbstractFormatter
 void setFormatterFactory(JFormattedTextField.AbstractFormatterFactory tf)
          设置 AbstractFormatterFactory
 void setValue(Object value)
          设置值,该值由从当前 AbstractFormatterFactory 获得的 AbstractFormatter 进行格式化。
 
从类 javax.swing.JTextField 继承的方法
actionPropertyChanged, addActionListener, configurePropertiesFromAction, createActionPropertyChangeListener, createDefaultModel, fireActionPerformed, getAccessibleContext, getAction, getActionListeners, getColumns, getColumnWidth, getHorizontalAlignment, getHorizontalVisibility, getPreferredSize, getScrollOffset, isValidateRoot, paramString, postActionEvent, removeActionListener, scrollRectToVisible, setAction, setActionCommand, setColumns, setFont, setHorizontalAlignment, setScrollOffset
 
从类 javax.swing.text.JTextComponent 继承的方法
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, read, removeCaretListener, removeKeymap, removeNotify, replaceSelection, select, selectAll, setCaret, setCaretColor, setCaretPosition, setComponentOrientation, setDisabledTextColor, setDragEnabled, setDropMode, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setText, setUI, updateUI, viewToModel, write
 
从类 javax.swing.JComponent 继承的方法
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, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, 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
 
从类 java.awt.Container 继承的方法
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, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
从类 java.awt.Component 继承的方法
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, hide, 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, 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, transferFocusUpCycle
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

字段详细信息

COMMIT

public static final int COMMIT
标识丢失焦点时,应该调用 commitEdit 的常量。如果在提交新值的过程中抛出 ParseException,则将保留无效值。

另请参见:
setFocusLostBehavior(int), 常量字段值

COMMIT_OR_REVERT

public static final int COMMIT_OR_REVERT
标识丢失焦点时,应该调用 commitEdit 的常量。如果在提交新值的过程中抛出 ParseException,则将恢复原值。

另请参见:
setFocusLostBehavior(int), 常量字段值

REVERT

public static final int REVERT
标识丢失焦点时,应该将编辑的值恢复为 JFormattedTextField 上所设当前值的常量。

另请参见:
setFocusLostBehavior(int), 常量字段值

PERSIST

public static final int PERSIST
标识丢失焦点时,应该保留编辑值的常量。

另请参见:
setFocusLostBehavior(int), 常量字段值
构造方法详细信息

JFormattedTextField

public JFormattedTextField()
创建一个没有 AbstractFormatterFactoryJFormattedTextField。使用 setMasksetFormatterFactory 配置 JFormattedTextField 以编辑特定的值类型。


JFormattedTextField

public JFormattedTextField(Object value)
创建一个具有指定值的 JFormattedTextField。这将根据 value 的类型创建一个 AbstractFormatterFactory

参数:
value - JFormattedTextField 的初始值

JFormattedTextField

public JFormattedTextField(Format format)
创建一个 JFormattedTextFieldformat 包装在一个适当的 AbstractFormatter 中,然后该 AbstractFormatter 包装在一个 AbstractFormatterFactory 中。

参数:
format - 用于查找 AbstractFormatter 的 Format

JFormattedTextField

public JFormattedTextField(JFormattedTextField.AbstractFormatter formatter)
创建一个具有指定 AbstractFormatterJFormattedTextField。该 AbstractFormatter 被放置在 AbstractFormatterFactory 中。

参数:
formatter - 进行格式化所使用的 AbstractFormatter。

JFormattedTextField

public JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory)
创建一个具有指定 AbstractFormatterFactoryJFormattedTextField

参数:
factory - 用于进行格式化的 AbstractFormatterFactory。

JFormattedTextField

public JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory,
                           Object currentValue)
创建一个具有指定 AbstractFormatterFactory 和初始值的 JFormattedTextField

参数:
factory - 用于进行格式化的 AbstractFormatterFactory
currentValue - 要使用的初始值
方法详细信息

setFocusLostBehavior

public void setFocusLostBehavior(int behavior)
设置丢失焦点时的行为。这是 JFormattedTextField.COMMIT_OR_REVERTJFormattedTextField.REVERTJFormattedTextField.COMMITJFormattedTextField.PERSIST 之一。注意,某些 AbstractFormatter 可能在出现更改时即进行下一步操作,所以该值无效。

如果传入的对象不是上述值之一,则将抛出 IllegalArgumentException

此属性的默认值为 JFormattedTextField.COMMIT_OR_REVERT

参数:
behavior - 标识丢失焦点时的行为
抛出:
IllegalArgumentException - 如果 behavior 不是某个已知值

getFocusLostBehavior

public int getFocusLostBehavior()
返回丢失焦点时的行为。这是 COMMIT_OR_REVERTCOMMITREVERTPERSIST 之一。注意,某些 AbstractFormatter 可能在出现更改时即进行下一步操作,所以该值无效。

返回:
返回丢失焦点时的行为

setFormatterFactory

public void setFormatterFactory(JFormattedTextField.AbstractFormatterFactory tf)
设置 AbstractFormatterFactoryAbstractFormatterFactory 能够返回 AbstractFormatter 的实例,它可用于格式化要显示的某个值,以及强制执行编辑策略。

如果尚未通过此方法(或构造方法)显式地设置 AbstractFormatterFactory,则根据该值的 Class 使用 AbstractFormatterFactory,然后使用 AbstractFormatterNumberFormatter 用于 NumberDateFormatter 用于 Dates,其他值将使用 DefaultFormatter

这是一个 JavaBeans 绑定 (bound) 属性。

参数:
tf - 用于查找 AbstractFormatter 实例的 AbstractFormatterFactory

getFormatterFactory

public JFormattedTextField.AbstractFormatterFactory getFormatterFactory()
返回当前的 AbstractFormatterFactory

返回:
用于确定 AbstractFormatterAbstractFormatterFactory
另请参见:
setFormatterFactory(javax.swing.JFormattedTextField.AbstractFormatterFactory)

setFormatter

protected void setFormatter(JFormattedTextField.AbstractFormatter format)
设置当前的 AbstractFormatter

通常不应调用此方法,而是设置 AbstractFormatterFactory 或设置值。当 JFormattedTextField 的状态更改并且需要重置该值时,JFormattedTextField 将调用此方法。JFormattedTextField 传入从 AbstractFormatterFactory 所获得的 AbstractFormatter

这是一个 JavaBeans 绑定属性。

参数:
format - 进行格式化所使用的 AbstractFormatter
另请参见:
setFormatterFactory(javax.swing.JFormattedTextField.AbstractFormatterFactory)

getFormatter

public JFormattedTextField.AbstractFormatter getFormatter()
返回用于格式化和分析当前值的 AbstractFormatter

返回:
用于进行格式化的 AbstractFormatter

setValue

public void setValue(Object value)
设置值,该值由从当前 AbstractFormatterFactory 获得的 AbstractFormatter 进行格式化。如果尚未指定任何 AbstractFormatterFactory,则此方法会试图根据 value 的类型创建它。

此属性的默认值为 null。

这是一个 JavaBeans 绑定属性。

参数:
value - 要显示的当前值

getValue

public Object getValue()
返回最后一个有效值。根据 AbstractFormatter 的编辑策略,此方法可能不会返回当前值。可通过调用 commitEdit 后调用 getValue 来获得当前编辑的值。

返回:
最后一个有效值

commitEdit

public void commitEdit()
                throws ParseException
强制从 AbstractFormatter 获得当前值并将其设置为当前值。 如果当前没有已安装的 AbstractFormatter,则此方法无效。

抛出:
ParseException - 如果 AbstractFormatter 无法格式化当前值

isEditValid

public boolean isEditValid()
如果所编辑的当前值有效,则返回 true。此值由当前的 AbstractFormatter 进行管理,并且没有针对它的公共 setter。

返回:
如果所编辑的当前值有效,则返回 true。

invalidEdit

protected void invalidEdit()
用户输入无效值时调用。这就使得组件可以提供反馈。默认的实现是发出蜂鸣声。


processInputMethodEvent

protected void processInputMethodEvent(InputMethodEvent e)
处理所有的输入法事件,如 InputMethodEvent.INPUT_METHOD_TEXT_CHANGEDInputMethodEvent.CARET_POSITION_CHANGED

覆盖:
JTextComponent 中的 processInputMethodEvent
参数:
e - InputMethodEvent
另请参见:
InputMethodEvent

processFocusEvent

protected void processFocusEvent(FocusEvent e)
处理所有的焦点事件,如 FocusEvent.FOCUS_GAINEDFocusEvent.FOCUS_LOST

覆盖:
Component 中的 processFocusEvent
参数:
e - FocusEvent
另请参见:
FocusEvent

getActions

public Action[] getActions()
获取编辑器的命令列表。这是嵌入的 UI 所支持的命令列表,该 UI 通过编辑器本身支持的命令集进行扩充。这些命令对于绑定到事件是很有用的,例如在 keymap 中。

覆盖:
JTextField 中的 getActions
返回:
命令列表

getUIClassID

public String getUIClassID()
获得 UI 的类 ID。

覆盖:
JTextField 中的 getUIClassID
返回:
字符串 "FormattedTextFieldUI"
另请参见:
JComponent.getUIClassID()

setDocument

public void setDocument(Document doc)
将该编辑器与某个文本文档关联。使用当前已注册的工厂构建一个文档视图,重新确认后由编辑器显示该文档。PropertyChange 事件 ("document") 被传播到每个侦听器。

覆盖:
JTextField 中的 setDocument
参数:
doc - 要显示/编辑的文档
另请参见:
JTextComponent.getDocument()

JavaTM 2 Platform
Standard Ed. 6

提交错误或意见
有关更多的 API 参考资料和开发人员文档,请参阅 Java 2 SDK SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。

版权所有 2004 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策