JavaTM 2 Platform
Standard Ed. 6

javax.swing
类 JTextPane

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

public class JTextPane
extends JEditorPane

可以用以图形方式表示的属性来标记的文本组件。有关使用文本窗格的 how-to 信息和示例,请参阅 The Java Tutorial 中的 Using Text Components 一节。

此组件模仿了由一串字符级属性组成的段落。每个段落都可能有一个附加的逻辑样式,如果默认属性没有通过段落上的属性集或一串字符重写,则此样式包含要使用的默认属性。组件和图像可以嵌入在文本流中。

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

警告:Swing 不是线程安全的。有关更多信息,请参阅 Swing's Threading Policy

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

另请参见:
StyledEditorKit

嵌套类摘要
 
从类 javax.swing.JEditorPane 继承的嵌套类/接口
JEditorPane.AccessibleJEditorPane, JEditorPane.AccessibleJEditorPaneHTML, JEditorPane.JEditorPaneAccessibleHypertextSupport
 
从类 javax.swing.text.JTextComponent 继承的嵌套类/接口
JTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, JTextComponent.KeyBinding
 
从类 javax.swing.JComponent 继承的嵌套类/接口
JComponent.AccessibleJComponent
 
从类 java.awt.Container 继承的嵌套类/接口
Container.AccessibleAWTContainer
 
从类 java.awt.Component 继承的嵌套类/接口
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
字段摘要
 
从类 javax.swing.JEditorPane 继承的字段
HONOR_DISPLAY_PROPERTIES, W3C_LENGTH_UNITS
 
从类 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
 
构造方法摘要
JTextPane()
          创建一个新的 JTextPane
JTextPane(StyledDocument doc)
          创建具有指定文档模型的新 JTextPane
 
方法摘要
 Style addStyle(String nm, Style parent)
          将一个新样式添加到逻辑样式层次结构中。
protected  EditorKit createDefaultEditorKit()
          创建默认使用的 EditorKit
 AttributeSet getCharacterAttributes()
          获取在当前插入符号位置的有效字符属性,或者 null
 MutableAttributeSet getInputAttributes()
          获取窗格的输入属性。
 Style getLogicalStyle()
          获取分配给由当前插入符号位置表示的段落的逻辑样式,或者 null
 AttributeSet getParagraphAttributes()
          获取插入符号位置的当前有效段落属性,如果没有,则返回 null
 Style getStyle(String nm)
          获取以前添加的已命名的非 null 样式。
 StyledDocument getStyledDocument()
          获取与编辑器关联的模型。
protected  StyledEditorKit getStyledEditorKit()
          获取编辑器工具包。
 String getUIClassID()
          返回 UI 的类 ID。
 void insertComponent(Component c)
          将一个组件插入到文档中,以替换当前选择的内容。
 void insertIcon(Icon g)
          将一个图标插入文档中,以替换当前选择的内容。
protected  String paramString()
          返回此 JTextPane 的字符串表示形式。
 void removeStyle(String nm)
          移除以前添加到文档中的已命名的非 null 样式。
 void replaceSelection(String content)
          用给定字符串所表示的新内容替换当前选择的内容。
 void setCharacterAttributes(AttributeSet attr, boolean replace)
          将给定属性应用于字符内容。
 void setDocument(Document doc)
          将编辑器与一个文本文档关联。
 void setEditorKit(EditorKit kit)
          设置当前安装的用于内容处理的工具包。
 void setLogicalStyle(Style s)
          设置在当前插入符号位置用于段落的逻辑样式。
 void setParagraphAttributes(AttributeSet attr, boolean replace)
          将给定属性应用于段落。
 void setStyledDocument(StyledDocument doc)
          将编辑器与一个文本文档关联。
 
从类 javax.swing.JEditorPane 继承的方法
addHyperlinkListener, createEditorKitForContentType, fireHyperlinkUpdate, getAccessibleContext, getContentType, getEditorKit, getEditorKitClassNameForContentType, getEditorKitForContentType, getHyperlinkListeners, getPage, getPreferredSize, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getStream, getText, read, registerEditorKitForContentType, registerEditorKitForContentType, removeHyperlinkListener, scrollToReference, setContentType, setEditorKitForContentType, setPage, setPage, setText
 
从类 javax.swing.text.JTextComponent 继承的方法
addCaretListener, addInputMethodListener, addKeymap, copy, cut, fireCaretUpdate, getActions, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getDropLocation, getDropMode, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getPrintable, getScrollableBlockIncrement, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, moveCaretPosition, paste, print, print, print, processInputMethodEvent, read, removeCaretListener, removeKeymap, removeNotify, select, selectAll, setCaret, setCaretColor, setCaretPosition, setComponentOrientation, setDisabledTextColor, setDragEnabled, setDropMode, 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, getBaseline, getBaselineResizeBehavior, 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, isPaintingForPrint, 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
 

构造方法详细信息

JTextPane

public JTextPane()
创建一个新的 JTextPane。创建并设置 StyledEditorKit 的一个新实例,并将文档模型设置为 null


JTextPane

public JTextPane(StyledDocument doc)
创建具有指定文档模型的新 JTextPane。创建并设置 javax.swing.text.StyledEditorKit 的一个新实例。

参数:
doc - 文档模型
方法详细信息

getUIClassID

public String getUIClassID()
返回 UI 的类 ID。

覆盖:
JEditorPane 中的 getUIClassID
返回:
字符串 "TextPaneUI"
另请参见:
JComponent.getUIClassID(), UIDefaults.getUI(javax.swing.JComponent)

setDocument

public void setDocument(Document doc)
将编辑器与一个文本文档关联。这必须是一个 StyledDocument

覆盖:
JTextComponent 中的 setDocument
参数:
doc - 要显示/编辑的文档
抛出:
IllegalArgumentException - 如果 doc 不能收缩为一个 StyledDocument,它是此文本组件所要求的模型类型
另请参见:
JTextComponent.getDocument()

setStyledDocument

public void setStyledDocument(StyledDocument doc)
将编辑器与一个文本文档关联。当前注册的工厂用来生成文档的一个视图,此视图由编辑器来显示。

参数:
doc - 要显示/编辑的文档

getStyledDocument

public StyledDocument getStyledDocument()
获取与编辑器关联的模型。

返回:
模型

replaceSelection

public void replaceSelection(String content)
用给定字符串所表示的新内容替换当前选择的内容。如果没有选择的内容,则该操作插入给定的文本。如果没有替换文本,则该操作移除当前选择的内容。替换文本将具有针对插入点的输入当前定义的属性。如果文档不是可编辑的,则发出蜂鸣并返回。

虽然大多数 Swing 方法不是线程安全的,但此方法是线程安全的。有关更多信息,请参阅 How to Use Threads

覆盖:
JEditorPane 中的 replaceSelection
参数:
content - 替换选定内容的内容

insertComponent

public void insertComponent(Component c)
将一个组件插入到文档中,以替换当前选择的内容。如果当前没有选择的内容,则将组件插入到插入符号的当前位置。在相关文档中这表示为内容字符的一个属性。

给定组件是 JTextPane 所使用的实际组件。由于组件不能是多个容器的子级,因此此方法不能在多个文本组件共享模型的情形下使用。

根据 Component.getAlignmentY 返回的值,组件相对于文本基线放置。对于 Swing 组件,可以方便地使用 JComponent.setAlignmentY 方法来设置此值。例如,设置值 0.75 将导致组件的 75% 位于基线之上,组件的 25% 位于基线之下。

虽然大多数 Swing 方法不是线程安全的,但此方法是线程安全的。有关更多信息,请参阅 How to Use Threads

参数:
c - 要插入的组件

insertIcon

public void insertIcon(Icon g)
将一个图标插入文档中,以替换当前选择的内容。如果当前没有选择的内容,则将图标插入到插入符号的当前位置。在相关文档中这被表示为内容字符的一个属性。

虽然大多数 Swing 方法不是线程安全的,但此方法是线程安全的。有关更多信息,请参阅 How to Use Threads

参数:
g - 要插入的图标
另请参见:
Icon

addStyle

public Style addStyle(String nm,
                      Style parent)
将一个新样式添加到逻辑样式层次结构中。样式属性自底向上进行解析,这样在子级中指定的属性重写在父级中指定的属性。

参数:
nm - 样式的名称(在命名样式集合中必须是惟一的)。如果样式未命名,则其名称可以为 null,但是调用者要负责管理返回的引用,因为未命名的样式不能通过其名称获取。未命名的样式可以用于诸如字符属性重写这类情况,比如,在样式运行中。
parent - 父样式。如果未指定的属性不需要以其他样式解析,则此值可以为 null
返回:
新的 Style

removeStyle

public void removeStyle(String nm)
移除以前添加到文档中的已命名的非 null 样式。

参数:
nm - 要移除的样式名称

getStyle

public Style getStyle(String nm)
获取以前添加的已命名的非 null 样式。

参数:
nm - 样式名称
返回:
Style

setLogicalStyle

public void setLogicalStyle(Style s)
设置在当前插入符号位置用于段落的逻辑样式。如果没有为字符和段落属性显式设置属性,则它们将通过分配给段落的逻辑样式进行解析,也就是通过某个层次结构来解析,该层次结构完全独立于文档中元素的层次结构。

虽然大多数 Swing 方法不是线程安全的,但此方法是线程安全的。有关更多信息,请参阅 How to Use Threads

参数:
s - 要分配给段落的逻辑样式,如果没有样式,则为 null

getLogicalStyle

public Style getLogicalStyle()
获取分配给由当前插入符号位置表示的段落的逻辑样式,或者 null

返回:
Style

getCharacterAttributes

public AttributeSet getCharacterAttributes()
获取在当前插入符号位置的有效字符属性,或者 null

返回:
属性,或者 null

setCharacterAttributes

public void setCharacterAttributes(AttributeSet attr,
                                   boolean replace)
将给定属性应用于字符内容。如果有选择内容,则属性将应用于所选择的范围。如果没有选择内容,则属性将应用于输入属性集合,此集合定义了所有新插入文本的属性。

虽然大多数 Swing 方法不是线程安全的,但此方法是线程安全的。有关更多信息,请参阅 How to Use Threads

参数:
attr - 属性
replace - 如果为 true,则首先替换现有属性

getParagraphAttributes

public AttributeSet getParagraphAttributes()
获取插入符号位置的当前有效段落属性,如果没有,则返回 null

返回:
属性

setParagraphAttributes

public void setParagraphAttributes(AttributeSet attr,
                                   boolean replace)
将给定属性应用于段落。如果有选择内容,则将属性应用于与选择内容相交的段落。如果没有选择内容,则将属性应用于当前插入符号位置的段落。

虽然大多数 Swing 方法不是线程安全的,但此方法是线程安全的。有关更多信息,请参阅 How to Use Threads

参数:
attr - 非 null 属性
replace - 如果为 true,则首先替换现有属性

getInputAttributes

public MutableAttributeSet getInputAttributes()
获取窗格的输入属性。

返回:
属性

getStyledEditorKit

protected final StyledEditorKit getStyledEditorKit()
获取编辑器工具包。

返回:
编辑器工具包

createDefaultEditorKit

protected EditorKit createDefaultEditorKit()
创建默认使用的 EditorKit。此实现返回 javax.swing.text.StyledEditorKit

覆盖:
JEditorPane 中的 createDefaultEditorKit
返回:
编辑器工具包

setEditorKit

public final void setEditorKit(EditorKit kit)
设置当前安装的用于内容处理的工具包。这是建立编辑器内容类型的绑定属性。

覆盖:
JEditorPane 中的 setEditorKit
参数:
kit - 所需的编辑器行为
抛出:
IllegalArgumentException - 如果 kit 不是一个 StyledEditorKit
另请参见:
JEditorPane.getEditorKit()

paramString

protected String paramString()
返回此 JTextPane 的字符串表示形式。此方法仅在进行调试的时候使用,对于各个实现,所返回字符串的内容和格式可能有所不同。返回的字符串可能为空,但不可能为 null

覆盖:
JEditorPane 中的 paramString
返回:
JTextPane 的字符串表示形式

JavaTM 2 Platform
Standard Ed. 6

提交错误或意见

版权所有 2008 Sun Microsystems, Inc. 保留所有权利。请遵守GNU General Public License, version 2 only