java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.text.JTextComponent
javax.swing.JTextField
javax.swing.JPasswordField
- 所有已实现的接口:
-
ImageObserver
,MenuContainer
,Serializable
,Accessible
,Scrollable
,SwingConstants
@JavaBean(description="Allows the editing of a line of text but doesn\'t show the characters.") public class JPasswordField extends JTextField
JPasswordField
是一个轻量级组件,允许编辑单行文本,其中视图指示已输入内容,但不显示原始字符。您可以在如何使用文本字段中找到更多信息和示例,这是《Java教程》中的一个部分。
JPasswordField
旨在与使用设置了echoChar
的java.awt.TextField
源代码兼容。它是单独提供的,以便更轻松地安全更改JTextField
的UI,而不影响密码输入。
注意: 默认情况下,JPasswordField 禁用输入方法;否则,输入字符可能在使用输入方法组合时可见。如果应用程序需要输入方法支持,请使用继承的方法enableInputMethods(true)
。
警告: JPasswordField
不会显示已输入的原始字符,而是显示替代文本或图形。但是,这并不会阻止密码出现在系统内存中。对于处理诸如密码文本之类的机密信息,请参阅安全编码指南中的相关部分。
警告: Swing 不是线程安全的。有关更多信息,请参见Swing的线程策略。
警告: 该类的序列化对象将不兼容未来的Swing版本。当前的序列化支持适用于短期存储或在运行相同Swing版本的应用程序之间进行RMI。从1.4版本开始,已将所有JavaBeans的长期存储支持添加到java.beans
包中。请参见XMLEncoder
。
- 自版本:
- 1.2
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected class
该类为JPasswordField
类实现了辅助功能支持。Nested classes/interfaces declared in class javax.swing.JTextField
JTextField.AccessibleJTextField
Nested classes/interfaces declared in class javax.swing.text.JTextComponent
JTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, JTextComponent.KeyBinding
Nested classes/interfaces declared in class javax.swing.JComponent
JComponent.AccessibleJComponent
Nested classes/interfaces declared in class java.awt.Container
Container.AccessibleAWTContainer
Nested classes/interfaces declared in class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Field Summary
Fields declared in class javax.swing.JTextField
notifyAction
Fields declared in class javax.swing.text.JTextComponent
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
Fields declared in class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Fields declared in class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
Fields declared in interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Fields declared in interface 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
-
Constructor Summary
ConstructorDescription构造一个新的JPasswordField
,具有默认文档、null
起始文本字符串和0列宽度。JPasswordField
(int columns) 构造一个具有指定列数的新空JPasswordField
。JPasswordField
(String text) 构造一个初始化为指定文本的新JPasswordField
。JPasswordField
(String text, int columns) 构造一个初始化为指定文本和列数的新JPasswordField
。JPasswordField
(Document doc, String txt, int columns) 构造一个使用给定文本存储模型和给定列数的新JPasswordField
。 -
Method Summary
Modifier and TypeMethodDescriptionvoid
copy()
在当前外观和感觉上调用provideErrorFeedback
,通常会触发错误蜂鸣声。void
cut()
在当前外观和感觉上调用provideErrorFeedback
,通常会触发错误蜂鸣声。boolean
如果此JPasswordField
设置了用于回显的字符集,则返回true。返回与此JPasswordField
关联的AccessibleContext
。char
返回用于回显的字符。char[]
返回此TextComponent
中包含的文本。getText()
已弃用。getText
(int offs, int len) 已弃用。从Java 2平台v1.2开始,已被getPassword
替换。返回渲染此组件的L&F类的名称。protected String
返回此JPasswordField
的字符串表示形式。void
setEchoChar
(char c) 设置此JPasswordField
的回显字符。void
将此TextComponent
的文本设置为指定的文本。void
updateUI()
重新加载可插拔UI。Methods declared in class javax.swing.JTextField
actionPropertyChanged, addActionListener, configurePropertiesFromAction, createActionPropertyChangeListener, createDefaultModel, fireActionPerformed, getAction, getActionListeners, getActions, getColumns, getColumnWidth, getHorizontalAlignment, getHorizontalVisibility, getPreferredSize, getScrollOffset, isValidateRoot, postActionEvent, removeActionListener, scrollRectToVisible, setAction, setActionCommand, setColumns, setDocument, setFont, setHorizontalAlignment, setScrollOffset
Methods declared in class javax.swing.text.JTextComponent
addCaretListener, addKeymap, 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, getToolTipText, getUI, isEditable, loadKeymap, modelToView, modelToView2D, moveCaretPosition, paste, print, print, print, read, removeCaretListener, removeKeymap, replaceSelection, restoreComposedText, saveComposedText, select, selectAll, setCaret, setCaretColor, setCaretPosition, setDisabledTextColor, setDragEnabled, setDropMode, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setUI, viewToModel, viewToModel2D, write
Methods declared in class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, 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, removeNotify, 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
Methods declared in class 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, transferFocusDownCycle, validate, validateTree
Methods declared in class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, 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, 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, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
Constructor Details
-
JPasswordField
public JPasswordField()构造一个新的JPasswordField
,具有默认文档、null
起始文本字符串和0列宽度。 -
JPasswordField
构造一个初始化为指定文本的新JPasswordField
。文档模型设置为默认值,列数设置为0。- 参数:
-
text
- 要显示的文本,如果没有则为null
-
JPasswordField
public JPasswordField(int columns) 构造一个具有指定列数的新空JPasswordField
。创建默认模型,并将初始字符串设置为null
。- 参数:
-
columns
- 列数 >= 0
-
JPasswordField
构造一个初始化为指定文本和列数的新JPasswordField
。文档模型设置为默认值。- 参数:
-
text
- 要显示的文本,如果没有则为null
-
columns
- 列数 >= 0
-
JPasswordField
构造一个使用给定文本存储模型和给定列数的新JPasswordField
。这是其他构造函数的构造函数。回显字符设置为'*',但可以通过当前外观和感觉更改。如果文档模型为null
,则将创建一个默认文档模型。- 参数:
-
doc
- 要使用的文本存储 -
txt
- 要显示的文本,如果没有则为null
-
columns
- 用于计算首选宽度的列数 >= 0;如果列数设置为零,则首选宽度将是组件实现自然产生的宽度
-
-
Method Details
-
getUIClassID
返回渲染此组件的L&F类的名称。- 覆盖:
-
getUIClassID
在类JTextField
中 - 返回:
- 字符串"PasswordFieldUI"
- 参见:
-
updateUI
public void updateUI()重新加载可插拔UI。用于获取新接口的键是getUIClassID()
。UI的类型是TextUI
。在设置UI后调用invalidate
。- 覆盖:
-
updateUI
在类JTextComponent
中 - 自版本:
- 1.6
- 参见:
-
getEchoChar
public char getEchoChar()返回用于回显的字符。默认为'*'。默认值可能会因当前运行的外观和感觉而有所不同。例如,Metal/Ocean的默认值是一个圆点字符。- 返回:
- 回显字符,如果未设置则为0
- 参见:
-
setEchoChar
@BeanProperty(bound=false, visualUpdate=true, description="character to display in place of the real characters") public void setEchoChar(char c) 设置此JPasswordField
的回显字符。请注意,这在很大程度上是一个建议,因为安装的视图可以使用任何图形技术来表示字段。设置值为0表示您希望看到输入的文本,类似于标准JTextField
的行为。- 参数:
-
c
- 要显示的回显字符 - 参见:
-
echoCharIsSet
public boolean echoCharIsSet()如果此JPasswordField
设置了用于回显的字符集,则返回true。如果回显字符不为0,则认为已设置字符。- 返回:
- 如果已设置回显字符,则返回true
- 参见:
-
cut
public void cut()在当前外观和感觉上调用provideErrorFeedback
,通常会触发错误蜂鸣声。将当前选择的范围从关联文本模型传输到系统剪贴板的正常行为,并从模型中删除内容,对于密码字段来说是不可接受的。- 覆盖:
-
cut
在类JTextComponent
中 - 参见:
-
copy
public void copy()在当前外观和感觉上调用provideErrorFeedback
,通常会触发错误蜂鸣声。将当前选择的范围从关联文本模型传输到系统剪贴板,并保留模型中的内容的正常行为,对于密码字段来说是不可接受的。- 覆盖:
-
copy
在类JTextComponent
中 - 参见:
-
getText
Deprecated.As of Java 2 platform v1.2, replaced bygetPassword
.返回此TextComponent
中包含的文本。如果底层文档为null
,将引发NullPointerException
。出于安全原因,此方法已被弃用。请改用
* getPassword
方法。- 覆盖:
-
getText
在类JTextComponent
中 - 返回:
- 文本
- 参见:
-
getText
Deprecated.As of Java 2 platform v1.2, replaced bygetPassword
.获取组件表示的文本的一部分。如果长度为0,则返回空字符串。出于安全原因,此方法已被弃用。请改用
getPassword
方法。- 覆盖:
-
getText
在类JTextComponent
中 - 参数:
-
offs
- 偏移量 >= 0 -
len
- 长度 >= 0 - 返回:
- 文本
- 抛出:
-
BadLocationException
- 如果偏移量或长度无效
-
setText
从类中复制的描述:JTextComponent
将此TextComponent
的文本设置为指定的文本。如果文本为null
或为空,则仅删除旧文本。插入文本后,结果光标位置由光标类的实现确定。请注意,文本不是绑定属性,因此在更改时不会触发
PropertyChangeEvent
。要监听文本的更改,请使用DocumentListener
。- 覆盖:
-
setText
在类JTextComponent
中 - 参数:
-
t
- 要设置的新文本 - 参见:
-
getPassword
返回此TextComponent
中包含的文本。如果底层文档为null
,将引发NullPointerException
。为了更强的安全性,建议在使用后清除返回的字符数组,将每个字符设置为零。- 返回:
- 文本
-
paramString
返回此JPasswordField
的字符串表示形式。此方法仅用于调试目的,返回的字符串的内容和格式可能因实现而异。返回的字符串可能为空,但不能为null
。- 覆盖:
-
paramString
在类JTextField
中 - 返回:
-
此
JPasswordField
的字符串表示形式
-
getAccessibleContext
返回与此JPasswordField
关联的AccessibleContext
。对于密码字段,AccessibleContext
采用AccessibleJPasswordField
的形式。如有必要,将创建一个新的AccessibleJPasswordField
实例。- 指定者:
-
getAccessibleContext
在接口Accessible
中 - 覆盖:
-
getAccessibleContext
在类JTextField
中 - 返回:
-
作为此
JPasswordField
的AccessibleContext
的AccessibleJPasswordField
-
getPassword
替换。