所有类


javax.swing
类 JEditorPane

java.lang.Object
  继承者 java.awt.Component
      继承者 java.awt.Container
          继承者 javax.swing.JComponent
              继承者 javax.swing.text.JTextComponent
                  继承者 javax.swing.JEditorPane
所有已实现的接口:
ImageObserver, MenuContainer, Serializable, Accessible, Scrollable
直接已知子类:
JTextPane

public class JEditorPane
   
   
   
   
   
extends JTextComponent

可编辑各种内容的文本组件。有关基础知识信息和使用编辑器窗格的示例,请参阅《The Java Tutorial》 中的 Using Text Components 一节。

此组件使用 EditorKit 的实现来完成其操作。对于给予它的各种内容,它能有效地将其形态变换为适当的文本编辑器种类。该编辑器在任意给定时间的内容类型都由当前已安装的 EditorKit 确定。如果将内容设置为新的 URL,则使用其类型来确定加载该内容所应使用的 EditorKit

默认情况下,已知下列内容类型:

text/plain
纯文本,即无法识别给定的类型时所使用的默认值。在此情况下使用的工具包是 DefaultEditorKit 的扩展,可生成有换行的纯文本视图。
text/html
HTML 文本。在此情况下使用的工具包是类 javax.swing.text.html.HTMLEditorKit,支持 HTML 3.2。
text/rtf
RTF 文本。在此情况下使用的工具包是类 javax.swing.text.rtf.RTFEditorKit,它提供了对多样化文本格式 (Rich Text Format) 的有限支持。

有多种方式可将内容加载到此组件中。

  1. 可使用 setText 方法来初始化字符串组件。在这种情况下,将使用当前的 EditorKit,且此类型为期望的内容类型。
  2. 可使用 read 方法来初始化 Reader 组件。注意,如果内容类型为 HTML,那么只有使用了 <base> 标记,或者设置了 HTMLDocument 上的 Base 属性时才能解析相关的引用(例如对于类似图像等内容)。在这种情况下,将使用当前的 EditorKit,且此类型为期望的内容类型。
  3. 可使用 setPage 方法来初始化 URL 组件。在这种情况下,将根据该 URL 来确定内容类型,并且设置为该内容类型所注册的 EditorKit

某些内容类型可能通过生成超链接事件来提供对超链接的支持。如果 JEditorPane不可编辑的(已调用了 JEditorPane.setEditable(false);),则 HTML EditorKit 将生成超链接事件。如果文档中嵌入了 HTML frame,则典型的响应是更改当前文档的某一部分。以下代码片段是一种可能的超链接侦听器实现,它特殊对待 HTML frame 事件,并且简单地显示其他所有已激活的超链接。


     class Hyperactive implements HyperlinkListener {
 
         public void hyperlinkUpdate(HyperlinkEvent e) {
             if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                 JEditorPane pane = (JEditorPane) e.getSource();
                 if (e instanceof HTMLFrameHyperlinkEvent) {
                     HTMLFrameHyperlinkEvent  evt = (HTMLFrameHyperlinkEvent)e;
                     HTMLDocument doc = (HTMLDocument)pane.getDocument();
                     doc.processHTMLFrameHyperlinkEvent(evt);
                 } else {
                     try {
                         pane.setPage(e.getURL());
                     } catch (Throwable t) {
                         t.printStackTrace();
                     }
                 }
             }
         }
     }

 

有关自定义如何呈现 text/html 的信息,请参阅 W3C_LENGTH_UNITSHONOR_DISPLAY_PROPERTIES

某些文档中与文化相关的信息是通过一种称为字符编码的机制来处理的。字符编码是字符集的成员(字母、象形文字、数字、符号或控制函数)和具体数字代码值的明确映射关系。它表示了文件的存储方式。字符编码有 ISO-8859-1、ISO-8859-5、Shift-jis、Euc-jp 和 UTF-8。将文件传递给用户代理 (JEditorPane) 时,该文件被转换为文档字符集 (ISO-10646 aka Unicode)。

有多种方式可获得使用 JEditorPane 时所用的字符集映射关系。

  1. 一种方式是将字符集指定为 MIME 类型的参数。通过调用 setContentType 方法来建立此方式。如果通过 setPage 方法加载内容,则其内容类型已根据 URL 的规范进行了设置。如果直接加载文件,则期望在加载前就已设置内容类型。
  2. 指定字符集的另一种方式是在该文档内指定。这需要在确定所需的字符集之前读取该文档。要处理此情况,需要 EditorKit.read 操作抛出将要被捕获的 ChangedCharSetException。然后使用新的 Reader 重新开始该 read 操作,该 Reader 使用 ChangedCharSetException(这是一个 IOException)中所指定的字符集。

换行
有关如何处理换行的讨论,请参阅 DefaultEditorKit

警告:此类的序列化对象与以后的 Swing 版本不兼容。当前序列化支持适用于短期存储,或适用于在运行相同 Swing 版本的应用程序之间进行 RMI(Remote Method Invocation,远程方法调用)。从 1.4 版本开始,已在 java.beans 包中添加了支持所有 JavaBeansTM 长期存储的功能。请参见 XMLEncoder


嵌套类摘要
protected  classJEditorPane.AccessibleJEditorPane
          此类实现对 JEditorPane 类的可访问性支持。
protected  classJEditorPane.AccessibleJEditorPaneHTML
          此类提供了对 AccessibleHypertext 的支持,并且用于这类实例中:在此 JEditorPane 中安装的 EditorKit 是一个 HTMLEditorKit 实例。
protected  classJEditorPane.JEditorPaneAccessibleHypertextSupport
          AccessibleJEditorPaneHTML.getAccessibleText 返回的内容。
 
从类 javax.swing.text.JTextComponent 继承的嵌套类/接口
JTextComponent.AccessibleJTextComponent, JTextComponent.KeyBinding
 
从类 javax.swing.JComponent 继承的嵌套类/接口
JComponent.AccessibleJComponent
 
从类 java.awt.Container 继承的嵌套类/接口
Container.AccessibleAWTContainer
 
从类 java.awt.Component 继承的嵌套类/接口
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
字段摘要
static StringHONOR_DISPLAY_PROPERTIES
          用于客户机属性的关键字,指示如果在样式化文本中未指定字体或前景色,那么是否使用该组件的默认字体和前景色。
static StringW3C_LENGTH_UNITS
          用于客户机属性的关键字,指示是否为 html 呈现使用 w3c 兼容的长度单位。
 
从类 javax.swing.text.JTextComponent 继承的字段
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
 
从类 javax.swing.JComponent 继承的字段
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
从类 java.awt.Component 继承的字段
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
从接口 java.awt.image.ImageObserver 继承的字段
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
构造方法摘要
JEditorPane()
          创建一个新的 JEditorPane
JEditorPane(String url)
          根据包含 URL 规范的字符串创建一个 JEditorPane
JEditorPane(String type, String text)
          创建一个已初始化为给定文件的 JEditorPane
JEditorPane(URL initialPage)
          根据用作输入的指定 URL 创建一个 JEditorPane
 
方法摘要
 voidaddHyperlinkListener(HyperlinkListener listener)
          为了通知所有的更改(例如选中和进入某个链接)而添加一个超链接侦听器。
protected  EditorKitcreateDefaultEditorKit()
          首次创建组件时,创建默认的编辑器工具包 (PlainEditorKit)。
static EditorKitcreateEditorKitForContentType(String type)
          根据默认的编辑器工具包注册表为给定的类型创建处理程序。
 voidfireHyperlinkUpdate(HyperlinkEvent e)
          通知因对此事件类型的通知感兴趣而已注册的所有侦听器。
 AccessibleContextgetAccessibleContext()
          获取与此 JEditorPane 关联的 AccessibleContext。
 StringgetContentType()
          获得设置此编辑器当前要处理的内容类型。
 EditorKitgetEditorKit()
          获取用于处理内容的、当前已安装的工具包。
static StringgetEditorKitClassNameForContentType(String type)
          返回当前向类型 type 所注册的 EditorKit 类名。
 EditorKitgetEditorKitForContentType(String type)
          获取用于给定内容类型的编辑器工具包。
 HyperlinkListener[]getHyperlinkListeners()
          返回使用 addHyperlinkListener() 添加到此 JEditorPane 的所有 HyperLinkListener 的数组。
 URLgetPage()
          获得当前正在显示的 URL。
 DimensiongetPreferredSize()
          返回该 JEditorPane 的首选大小。
 booleangetScrollableTracksViewportHeight()
          如果某个视口始终应该强迫此 Scrollable 的高度符合该视口的高度,则返回 true。
 booleangetScrollableTracksViewportWidth()
          如果某个视口始终应该强迫此 Scrollable 的宽度符合该视口的宽度,则返回 true。
protected  InputStreamgetStream(URL page)
          获取给定 URL 的流,该 URL 是将要由 setPage 方法加载的。
 StringgetText()
          根据此编辑器的内容类型返回此 TextComponent 中所包含的文本。
 StringgetUIClassID()
          获得该 UI 的类 ID。
protected  StringparamString()
          返回此 JEditorPane 的字符串表示形式。
 voidread(InputStream in, Object desc)
          此方法根据流进行初始化。
static voidregisterEditorKitForContentType(String type, String classname)
          建立 typeclassname 的默认绑定。
static voidregisterEditorKitForContentType(String type, String classname, ClassLoader loader)
          建立 typeclassname 的默认绑定。
 voidremoveHyperlinkListener(HyperlinkListener listener)
          移除超链接侦听器。
 voidreplaceSelection(String content)
          用由给定字符串表示的新内容替换当前选中的内容。
 voidscrollToReference(String reference)
          将视图滚动到给定的参考位置(也就是正在显示的 URL 的 UL.getRef 方法所返回的值)。
 voidsetContentType(String type)
          设置此编辑器所处理的内容类型。
 voidsetEditorKit(EditorKit kit)
          设置当前为处理内容而安装的工具包。
 voidsetEditorKitForContentType(String type, EditorKit k)
          直接设置用于给定类型的编辑器工具包。
 voidsetPage(String url)
          设置当前要显示的 URL。
 voidsetPage(URL page)
          设置当前要显示的 URL。
 voidsetText(String t)
          将此 TextComponent 的文本设置为指定内容,预期以此编辑器的内容类型格式提供该内容。
 
从类 javax.swing.text.JTextComponent 继承的方法
addCaretListener, addInputMethodListener, addKeymap, copy, cut, fireCaretUpdate, getActions, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getScrollableBlockIncrement, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, moveCaretPosition, paste, processInputMethodEvent, read, removeCaretListener, removeKeymap, removeNotify, select, selectAll, setCaret, setCaretColor, setCaretPosition, setComponentOrientation, setDisabledTextColor, setDocument, setDragEnabled, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setUI, updateUI, viewToModel, write
 
从类 javax.swing.JComponent 继承的方法
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
从类 java.awt.Container 继承的方法
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
从类 java.awt.Component 继承的方法
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

字段详细信息

W3C_LENGTH_UNITS

public static final String W3C_LENGTH_UNITS
用于客户机属性的关键字,指示是否为 html 呈现使用 w3c 兼容的长度单位。

默认不启用此属性;要启用它,可将带有此名称的客户机 property 设置为 Boolean.TRUE

从以下版本开始:
1.5
另请参见:
常量字段值

HONOR_DISPLAY_PROPERTIES

public static final String HONOR_DISPLAY_PROPERTIES
用于客户机属性的关键字,指示如果在样式化文本中未指定字体或前景色,那么是否使用该组件的默认字体和前景色。

根据外观的不同,该默认值有所不同;要启用该属性,可将带有此名称的客户机 property 设置为 Boolean.TRUE

从以下版本开始:
1.5
另请参见:
常量字段值
构造方法详细信息

JEditorPane

public JEditorPane()
创建一个新的 JEditorPane。文档模型被设置为 null


JEditorPane

public JEditorPane(URL initialPage)
            throws IOException
根据用作输入的指定 URL 创建一个 JEditorPane

参数:
initialPage - URL
抛出:
IOException - 如果该 URL 为 null 或者无法访问

JEditorPane

public JEditorPane(String url)
            throws IOException
根据包含 URL 规范的字符串创建一个 JEditorPane

参数:
url - URL
抛出:
IOException - 如果该 URL 为 null 或者无法访问

JEditorPane

public JEditorPane(String type,
                   String text)
创建一个已初始化为给定文件的 JEditorPane。这是调用 setContentTypesetText 方法的便捷构造方法。

参数:
type - 给定文本的 mime 类型
text - 初始化所使用的文本;可能为 null
抛出:
NullPointerException - 如果 type 参数为 null
方法详细信息

addHyperlinkListener

public void addHyperlinkListener(HyperlinkListener listener)
为了通知所有的更改(例如选中和进入某个链接)而添加一个超链接侦听器。

参数:
listener - 侦听器

removeHyperlinkListener

public void removeHyperlinkListener(HyperlinkListener listener)
移除超链接侦听器。

参数:
listener - 侦听器

getHyperlinkListeners

public HyperlinkListener[] getHyperlinkListeners()
返回使用 addHyperlinkListener() 添加到此 JEditorPane 的所有 HyperLinkListener 的数组。

返回:
已添加的所有 HyperLinkListener,如果尚未添加侦听器,则返回一个空数组
从以下版本开始:
1.4