📄 textfield.html
字号:
<td>A <code>JTextField</code> subclass that doesn't show the characters the user types.See<a href="passwordfield.html">How to Use Password Fields</a>.</tr><tr><td><code>JComboBox</code><td>Can be editable,and provides a menu of strings to choose from.See<a href="combobox.html">How to Use Combo Boxes</a>.</tr><tr><td><code>JSpinner</code><td>Combines a formatted text fieldwith a couple of small buttons that let the user choose the previous or next available value.See<a href="spinner.html">How to Use Spinners</a>.</tr></table><p><p>The following example displays a basic text field and a text area.The text field is editable; the text area isn't.When the user presses Enterin the text field,the program copies the text field'scontents to the text area,and then selects all the text in the text field.<p><p><center><IMG SRC="../../figures/uiswing/components/TextDemoSwing.png" WIDTH="246" HEIGHT="152" ALIGN="BOTTOM" ALT="A snapshot of TextDemo"></center></p><p>You can <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/TextDemo.jnlp">run TextDemo</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 source code is in <a class="SourceLink" target="_blank" href="examples/TextDemo.java"><code>TextDemo.java</code></a>.Here's the code that creates and sets up the text field:<blockquote><pre>textField = new JTextField(20);textField.addActionListener(this);</pre></blockquote>The integer argument passedto the <code>JTextField</code> constructor,<code>20</code> in the example,indicates the number of columns in the field.This number is used along with metrics provided bythe field's current fontto calculate the field's preferred width.It does not limit the number of characters the usercan enter.To do that, you can either use a<a href="formattedtextfield.html">formatted text field</a>or a document listener, as described in <a href="generaltext.html">Text Component Features</a>.<blockquote><hr><strong>Note:</strong> We encourage you to specify the number of columnsfor each text field.If you don't specify the number of columns or a preferred size,then the field's preferred size changes whenever the text changes,which can result in unwanted layout updates.<hr></blockquote><p>The next line of coderegisters a <code>TextDemo</code> objectas an action listener for the text field.Here's the <code>actionPerformed</code> methodthat handles action events from the text field:<blockquote><pre>private final static String newline = "\n";...public void actionPerformed(ActionEvent evt) { String text = textField.getText(); textArea.append(text + newline); textField.selectAll();}</pre></blockquote>Notice the use of <code>JTextField</code>'s <code>getText</code>method to retrieve the text currently contained by the text field.The text returned by this methoddoes <em>not</em> include a newline characterfor the Enter key that fired the action event.<p>You've seen how a basic text field can be used.Because <code>JTextField</code> inherits from<code>JTextComponent</code>, it's very flexible and can be customized almostany way you like.For example, you can add a document listeneror document filterto be notified when the text changes and(in the filter case)modify the text field accordingly.Information on text components is in<a href="generaltext.html">Text Component Features</a>Before customizing a <code>JTextField</code>,however, make sure that one of the other <a href="#varieties">components based on text fields</a>won't do the job for you.<p>Often, text fields are paired with labels that describe the text fields.See <a href="#eg">Examples that Use Text Fields</a>for pointers to examples of creating these pairs.</blockquote><h3><a name="api">The Text Field API</a></h3><blockquote>The following tables list the commonly used<code>JTextField</code> constructors and methods.Other methods you are likely to callare defined in <code>JTextComponent</code>.Refer to <a href="textapi.html">The Text Component API</a>.<p>You might also invoke methods on atext field that it inherits from its other ancestors,such as <code>setPreferredSize</code>,<code>setForeground</code>, <code>setBackground</code>, <code>setFont</code>, and so on.See<a href="jcomponent.html">The JComponent Class</a>for tables of commonly used inherited methods.<p>The API for using text fields falls into these categories:<ul><li><a href="#contents">Setting or Getting the Field's Contents</a><li><a href="#looks">Fine Tuning the Field's Appearance</a><li><a href="#function">Implementing the Field's Functionality</a></ul><p><table border=1><caption><a name="contents">Setting or Getting the Field's Contents</a></caption><tr><th>Method or Constructor</th><th>Purpose</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextField.html#JTextField()">JTextField()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextField.html#JTextField(java.lang.String)">JTextField(String)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextField.html#JTextField(java.lang.String, int)">JTextField(String, int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextField.html#JTextField(int)">JTextField(int)</a> </td> <td>Create a text 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. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#setText(java.lang.String)">void setText(String)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#getText()">String getText()</a><br><em>(defined in <code>JTextComponent</code>)</em> </td> <td>Set or get the text displayed by the text field. </td> </tr></table><p><table border=1><caption><a name="looks">Fine Tuning the Field's Appearance</a></caption><tr><th>Method</th><th>Purpose </th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#setEditable(boolean)">void setEditable(boolean)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#isEditable()">boolean isEditable()</a><br><em>(defined in <code>JTextComponent</code>)</em> </td> <td>Set or get whether the user can edit the text in the text field. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextField.html#setColumns(int)">void setColumns(int);</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextField.html#getColumns()">int getColumns()</a> </td> <td>Set or get the number of columns displayed by the text field. This is really just a hint for computing the field's preferred width. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextField.html#setHorizontalAlignment(int)">void setHorizontalAlignment(int);</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextField.html#getHorizontalAlignment()">int getHorizontalAlignment()</a> </td> <td>Set or get how the text is aligned horizontally within its area. You can use <code>JTextField.LEADING</code>, <code>JTextField.CENTER</code>, and <code>JTextField.TRAILING</code> for arguments. </td> </tr></table><p><table border=1><caption><a name="function">Implementing the Field's Functionality</a></caption><tr><th>Method</th><th>Purpose </th></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> </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 text field. </td> </tr></table></blockquote><h3><a name="eg">Examples that Use Text Fields</a></h3><blockquote>This table shows a few of the examples that use <code>JTextField</code>and where those examples are described.For examples of code that's similar among all varieties of text fields,such as dealing with layout,also look at the example lists for related components such as<a href="formattedtextfield.html#eg">formatted text fields</a> and<a href="spinner.html#eg">spinners</a>.<p><table><tr><th align=left> Example</th><th align=left> Where Described</th><th align=left> Notes</th></tr><tr><td valign=top> <a href="examples/index.html#TextDemo">TextDemo</a></td><td valign=top> This section</td><td valign=top> An application that uses a basic text field with an action listener.</td></tr><tr><td valign=top> <a href="examples/index.html#DialogDemo">DialogDemo</a></td><td valign=top> <a href="dialog.html">How to Make Dialogs</a></td><td valign=top> <code>CustomDialog.java</code> includes a text field whose value is checked. You can bring up the dialog by clicking the More Dialogs tab, selecting the Input-validating dialog radio button, and then clicking the Show it! buton.</td></tr><tr><td valign=top><a href="examples/index.html#TextSamplerDemo">TextSamplerDemo</a></td><td valign=top> <a href="text.html">Using Text Components</a></td><td valign=top> Lays out label-text field pairs using a <code>GridBagLayout</code> and a convenience method:<pre>addLabelTextRows(JLabel[] <em>labels</em>, JTextField[] <em>textFields</em>, GridBagLayout <em>gridbag</em>, Container <em>container</em>)</code></pre><tr><td valign=top><a href="examples/index.html#TextInputDemo">TextInputDemo</a></td><td valign=top> <a href="formattedtextfield.html">How to Use Formatted Text Fields</a></td><td valign=top> Lays out label-text field pairs using a <code>SpringLayout</code> and a <code>SpringUtilities</code> convenience method:<pre>makeCompactGrid(Container <em>parent</em>, int <em>rows</em>, int <em>cols</em>, int <em>initialX</em>, int <em>initialY</em>, int <em>xPad</em>, int <em>yPad</em>)</pre></table> </blockquote> <div class=NavBit> <a target=_top href=textarea.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=toolbar.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 Text Areas <br><b>Next page:</b> How to Use Tool Bars </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -