📄 textapi.html
字号:
<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=generaltext.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=componentlist.html>Next »</a> </div> <div id=PageTitle>The Text Component API</div> <blockquote><a name="api"> </a>This section lists commonly used partsof the API shared by text components,much of it defined by the <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html"><code>JTextComponent</code></a> class.<a href="generaltext.html">Text Component Features</a>discusses how to use some of this API.<p>Also see<a href="jcomponent.html">The JComponent Class</a>,which describes the API that text componentsinherit from <code>JComponent</code>.For information about the API for specific text components,see the how-to page for the component:<a href="textfield.html">text field</a>,<a href="passwordfield.html">password field</a>,<a href="formattedtextfield.html">formatted text field</a>,<a href="textarea.html">text area</a>, or<a href="editorpane.html">editor pane and text pane</a>.<p>For a complete listing of and further details about the text API,see the API documentation for<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html"><code>JTextComponent</code></a> and for the various classes andinterfaces in the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/package-summary.html">text package</a>.<p>The API listed in this section falls into these categories:<ul><li> <a href="#appearance">Setting Attributes</a><li> <a href="#selection">Manipulating the Selection</a><li> <a href="#conversions">Converting Positions Between the Model and The View</a><li> <a href="#apiforcommands">Text Editing Commands</a><li> <a href="#docclasses">Classes and Interfaces that Represent Documents</a><li> <a href="#docmethods">Working with Documents</a><li> <a href="#carrots">Manipulating Carets and Selection Highlighters</a><li> <a href="#io">Reading and Writing Text</a></ul><p><table border=1> <caption><a name="appearance"> Setting Attributes<br><em>These methods are in the <code>JTextComponent</code> class.</em></a></caption><tr><th>Method</th><th>Description</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#setDisabledTextColor(java.awt.Color)">void setDisabledTextColor(Color)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#getDisabledTextColor()">Color getDisabledTextColor()</code></font></a> </td> <td>Set or get the color used to display text when the text component is disabled. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#setMargin(java.awt.Insets)">void setMargin(Insets)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#getMargin()">Insets getMargin()</a> </td> <td>Set or get the margin between the text and the text component's border. </td> </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> </td> <td>Set or get whether the user can edit the text in the text component. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#setDragEnabled(boolean)">void setDragEnabled(boolean)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#getDragEnabled()">boolean getDragEnabled()</a> </td> <td>Set or get the <code>dragEnabled</code> property, which must be true to enable drag handling on this component. The default value is false. See<a class="TutorialLink" target="_top" href="../../dnd/index.html">Drag and Drop</a> for more details. </tr></table><p><table border=1><caption><a name="selection">Manipulating the Selection</a><br><em>These methods are in the <code>JTextComponent</code> class.</em></caption><tr><th>Method</th><th>Description</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#getSelectedText()">String getSelectedText()</a> </td> <td>Get the currently selected text. </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><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#select(int, int)">void select(int, int)</a> </td> <td>Select all text or select text within a start and end range. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#setSelectionStart(int)">void setSelectionStart(int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#setSelectionEnd(int)">void setSelectionEnd(int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#getSelectionStart()">int getSelectionStart()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#getSelectionEnd()">int getSelectionEnd()</a> </td> <td>Set or get extent of the current selection by index. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#setSelectedTextColor(java.awt.Color)">void setSelectedTextColor(Color)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#getSelectedTextColor()">Color getSelectedTextColor()</a> </td> <td>Set or get the color of selected text. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#setSelectionColor(java.awt.Color)">void setSelectionColor(Color)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#getSelectionColor()">Color getSelectionColor()</a> </td> <td>Set or get the background color of selected text. </td> </tr></table><p><table border=1> <caption><a name="conversions"> Converting Positions Between the Model and the View</a><br><em>These methods are in the <code>JTextComponent</code> class.</em></caption><tr><th>Method</th><th>Description</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#viewToModel(java.awt.Point)">int viewToModel(Point)</a> </td> <td>Convert the specified point in the view coordinate system to a position within the text. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#modelToView(int)">Rectangle modelToView(int)</a> </td> <td>Convert the specified position within the text to a rectangle in the view coordinate system. </td> </tr></table><p><table border=1><caption><a name="apiforcommands">Text Editing Commands</a></caption> <tr><th>Class or Method</th><th>Description</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#cut()">void cut()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#copy()">void copy()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#paste()">void paste()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#replaceSelection(java.lang.String)">void replaceSelection(String)</a> <br><em>(in <code>JTextComponent</code>)</em> </td> <td>Cut, copy, and paste text using the system clipboard. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/EditorKit.html">EditorKit</a> </td> <td> Provides a text component's view factory, document, caret, and actions, as well as reading and writing documents of a particular format. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/DefaultEditorKit.html">DefaultEditorKit</a> </td> <td>A concrete subclass of <code>EditorKit</code> that provides the basic text editing capabilities. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/StyledEditorKit.html">StyledEditorKit</a> </td> <td>A subclass of <code>Default EditorKit</code> that provides additional editing capabilities for styled text. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/DefaultEditorKit.html#field_summary">String <em>xxxx</em>Action</a> <br><em>(in <code>DefaultEditorKit</code>)</em> </td> <td>The names of all the actions supported by the default editor kit. See <a href="generaltext.html#commands">Associating Text Actions with Menus and Buttons</a>. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/DefaultEditorKit.BeepAction.html">BeepAction</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/DefaultEditorKit.CopyAction.html">CopyAction</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/DefaultEditorKit.CutAction.html">CutAction</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/DefaultEditorKit.DefaultKeyTypedAction.html">DefaultKeyTypedAction</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/DefaultEditorKit.InsertBreakAction.html">InsertBreakAction</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/DefaultEditorKit.InsertContentAction.html">InsertContentAction</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/DefaultEditorKit.InsertTabAction.html">InsertTabAction</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/DefaultEditorKit.PasteAction.html">PasteAction</a> <br><em>(in <code>DefaultEditorKit</code>)</em> </td> <td>Inner classes that implement various text editing commands. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/StyledEditorKit.AlignmentAction.html">AlignmentAction</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/StyledEditorKit.BoldAction.html">BoldAction</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/StyledEditorKit.FontFamilyAction.html">FontFamilyAction</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/StyledEditorKit.FontSizeAction.html">FontSizeAction</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/StyledEditorKit.ForegroundAction.html">ForegroundAction</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/StyledEditorKit.ItalicAction.html">ItalicAction</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/StyledEditorKit.StyledTextAction.html">StyledTextAction</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/StyledEditorKit.UnderlineAction.html">UnderlineAction</a> <br><em>(in <code>StyledEditorKit</code>)</em> </td> <td>Inner classes that implement various editing commands for styled text. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/text/JTextComponent.html#getActions()">Action[] getActions()</a> <br><em>(in <code>JTextComponent</code>)</em> </td> <td>Get the actions supported by this component. This method gets the array of actions from the editor kit if one is used by the component. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#getInputMap()">InputMap getInputMap()</a> <br><em>(in <code>JComponent</code>)</em> </td> <td>Gets the input map that binds key strokes to actions. See <a href="generaltext.html#bindingkeystrokes">Associating Text Actions with Key Strokes</a>. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/InputMap.html#put(javax.swing.KeyStroke, java.lang.Object)">void put(KeyStroke, Object)</a> <br><em>(in <code>InputMap</code>)</em> </td> <td>Binds the specified key to the specified action. You generally specify the action by its name, which for standard editing actions is represented by a string constant such as <code>DefaultEditorKit.backwardAction</code>. </td> </tr></table><p><table border=1> <caption><a name="docclasses"> Classes and Interfaces that Represent Documents</a></caption>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -