📄 passwordfield.html
字号:
<div class="linkBHEAD"><a href="button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a></div><div class="linkBHEAD"><a href="colorchooser.html">How to Use Color Choosers</a></div><div class="linkBHEAD"><a href="combobox.html">How to Use Combo Boxes</a></div><div class="linkBHEAD"><a href="dialog.html">How to Make Dialogs</a></div><div class="linkBHEAD"><a href="editorpane.html">How to Use Editor Panes and Text Panes</a></div><div class="linkBHEAD"><a href="filechooser.html">How to Use File Choosers</a></div><div class="linkBHEAD"><a href="formattedtextfield.html">How to Use Formatted Text Fields</a></div><div class="linkBHEAD"><a href="frame.html">How to Make Frames (Main Windows)</a></div><div class="linkBHEAD"><a href="internalframe.html">How to Use Internal Frames</a></div><div class="linkBHEAD"><a href="label.html">How to Use Labels</a></div><div class="linkBHEAD"><a href="layeredpane.html">How to Use Layered Panes</a></div><div class="linkBHEAD"><a href="list.html">How to Use Lists</a></div><div class="linkBHEAD"><a href="menu.html">How to Use Menus</a></div><div class="linkBHEAD"><a href="panel.html">How to Use Panels</a></div><div class="nolinkBHEAD">How to Use Password Fields</div><div class="linkBHEAD"><a href="progress.html">How to Use Progress Bars</a></div><div class="linkBHEAD"><a href="rootpane.html">How to Use Root Panes</a></div><div class="linkBHEAD"><a href="scrollpane.html">How to Use Scroll Panes</a></div><div class="linkBHEAD"><a href="separator.html">How to Use Separators</a></div><div class="linkBHEAD"><a href="slider.html">How to Use Sliders</a></div><div class="linkBHEAD"><a href="spinner.html">How to Use Spinners</a></div><div class="linkBHEAD"><a href="splitpane.html">How to Use Split Panes</a></div><div class="linkBHEAD"><a href="tabbedpane.html">How to Use Tabbed Panes</a></div><div class="linkBHEAD"><a href="table.html">How to Use Tables</a></div><div class="linkBHEAD"><a href="textarea.html">How to Use Text Areas</a></div><div class="linkBHEAD"><a href="textfield.html">How to Use Text Fields</a></div><div class="linkBHEAD"><a href="toolbar.html">How to Use Tool Bars</a></div><div class="linkBHEAD"><a href="tooltip.html">How to Use Tool Tips</a></div><div class="linkBHEAD"><a href="tree.html">How to Use Trees</a></div><div class="linkAHEAD"><a href="icon.html">How to Use Icons</a></div><div class="linkAHEAD"><a href="border.html">How to Use Borders</a></div><div class="linkAHEAD"><a href="problems.html">Solving Common Component Problems</a></div></div> </div> <div id=MainFlow class=MainFlow_indented> <span id=BreadCrumbs> <a href=../../index.html target=_top>Home Page</a> > <a href=../index.html target=_top>Creating a GUI with JFC/Swing</a> > <a href=index.html target=_top>Using Swing Components</a> </span> <div class=NavBit> <a target=_top href=panel.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=progress.html>Next »</a> </div> <div id=PageTitle>How to Use Password Fields</div> <blockquote>The<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPasswordField.html"><code>JPasswordField</code></a> class, a subclass of <code>JTextField</code>,provides text fields specialized for password entry.For security reasons,a password field doesn't show the characters the user types.Instead, the field displays another charactersuch as an asterisk '*'.As another security precaution,a password field stores its valueas an array of characters,rather than as a string.Like an ordinary <a href="textfield.html">text field</a>,a password field fires an<a class="TutorialLink" target="_top" href="../events/actionlistener.html">action event</a> when the user indicates that text entry is complete,such as by pressing the Enter button.<p>Here's a picture of a demothat brings up a small windowand prompts the user to type in a password.<p><center><IMG SRC="../../figures/uiswing/components/PasswordDemo.png" WIDTH="325" HEIGHT="96" ALIGN="BOTTOM" ALT="A snapshot of PasswordDemo, which uses a password field"></center></p><p>You can<a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/PasswordDemo.jnlp">run PasswordDemo</a> (it requires release 6) using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java<sup><font size=-2>TM</font></sup> Web Start</a>.(The password is "bugaboo".)The source code is in<a class="SourceLink" target="_blank" href="examples/PasswordDemo.java"><code><code>PasswordDemo.java</code></code></a>.Here's the code that createsand sets up the password field:<blockquote><pre>passwordField = new JPasswordField(10);passwordField.setActionCommand(OK);passwordField.addActionListener(this);</pre></blockquote>The argument passed into the <code>JPasswordField</code> constructorindicates the preferred size of the field —at least 10 columns wide, in this case.By default a password field displays a dot foreach character typed. Calling <code>setEchoChar</code>allows it to be changed to another character.Finally, the code adds an action listener to the password field,which checks the value typed in by the user.Here's the implementation of the action listener's<code>actionPerformed</code> method:<blockquote><pre>public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (OK.equals(cmd)) { //Process the password. <b>char[] input = passwordField.getPassword();</b> if (isPasswordCorrect(input)) { JOptionPane.showMessageDialog(controllingFrame, "Success! You typed the right password."); } else { JOptionPane.showMessageDialog(controllingFrame, "Invalid password. Try again.", "Error Message", JOptionPane.ERROR_MESSAGE); } //Zero out the possible password, for security. for (int i = 0; i < input.length; i++) { input[i] = 0; } passwordField.selectAll(); resetFocus(); } else <em>...//handle the Help button...</em>}</pre></blockquote><blockquote><hr><strong>Security note:</strong> Although the <code>JPasswordField</code> classinherits the <code>getText</code> method,you should use the <code>getPassword</code> method instead.Not only is <code>getText</code> less secure,but in the future it might return the visible string(for example, <code>"******"</code>)instead of the typed-in string.<p>To further enhance security,once you are finished with the character array returned by<code>getPassword</code>,you should set each of its elements to zero.The preceding code snippet shows how to do this.<hr></blockquote><p>A program using a password field typicallyvalidates the password before completing any actionsrequiring the password.This program calls a private method,<code>isPasswordCorrect</code>,that compares the value returned by <code>getPassword</code>to a value stored in a character array.Here is its code:<blockquote><pre>private static boolean isPasswordCorrect(char[] input) { boolean isCorrect = true; char[] correctPassword = { 'b', 'u', 'g', 'a', 'b', 'o', 'o' }; if (input.length != correctPassword.length) { isCorrect = false; } else { for (int i = 0; i < input.length; i++) { if (input[i] != correctPassword[i]) { isCorrect = false; } } } //Zero out the password. for (int i = 0; i < correctPassword.length; i++) { correctPassword[i] = 0; } return isCorrect;}</pre></blockquote></blockquote><h3><a name="api">The Password Field API</a></h3><blockquote>The following tables list the commonly used<code>JPasswordField</code>constructors and methods.For information on the APIpassword fields inherit,see<a href="textfield.html">How to Use Text Fields</a>.<p><table border=1><caption><a name="JPasswordFieldapi">Commonly Used JPasswordField Constructorsand Methods</a></caption><tr><th>Constructor or Method</th><th>Purpose</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPasswordField.html#JPasswordField()">JPasswordField()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPasswordField.html#JPasswordField(java.lang.String)">JPasswordField(String)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPasswordField.html#JPasswordField(java.lang.String, int)">JPasswordField(String, int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPasswordField.html#JPasswordField(int)">JPasswordField(int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPasswordField.html#JPasswordField(javax.swing.text.Document, java.lang.String, int)">JPasswordField(Document, String, int)</a> </td> <td>Create a password field. When present, the <code>int</code> argument specifies the desired width in columns. The <code>String</code> argument contains the field's initial text. The <code>Document</code> argument provides a custom model for the field. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPasswordField.html#getPassword()">char[] getPassword()</a> </td> <td>Set or get the text displayed by the password field. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPasswordField.html#setEchoChar(char)">void setEchoChar(char)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JPasswordField.html#getEchoChar()">char getEchoChar()</a> </td> <td>Set or get the echo character — the character displayed instead of the actual characters typed by the user. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextField.html#addActionListener(java.awt.event.ActionListener)">void addActionListener(ActionListener)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextField.html#removeActionListener(java.awt.event.ActionListener)">void removeActionListener(ActionListener)</a> <br><em>(defined in <code>JTextField</code>)</em> </td> <td>Add or remove an action listener. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#selectAll()">void selectAll()</a><br><em>(defined in <code>JTextComponent</code>)</em> </td> <td>Select all characters in the password field. </td> </tr></table></blockquote><h3><a name="eg">Examples that Use Password Fields</a></h3><blockquote><a href="examples/index.html#PasswordDemo">PasswordDemo</a>is the Tutorial's only example that usesa <code>JPasswordField</code> object.However, the Tutorial has many examples that use<code>JTextField</code>s,whose API is inherited by <code>JPasswordField</code>.See <a href="textfield.html#eg">Examples that Use Text Fields</a>for further information. </blockquote> <div class=NavBit> <a target=_top href=panel.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=progress.html>Next »</a> </div> </div> <div id=Footer><div id=TagNotes> Problems with the examples? Try <a target="_blank" href=../../information/run-examples.html>Compiling and Running the Examples: FAQs</a>. <br> Complaints? Compliments? Suggestions? <a target="_blank" href="http://developer.sun.com/contact/tutorial_feedback.jsp">Give us your feedback</a>.<br><br> <a target="_blank" href="../../information/copyright.html">Copyright</a> 1995-2006 Sun Microsystems, Inc. All rights reserved. <span id=Download></span></div> </div> <div class=PrintHeaders> <b>Previous page:</b> How to Use Panels <br><b>Next page:</b> How to Use Progress Bars </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -