📄 textarea.html
字号:
private final static String newline = "\n";...textArea.append(text + newline);</pre></blockquote><p>Unless the user has moved the caret (insertion point)by clicking or dragging in the text area,the text area automatically scrolls so that the appended text is visible.You can force the text area to scroll to the bottomby moving the caret to the end of the text area, like this,after the call to <code>append</code>:<blockquote><pre>textArea.setCaretPosition(textArea.getDocument().getLength());</pre></blockquote></blockquote><h3><a name="custom">Customizing Text Areas</a></h3><blockquote>You can customize text areas in several ways.For example, although a given text area can display text in only one font and color,you can (as for any component)set which font and color it uses.You can also determine how the text areawraps lines and the number of characters per tab.Finally, you can use the methods <code>JTextArea</code> inheritsfrom <code>JTextComponent</code>to set properties such as the caret,support for dragging,selection color, and so on.<p>The following code,taken from <a class="SourceLink" target="_blank" href="examples/TextSamplerDemo.java"><code>TextSamplerDemo.java</code></a>, demonstratesinitializing an editable text area.The text area uses the specified italic font,and wraps lines between words.<blockquote><pre>JTextArea textArea = new JTextArea( "This is an editable JTextArea. " + "A text area is a \"plain\" text component, " + "which means that although it can display text " + "in any font, all of the text is in the same font.");textArea.setFont(new Font("Serif", Font.ITALIC, 16));textArea.setLineWrap(true);textArea.setWrapStyleWord(true);</pre></blockquote><p>By default, a text area doesn't wrap linesthat are too long for the display area.Instead it uses one linefor all the text between newline characters and—if the text area is within a<a href="scrollpane.html">scroll pane</a> —allows itself to be scrolled horizontally.This example turns line wrapping on witha call to <code>setLineWrap</code>and then calls <code>setWrapStyleWord</code>to indicate that the text area should wrap linesat word boundaries rather than at characterboundaries.<p>To provide scrolling capability, the exampleputs the text area in a scroll pane.<blockquote><pre>JScrollPane areaScrollPane = new JScrollPane(textArea);areaScrollPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);areaScrollPane.setPreferredSize(new Dimension(250, 250));</pre></blockquote>You might have noticed that the <code>JTextArea</code> constructorused in this exampledoes not specify the number of rows or columns.Instead, the code limits the size of the text areaby setting the scroll pane's preferred size.</blockquote><h3><a name="api">The Text Area API</a></h3><blockquote>The following tables list the commonly used<code>JTextArea</code> constructors and methods.Other methods you are likely to callare defined in <code>JTextComponent</code>,and listed in<a href="textapi.html">The Text Component API</a>.<p>You might also invoke methods on atext area 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 areas falls into these categories:<ul><li><a href="#contents">Setting or Getting Contents</a><li><a href="#looks">Fine Tuning Appearance</a><li><a href="#function">Implementing Functionality</a></ul><p><table border=1><caption><a name="contents">Setting or Getting 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/JTextArea.html#JTextArea()">JTextArea()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#JTextArea(java.lang.String)">JTextArea(String)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#JTextArea(java.lang.String, int, int)">JTextArea(String, int, int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#JTextArea(int, int)">JTextArea(int, int)</a> </td> <td>Create a text area. When present, the <code>String</code> argument contains the initial text. The <code>int</code> arguments specify the desired width in columns and height in rows, respectively. </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 area. </td> </tr></table><p><table border=1><caption><a name="looks">Fine Tuning the Text Area'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 area. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#setColumns(int)">void setColumns(int);</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#getColumns()">int getColumns()</a> </td> <td>Set or get the number of columns displayed by the text area. This is really just a hint for computing the area's preferred width. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#setRows(int)">void setRows(int);</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#getRows()">int getRows()</a> </td> <td>Set or get the number of rows displayed by the text area. This is a hint for computing the area's preferred width. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#setTabSize(int)">int setTabSize(int)</a> </td> <td>Set the number of characters a tab is equivalent to. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#setLineWrap(boolean)">int setLineWrap(boolean)</a> </td> <td>Set whether lines are wrapped if they are too long to fit within the allocated width. By default, this property is false and lines are not wrapped. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#setWrapStyleWord(boolean)">int setWrapStyleWord(boolean)</a> </td> <td>Set whether lines can be wrapped at white space (word boundaries) or at any character. By default, this property is false, and lines can be wrapped (if line wrapping is turned on) at any character. </td> </tr></table><p><table border=1><caption><a name="function">Implementing the Text Area'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/text/JTextComponent.html#selectAll()">void selectAll()</a><br><em>(defined in <code>JTextComponent</code>)</em> </td> <td>Select all characters in the text area. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#append(java.lang.String)">void append(String)</a> </td> <td>Add the specified text to the end of the text area. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#insert(java.lang.String, int)">void insert(String, int)</a> </td> <td>Insert the specified text at the specified position. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#replaceRange(java.lang.String, int, int)">void replaceRange(String, int, int)</a> </td> <td>Replace the text between the indicated positions with the specified string. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#getLineCount()">int getLineCount()</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#getLineOfOffset(int)">int getLineOfOffset(int)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#getLineStartOffset(int)">int getLineStartOffset(int)</a><br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextArea.html#getLineEndOffset(int)">int getLineEndOffset(int)</a> </td> <td> Utilities for finding a line number or the position of the beginning or end of the specified line. </td> </tr></table></blockquote><h3><a name="eg">Examples that Use Text Areas</a></h3><blockquote>Many Tutorial examples use <code>JTextArea</code>,typically to provide an area where events are logged.Here's a partial list of the demosthat use text areas.<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 appends user-entered text to a text area.</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> Uses one of each of Swing's text components.</td></tr><tr><td valign=top><a href="examples/index.html#HtmlDemo">HtmlDemo</a></td><td valign=top> <a href="html.html">Using HTML in Swing Components</a></td><td valign=top> A text area lets the user enter HTML code to be displayed in a label.</td></tr><tr><td valign=top><a class="TutorialLink" target="_top" href="../misc/examples/index.html#BasicDnD">BasicDnD</a></td><td valign=top><a class="TutorialLink" target="_top" href="../dnd/intro.html">Introduction to Drag and Drop and Data Transfer</a></td><td valign=top> Demonstrates built-in drag-and-drop functionalityof several Swing components,including text areas.</td></tr><tr><td valign=top><a class="TutorialLink" target="_top" href="../misc/examples/index.html#ExtendedDnDDemo">ExtendedDnDDemo</a></td><td valign=top><a class="TutorialLink" target="_top" href="../dnd/intro.html">Introduction to Drag and Drop and Data Transfer</a></td><td valign=top> Demonstrates dragging and dropping textbetween a text area, a list, and a table.</td></tr><tr><td valign=top><a class="TutorialLink" target="_top" href="../misc/examples/index.html#DragFileDemo">DragFileDemo</a></td><td valign=top><a class="TutorialLink" target="_top" href="../dnd/intro.html">Introduction to Drag and Drop and Data Transfer</a></td><td valign=top> Demonstrates dragging file contents from a file chooser into a text area.A tabbed pane lets you easily switch between files.</td></tr><tr><td valign=top><a class="TutorialLink" target="_top" href="../misc/examples/index.html#FocusConceptsDemo">FocusConceptsDemo</a></td><td valign=top><a class="TutorialLink" target="_top" href="../misc/focus.html">How to Use the Focus Subsystem</a></td><td valign=top> Demonstrates how focus works,using a few components that include a text area.</td></tr></table> </blockquote> <div class=NavBit> <a target=_top href=table.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=textfield.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 Tables <br><b>Next page:</b> How to Use Text Fields </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -