简体   繁体   English

如何在JTextPane中轻松编辑所选文本的样式?

[英]How do I easily edit the style of the selected text in a JTextPane?

How do I easily edit the style of the selected text in a JTextPane? 如何在JTextPane中轻松编辑所选文本的样式? There doesn't seem to be many resources on this. 这方面似乎没有太多资源。 Even if you can direct me to a good resource on this, I'd greatly appreciate it. 即使你可以指导我获得一个很好的资源,我也非常感激。

Also, how do I get the current style of the selected text? 另外,如何获取所选文本的当前样式? I tried styledDoc.getLogicalStyle(textPane.getSelectionStart()); 我试过styledDoc.getLogicalStyle(textPane.getSelectionStart()); but it doesn't seem to be working. 但它似乎没有起作用。

Here's a code snippet to insert a formatted "Hello World!" 这是一个插入格式化的“Hello World!”的代码片段。 string in a JEditorPane : JEditorPane字符串:

Document doc = yourEditorPane.getDocument();

StyleContext sc = new StyleContext();
Style style = sc.addStyle("yourStyle", null);

Font font = new Font("Arial", Font.BOLD, 18);

StyleConstants.setForeground(style, Color.RED);
StyleConstants.setFontFamily(style, font.getFamily());
StyleConstants.setBold(style, true);

doc.insertString(doc.getLength(), "Hello World!", style);

Take a look at the following code in this pastebin: 看看这个pastebin中的以下代码:

http://pbin.oogly.co.uk/listings/viewlistingdetail/d6fe483a52c52aa951ca15762ed3d3 http://pbin.oogly.co.uk/listings/viewlistingdetail/d6fe483a52c52aa951ca15762ed3d3

The example is from here: 这个例子来自这里:

http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneStylesExample3.htm http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneStylesExample3.htm

It looks like you can change the style using the following in an action listener: 看起来您可以在动作侦听器中使用以下内容更改样式:

final Style boldStyle = sc.addStyle("MainStyle", defaultStyle);
StyleConstants.setBold(boldStyle, true);   

doc.setCharacterAttributes(0, 10, boldStyle, true);

It sets the style of the text between the given offset and length to a specific style. 它将给定偏移量和长度之间的文本样式设置为特定样式。

See the full pastebin for more details. 有关详细信息,请参阅完整的pastebin。 That should fix your problem though. 这应该可以解决你的问题。

The easiest way to manipulate text panels is using editor kits and their associated actions . 操作文本面板的最简单方法是使用编辑器工具包及其相关操作 You can find a demo of this in the JDK samples (under jdk\\demo\\jfc\\Stylepad ). 您可以在JDK示例中找到此演示(在jdk \\ demo \\ jfc \\ Stylepad下 )。

Sample code that installs a StyledEditorKit and uses a FontSizeAction to manipulate the text: 安装StyledEditorKit并使用FontSizeAction操作文本的示例代码:

  public static void main(String[] args) {
    // create a rich text pane
    JTextPane textPane = new JTextPane();
    JScrollPane scrollPane = new JScrollPane(textPane,
        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    // install the editor kit
    StyledEditorKit editorKit = new StyledEditorKit();
    textPane.setEditorKit(editorKit);
    // build the menu
    JMenu fontMenu = new JMenu("Font Size");
    for (int i = 48; i >= 8; i -= 10) {
      JMenuItem menuItem = new JMenuItem("" + i);
      // add an action
      menuItem
          .addActionListener(new StyledEditorKit.FontSizeAction(
              "myaction-" + i, i));
      fontMenu.add(menuItem);
    }
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(fontMenu);
    // show in a frame
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 400);
    frame.setJMenuBar(menuBar);
    frame.setContentPane(scrollPane);
    frame.setVisible(true);
  }

(Tip: if you want to use a FontFamilyAction , have a look at GraphicsEnvironment.getAvailableFontFamilyNames() and logical font family names .) (提示:如果要使用FontFamilyAction ,请查看GraphicsEnvironment.getAvailableFontFamilyNames()逻辑字体系列名称 。)

我建议看看Sun的Java Tutorial有关编辑器窗格的内容。

Ok, wow. 好的,哇 Hard question. 难以回答的问题。 So I have not found a way to get the style of a given character. 所以我还没有找到一种方法来获得给定角色的风格。 You can, however, get the MutableAttributeSet for a given character and then test to see if the style is in that attribute set. 但是,您可以获取给定字符的MutableAttributeSet,然后测试该样式是否在该属性集中。

   Style s; //your style
   Element run = styledDocument.getCharacterElement( 
       textPane.getSelectionStart() );
   MutableAttributeSet curAttr =
       ( MutableAttributeSet )run.getAttributes();
   boolean containsIt = curAttr.containsAttributes( s );

One problem with getting the Style for a range of characters is that there may be more than one style applied to that range (example: you may select text where some is bold and some is not). 获取一系列字符的样式的一个问题是可能有多个样式应用于该范围(例如:您可以选择文本,其中一些是粗体而一些不是)。

To update the selected text you can: 要更新所选文本,您可以:

  Style s; //your style
  JTextPane textPane; //your textpane
  textPane.setCharacterAttributes( s, false );

Oh, and it appears that the function getLogicalStyle doesn't work because it's returning the default style (or maybe just the style) for the paragraph that contains p, rather than the the style of the character at p. 哦,似乎函数getLogicalStyle不起作用,因为它返回包含p的段落的默认样式(或者可能只是样式),而不是p处的字符样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM