⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 editorpane.html

📁 jsf、swing的官方指南
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<ul><li> <a href="#editorpane">Using an Editor Pane to Display Text from a URL</a><li> <a href="#recap">Editor Panes vs. Text Panes</a><li> <a href="#textpane">An Example of Using a Text Pane</a><li> <a href="#api">The Editor Pane and Text Pane API</a><li> <a href="#eg">Examples that Use Editor Panes and Text Panes</a></ul></blockquote><a name="editorpane"><h3>Using an Editor Pane to Display Text from a URL</h3></a><blockquote>One task that you can accomplishwithout knowing anything about the Swing text systemis displaying text from a URL.Here's the codefrom <a class="SourceLink" target="_blank" href="examples/TextSamplerDemo.java"><code>TextSamplerDemo.java</code></a> that creates an uneditable editor panethat displays text formatted with HTML tags:<blockquote><pre>JEditorPane editorPane = new JEditorPane();editorPane.setEditable(false);java.net.URL helpURL = TextSamplerDemo.class.getResource(                                "TextSamplerDemoHelp.html");if (helpURL != null) {    try {        editorPane.setPage(helpURL);    } catch (IOException e) {        System.err.println("Attempted to read a bad URL: " + helpURL);    }} else {    System.err.println("Couldn't find file: TextSamplerDemoHelp.html");}//Put the editor pane in a scroll pane.JScrollPane editorScrollPane = new JScrollPane(editorPane);editorScrollPane.setVerticalScrollBarPolicy(                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);editorScrollPane.setPreferredSize(new Dimension(250, 145));editorScrollPane.setMinimumSize(new Dimension(10, 10));</pre></blockquote>The code uses the default constructor to create theeditor pane, then calls <code>setEditable(false)</code>so the user cannot edit the text.Next, the code creates the <code>URL</code> object,and calls the <code>setPage</code> method with it.<p>The <code>setPage</code> method opens the resourcepointed to by the URL andfigures out the format of the text(which in the example is HTML).If the text format is known,the editor pane initializes itselfwith the text found at the URL.A standard editor pane can understandplain text, HTML, and RTF.	Note that the page might be loaded asynchronously,	which keeps the GUI responsive	but means that you shouldn't count on the	data being completely loaded	after the call to <code>setPage</code> returns.</blockquote><h3><a name="recap">Editor Panes vs. Text Panes</a></h3><blockquote>For most uses of editor panes and text panes,you need to understand the text system,which is described in<a href="generaltext.html">Text Component Features</a>.Several facts about editor panes and text panesare sprinkled throughout that section.Here we list the facts again, to collect them in one placeand to provide a bit more detail.The information here should help you understand the differencesbetween editor panes and text panes, and when to use which.<ul><li> An editor pane or a text pane     can easily be loaded with text from a URL     using the <code>setPage</code> method.     The <code>JEditorPane</code> class also provides constructors     that let you initialize an editor pane from a URL.     <code>JTextPane</code> has no such constructors.     See     <a href="#editorpane">     Using an Editor Pane to Display Text from a URL</a>     for an example of using this feature     to load an uneditable editor pane with HTML-formatted text.<p>     Be aware that the document and editor kit     might change when using the <code>setPage</code> method.     For example,     if an editor pane contains plain text (the default),     and you load it with HTML,     the document will change to an <code>HTMLDocument</code> instance     and the editor kit will change to an <code>HTMLEditorKit</code> instance.     If your program uses the <code>setPage</code> method,     make sure the code adjusts for possible changes     to the pane's document and editor kit instances     (re-register document listeners on the new document, and so on).<p><li> Editor panes, by default,     know how to read, write, and edit plain, HTML, and RTF text.     Text panes inherit this capability     but impose certain limitations.     A text pane insists that its document     implement the <code>StyledDocument</code> interface.     <code>HTMLDocument</code> and <code>RTFDocument</code>     are both <code>StyledDocuments</code>     so HTML and RTF work as expected within a text pane.     If you load a text pane with plain text though,     the text pane's document is not a <code>PlainDocument</code>     as you might expect,     but a <code>DefaultStyledDocument</code>.     <p><li> To support a custom text format,     implement an editor kit that can read, write, and edit     text of that format.     Then call the     <code>registerEditorKitForContentType</code> method     to register your kit with the <code>JEditorPane</code> class.     By registering an editor kit in this way,     all editor panes and text panes in your program will be able     to read, write, and edit the new format.     However, if the new editor kit is not a <code>StyledEditorKit</code>,     text panes will not support the new format.<p><li> As mentioned previously,     a text pane requires its document      to implement the <code>StyledDocument</code> interface.     The Swing text package provides     a default implementation of this interface,     <code>DefaultStyledDocument</code>,     which is the document text panes use by default.     A text pane also requires that its editor kit be an instance of a     <code>StyledEditorKit</code> (or a subclass).     Be aware that the <code>read</code> and     <code>write</code> methods for <code>StyleEditorKit</code>     write plain text.<p><li> Through its styled document and styled editor kit,     text panes provide support for named styles and logical styles.     The <code>JTextPane</code> class itself contains     many methods for working with styles that     simply call methods in its document or editor kit.<p><li> Through the API provided in the <code>JTextPane</code> class,     you can embed images and components in a text pane.     You can embed images in an editor pane, too,     but only by including the images in an HTML or RTF file.</ul></blockquote><a name="textpane"><h3>An Example of Using a Text Pane</h3></a><blockquote>Here's code from <code>TextSamplerDemo</code>that creates and initializes a text pane.<blockquote><pre>String[] initString =	{ /* ... <em> fill array with initial text </em> ... */ };String[] initStyles =	{ /* ... <em> fill array with names of styles </em> ... */ };JTextPane textPane = new JTextPane();StyledDocument doc = textPane.getStyledDocument();addStylesToDocument(doc);//Load the text pane with styled text.try {    for (int i=0; i < initString.length; i++) {	doc.insertString(doc.getLength(), initString[i],			 doc.getStyle(initStyles[i]));    }} catch (BadLocationException ble) {    System.err.println("Couldn't insert initial text into text pane.");}</pre></blockquote>Briefly, this code hard-codes the initial text into an arrayand creates and hard-codes several <em>styles</em> &#151;objects thatrepresent different paragraph and character formats &#151;into another array.Next, the code loops over the arrays,inserts the text into the text pane,and specifies the style to use for the inserted text.<p>Although this makes for an interesting exampleand concisely shows off several features of <code>JTextPane</code>,"real-world" programs aren't likely to initializea text pane this way.Instead, the program would use a text pane to save out a document,which would then be used to initialize the text pane.</blockquote><h3><a name="api">The Editor Pane and Text Pane API</a></h3><blockquote>This section lists a bit of the APIrelated to text and editor panes.Many of the most useful methods for <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JEditorPane.html">JEditorPane</a> and its subclass<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextPane.html">JTextPane</a> are inherited from <code>JTextComponent</code>.You can find the API tables for<code>JTextComponent</code> in <a href="textapi.html">The Text Component API</a>.Also see<a href="jcomponent.html">The JComponent Class</a>,which describes the API inherited from <code>JComponent</code>.<p><table border=1><caption><a name="loading">JEditorPane API for Displaying Text from a URL</a></caption><tr><th>Method or Constructor</th><th>Description</th></tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JEditorPane.html#JEditorPane(java.net.URL)">JEditorPane(URL)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JEditorPane.html#JEditorPane(java.lang.String)">JEditorPane(String)</a>    </td>    <td>Create an editor pane loaded with the text        at the specified URL.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JEditorPane.html#setPage(java.net.URL)">setPage(URL)</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JEditorPane.html#setPage(java.lang.String)">setPage(String)</a>    </td>    <td>Load an editor pane (or text pane) with the text        at the specified URL.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JEditorPane.html#getPage()">URL getPage()</a>    </td>    <td>Get the URL for the editor pane's (or text pane's) current page.    </td>  </tr></table><p><table border=1><caption><a name="loading">JTextPane API</a></caption><tr><th>Method or Constructor</th><th>Description</th></tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextPane.html#JTextPane()">JTextPane()</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextPane.html#JTextPane(javax.swing.text.StyledDocument)">JTextPane(StyledDocument)</a>    </td>    <td>Create a text pane.	The optional argument specifies the text pane's model.    </td>  </tr>  <tr>    <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextPane.html#getStyledDocument()">StyledDocument getStyledDocument</a>    <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JTextPane.html#setStyledDocument(javax.swing.text.StyledDocument)">setStyledDocument(StyledDocument)</a>    </td>    <td>Get or set the text pane's model.    </td>  </tr></table></blockquote><h3><a name="eg">Examples that Use Text Panes and Editor Panes</a></h3><blockquote>To get started with text,you might want to run these programs and examinetheir code to find something similarto what you want to do.<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#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#TextComponentDemo">    TextComponentDemo</a></td><td valign=top><a href="generaltext.html">Text Component Features</a></td><td valign=top>Provides a customized text pane.    Illustrates many text component features,    such as undo and redo,     document filters,    document listeners,    caret change listeners,    and associating editing actions     with menus and key strokes.</td></tr><tr><td valign=top><a href="examples/index.html#TreeDemo">    TreeDemo</a><td valign=top><a href="tree.html">How to Use Trees</a></td><td valign=top>Uses an editor pane to display help    loaded from an HTML file.</td></tr></table>        </blockquote>        <div class=NavBit>            <a target=_top href=dialog.html>&laquo; Previous</a>            &bull;            <a target=_top href=../TOC.html>Trail</a>            &bull;            <a target=_top href=filechooser.html>Next &raquo;</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 Make Dialogs        <br><b>Next page:</b> How to Use File Choosers    </div>    </body></html> 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -