The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
Two Swing classes support styled text: 两个Swing类支持样式化文本:JEditorPane
and its subclass JTextPane
. JEditorPane
及其子类JTextPane
。The JEditorPane
class is the foundation for Swing's styled text components and provides a mechanism through which you can add support for custom text formats. JEditorPane
类是Swing样式化文本组件的基础,并提供了一种机制,通过该机制可以添加对自定义文本格式的支持。If you want unstyled text, use a text area instead.如果需要未设置样式的文本,请使用文本区域。
You can see an editor pane and a text pane in use by running TextSamplerDemo. 通过运行TextSamplerDemo,您可以看到正在使用的编辑器窗格和文本窗格。Here is a picture of the 下面是TextSamplerDemo
example.TextSamplerDemo
示例的图片。
Click the Launch button to run TextSamplerDemo using Java™ Web Start (download JDK 7 or later). 单击启动按钮,使用Java™Web启动运行TextSamplerDemo(下载JDK 7或更高版本)。Alternatively, to compile and run the example yourself, consult the example index.或者,要自己编译和运行示例,请参考示例索引。
The TextSamplerDemo
example barely begins to demonstrate the capabilities of editor panes and text panes. TextSamplerDemo
示例刚刚开始演示编辑器窗格和文本窗格的功能。However, the top right editor pane illustrates a handy, easy-to-use feature: it displays uneditable help information loaded from a URL. 然而,右上角的编辑器窗格展示了一个方便易用的功能:它显示从URL加载的不可编辑的帮助信息。The text pane at the lower right demonstrates that you can easily embed images and even components directly into text panes.右下角的文本窗格表明,您可以轻松地将图像甚至组件直接嵌入到文本窗格中。
If you need a fully-fledged help system, take a look at the javahelp project.如果您需要一个成熟的帮助系统,请查看javahelp项目。
The Swing text API is powerful and immense, and we could devote an entire book just to using editor panes and text panes. Swing text API功能强大,功能强大,我们可以花一整本书来讨论如何使用编辑器窗格和文本窗格。This section introduces their capabilities, offers hints on which one you might want to use, and points to other sources of information.本节介绍了它们的功能,提供了您可能希望使用哪种功能的提示,并指出了其他信息来源。
One task that you can accomplish without knowing anything about the Swing text system is displaying text from a URL. 您可以在不了解Swing文本系统的情况下完成的一项任务是显示URL中的文本。Here is the code from 下面是TextSamplerDemo.java
that creates an uneditable editor pane that displays text formatted with HTML tags.TextSamplerDemo.java
中的代码,该代码创建了一个不可编辑的编辑器窗格,该窗格显示用HTML标记格式化的文本。
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));
The code uses the default constructor to create the editor pane, then calls 代码使用默认构造函数创建编辑器窗格,然后调用setEditable(false)
so the user cannot edit the text. setEditable(false)
,因此用户无法编辑文本。Next, the code creates the 接下来,代码创建URL
object, and calls the setPage
method with it.URL
对象,并使用它调用setPage
方法。
The setPage
method opens the resource pointed to by the URL and figures out the format of the text (which is HTML in the example). setPage
方法打开URL指向的资源,并计算文本的格式(在本例中为HTML)。If the text format is known, the editor pane initializes itself with the text found at the URL. 如果文本格式已知,则编辑器窗格将使用URL中的文本进行初始化。A standard editor pane can understand plain text, HTML, and RTF. 标准编辑器窗格可以理解纯文本、HTML和RTF。Note that the page might be loaded asynchronously, which keeps the GUI responsive but means that you should not count on the data being completely loaded after the call to 请注意,页面可能是异步加载的,这会使GUI保持响应,但这意味着您不应指望在调用setPage
returns.setPage
返回后完全加载数据。
In order to use editor panes and text panes, you need to understand the text system, which is described in Text Component Features. 为了使用编辑器窗格和文本窗格,您需要了解文本系统,这在文本组件功能中有描述。Several facts about editor panes and text panes are scattered throughout that section. 关于编辑器窗格和文本窗格的几个事实分散在该部分中。Here we list the facts again and provide a bit more detail. 这里我们再次列出事实,并提供更多细节。The information here should help you understand the differences between editor panes and text panes, and when to use which.此处的信息将帮助您理解编辑器窗格和文本窗格之间的区别,以及何时使用它们。
setPage
method. setPage
方法从URL轻松加载文本。JEditorPane
class also provides constructors that let you initialize an editor pane from a URL. JEditorPane
类还提供了构造函数,可用于从URL初始化编辑器窗格。JTextPane
class has no such constructors. JTextPane
类没有这样的构造函数。Be aware that the document and editor kit might change when using the 请注意,使用setPage
method. setPage
方法时,文档和编辑器工具包可能会发生更改。For example, if an editor pane contains plain text (the default), and you load it with HTML, the document will change to an 例如,如果编辑器窗格包含纯文本(默认值),并且您使用HTML加载它,则文档将更改为HTMLDocument
instance and the editor kit will change to an HTMLEditorKit
instance. HTMLDocument
实例,编辑器工具包将更改为HTMLEditorKit
实例。If your program uses the 如果您的程序使用setPage
method, make sure you adjust your code for possible changes to the pane's document and editor kit instances (re-register document listeners on the new document, and so on).setPage
方法,请确保调整代码以应对窗格的文档和编辑器工具包实例可能发生的更改(在新文档上重新注册文档侦听器,等等)。
StyledDocument
interface. StyledDocument
接口。HTMLDocument
and RTFDocument
are both StyledDocuments
so HTML and RTF work as expected within a text pane. HTMLDocument
和RTFDocument
都是StyledDocument
,因此HTML和RTF在文本窗格中按预期工作。PlainDocument
as you might expect, but a DefaultStyledDocument
.PlainDocument
,而是DefaultStyledDocument
。registerEditorKitForContentType
method to register your kit with the JEditorPane
class. registerEditorKitForContentType
方法,向JEditorPane
类注册工具包。StyledEditorKit
, text panes will not support the new format.StyledEditor
工具包,则文本窗格将不支持新格式。StyledDocument
interface. StyledDocument
接口。DefaultStyledDocument
, which is the document that text panes use by default. DefaultStyledDocument
,它是文本窗格默认使用的文档。StyledEditorKit
(or a subclass). StyledEditor
工具包(或子类)的实例。read
and write
methods for StyleEditorKit
work with plain text.StyleEditorIt
的read
方法和write
方法可用于纯文本。JTextPane
class itself contains many methods for working with styles that simply call methods in its document or editor kit.JTextPane
类本身包含许多用于处理样式的方法,这些样式只调用其文档或编辑器工具包中的方法。JTextPane
class, you can embed images and components in a text pane. JTextPane
类中提供的API,您可以在文本窗格中嵌入图像和组件。Here is the code from the 下面是TextSamplerDemo
example that creates and initializes a text pane.TextSamplerDemo
示例中创建和初始化文本窗格的代码。
String[] initString = { /* ... fill array with initial text ... */ }; String[] initStyles = { /* ... fill array with names of styles ... */ }; 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."); }
Briefly, this code hard-codes the initial text into an array and creates and hard-codes several styles objects that represent different paragraph and character formats 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.接下来,代码在数组上循环,将文本插入文本窗格,并指定用于插入文本的样式。
Although this is an interesting example that concisely demonstrates several features of 尽管这是一个有趣的示例,简要地展示了JTextPane
, "real-world" programs aren't likely to initialize a text pane this way. JTextPane
的几个功能,“现实世界”程序不太可能以这种方式初始化文本窗格。Instead, a program would use an editor pane to save a document which would then be used to initialize the text pane.相反,程序将使用编辑器窗格来保存文档,然后该文档将用于初始化文本窗格。
This section lists some of the API related to text and editor panes. 本节列出了一些与文本和编辑器窗格相关的API。Many of the most useful methods for JEditorPane and its subclass JTextPane are inherited from the JTextComponent
class. JEditorPane
及其子类JTextPane
的许多最有用的方法都是从JTextComponent
类继承的。You can find the API tables for 您可以在文本组件API中找到JTextComponent
in The Text Component API. JTextComponent
的API表。Also see The JComponent Class, which describes the API inherited from 另请参见JComponent
.JComponent
类,它描述了从jComponent
继承的API。
JEditorPane(URL) JEditorPane(String)
|
|
setPage(URL) setPage(String) |
|
URL getPage() |
JTextPane() JTextPane(StyledDocument) |
|
StyledDocument getStyledDocument setStyledDocument(StyledDocument) |
To begin using text, you might want to run these programs and examine their code to find something similar to what you want to do.要开始使用文本,您可能需要运行这些程序并检查它们的代码,以找到与您想要做的类似的内容。
TextSamplerDemo |
||
TextComponentDemo |
||
TreeDemo |
Learn to edit HTML text in JavaFX with the Using JavaFX UI Controls: HTML Editor tutorial.学习使用JavaFXUI控件:HTML编辑器教程在JavaFX中编辑HTML文本。