【问题标题】:How do I easily edit the style of the selected text in a JTextPane?如何在 JTextPane 中轻松编辑所选文本的样式?
【发布时间】:2009-04-19 23:35:50
【问题描述】:

如何在 JTextPane 中轻松编辑所选文本的样式?这方面的资源似乎不多。即使你能指导我找到一个很好的资源,我也会非常感激。

另外,如何获取所选文本的当前样式?我试过styledDoc.getLogicalStyle(textPane.getSelectionStart());,但它似乎不起作用。

【问题讨论】:

    标签: java swing jtextpane


    【解决方案1】:

    这是一个代码 sn-p 用于插入格式化的“Hello World!” 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);
    

    【讨论】:

    • 我不知道为什么这被否决了,因为这是这里最正确的答案!
    【解决方案2】:

    看看这个pastebin中的以下代码:

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

    示例来自这里:

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

    看起来您可以在动作侦听器中使用以下内容更改样式:

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

    它将给定偏移量和长度之间的文本样式设置为特定样式。

    有关详细信息,请参阅完整的 pastebin。不过,这应该可以解决您的问题。

    【讨论】:

      【解决方案3】:

      操作文本面板的最简单方法是使用editor kits 及其关联的actions。您可以在 JDK 示例(在 jdk\demo\jfc\Stylepad 下)中找到此演示。

      安装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);
        }
      

      (提示:如果您想使用FontFamilyAction,请查看GraphicsEnvironment.getAvailableFontFamilyNames()logical font family names。)

      【讨论】:

      • 很好的例子,但我有一个问题。如何获取带有样式的文本以及如何将带有样式的文本保存在数据库中?
      【解决方案4】:

      我建议您查看 Sun 的 Java Tutorial 关于编辑器窗格。

      【讨论】:

        【解决方案5】:

        好的,哇。难以回答的问题。所以我还没有找到一种方法来获得给定角色的风格。但是,您可以获取给定字符的 MutableAttributeSet,然后测试样式是否在该属性集中。

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

        获取一系列字符的样式的一个问题是,可能有多个样式应用于该范围(例如:您可以选择有些为粗体而有些不是)的文本。

        要更新所选文本,您可以:

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

        哦,看来函数 getLogicalStyle 不起作用,因为它返回的是包含 p 的段落的默认样式(或者可能只是样式),而不是 p 处字符的样式。

        【讨论】:

          猜你喜欢
          • 2012-11-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-26
          • 2010-09-08
          • 2020-10-11
          相关资源
          最近更新 更多