JTextPane - 具有两种样式的短语

use*_*704 7 java swing

我只是面对一件有趣的事情.

我正在改变选定的文字样式.问题是当我一个一个地更改ONE WORD的样式时它很好但接下来如果我选择一个整个样式的短语并改变它的字体颜色整个短语只变成一个样式(所选文本中的第一个样式):(

这是问题片段

  private void setFontColorStyle()
    {
        JTextPane editor=this.getTextPane();
        String text=this.getTextPane().getSelectedText();

        StyledDocument doc=(StyledDocument) editor.getDocument();
        int selectionEnd=this.getTextPane().getSelectionEnd();
        int selectionStart=this.getTextPane().getSelectionStart();


        Element element=doc.getCharacterElement(selectionStart);
        AttributeSet as = element.getAttributes();

        String family = StyleConstants.getFontFamily(as);
        int fontSize = StyleConstants.getFontSize(as);
        boolean isBold=StyleConstants.isBold(as);
        boolean isItalic=StyleConstants.isItalic(as);
        boolean isUnderlined=StyleConstants.isUnderline(as);

        StyleContext context = new StyleContext();
        Style style;

        this.getTextPane().replaceSelection("");

        style = context.addStyle("mystyle", null);
        style.addAttribute(StyleConstants.FontSize, fontSize);
        style.addAttribute(StyleConstants.FontFamily, family);
        style.addAttribute(StyleConstants.Foreground, this.fontColor);
        style.addAttribute(StyleConstants.Bold, isBold);
        style.addAttribute(StyleConstants.Italic, isItalic);
        style.addAttribute(StyleConstants.Underline, isUnderlined);

        this.getTextPane().replaceSelection("");
        try {
            this.getTextPane().getStyledDocument().insertString(selectionEnd - text.length(), text, style);
        } catch (BadLocationException ex) {

        }
    }
Run Code Online (Sandbox Code Playgroud)

这里是大胆的制作方法代码...()斜体和下划线所有相同的逻辑,所以我想这很清楚

private void setFontBoldStyle()
    {
         if(this.getTextPane().getSelectedText()!=null)
        {

        String text = this.getTextPane().getSelectedText();
        int selectionStart=this.getTextPane().getSelectionStart();
        int selectionEnd=this.getTextPane().getSelectionEnd();






        StyleContext context = new StyleContext();
        Style style;


        Element element=doc.getCharacterElement(selectionStart);
        Enumeration en=doc.getStyleNames();

        AttributeSet as = element.getAttributes();

        /**
         * Get style from history...
         */
        String family = StyleConstants.getFontFamily(as);
        int fontSize = StyleConstants.getFontSize(as);
        Color currentColor=StyleConstants.getForeground(as);
        boolean isBold=StyleConstants.isBold(as)?false:true;
        boolean isItalic=StyleConstants.isItalic(as);
        boolean isUnderlined=StyleConstants.isUnderline(as);

        String styleName=String.valueOf(Math.random());

        style = context.addStyle(styleName, null);
//        style.addAttribute(StyleConstants.FontSize, fontSize);
//        style.addAttribute(StyleConstants.FontFamily, family);
        style.addAttribute(StyleConstants.Foreground, currentColor);
        style.addAttribute(StyleConstants.FontFamily, family);
        style.addAttribute(StyleConstants.FontSize, fontSize);
        style.addAttribute(StyleConstants.Bold, isBold);
        style.addAttribute(StyleConstants.Italic, isItalic);
        style.addAttribute(StyleConstants.Underline, isUnderlined);

        this.getTextPane().replaceSelection("");



        try {
            this.getTextPane().getStyledDocument().insertString(selectionEnd - text.length(), text, style);
        } catch (BadLocationException ex) {

        }

        }//if end...


    }
Run Code Online (Sandbox Code Playgroud)

这是大胆的方法调用代码:

private void setFontBold()
    {
        this.setFontBoldStyle(); 
    }
Run Code Online (Sandbox Code Playgroud)

...和颜色方法调用

 private void setFontColor(Color fontColor)
    {
        this.fontColor=fontColor;
        this.setFontColorStyle();

    }
Run Code Online (Sandbox Code Playgroud)

...和动作听众(粗体)......

 private void boldButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
         this.getTextPane().requestFocusInWindow();
         this.setFontBold();
    }                                          
Run Code Online (Sandbox Code Playgroud)

......还有颜色

private void colorButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            

       this.getTextPane().requestFocusInWindow();

       ColorDialog colorEditor=new ColorDialog();

      //returns rgb color...
       Color color=colorEditor.getSelectedColor(this.getDialog(), true,false);


       if(color==null){
           JOptionPane.showMessageDialog(this.getDialog(), "null color");
           return;
       }

       this.setFontColor(color);
    }                                           
Run Code Online (Sandbox Code Playgroud)

当我想要改变一个完整的不同风格的选定文本颜色时,我非常需要你关于如何保持所选文本样式不变的建议(如粗体或字体系列)?

更清楚......

例如我有文字

我的 Hello World不漂亮:)

接下来,我选择整个短语并将其颜色从黑色更改为红色.下一个文字变成红色,但整个短语根据第一种风格变得粗体.但问题是保持粗体和斜体样式会很有趣,但同时又有了红色:)这很简单,但我只是混淆了如何在所选文本区域的框架中控制多种样式?

任何有用的评论都非常感谢

tra*_*god 3

TextComponentDemo如何使用编辑器窗格和文本窗格中讨论的是如何管理此功能以及其他文本组件功能的一个很好的示例。

附录:TextComponentDemo依靠预定义的Action对象来处理编辑任务。方便地,StyledEditorKit包含一系列派生自 的嵌套类StyledTextAction。作为一个具体示例,以下是如何将 an 添加AlignmentAction到方法中的Style菜单:TextComponentDemocreateStyleMenu()

protected JMenu createStyleMenu() {
    JMenu menu = new JMenu("Style");

    Action action = new StyledEditorKit.AlignmentAction(
        "left-justify", StyleConstants.ALIGN_LEFT);
    action.putValue(Action.NAME, "Left");
    menu.add(action);
    menu.addSeparator();
    ...
}
Run Code Online (Sandbox Code Playgroud)

其余(任意)对齐操作名称在StyledEditorKit.

附录:setCharacterAttributes()是嵌套编辑操作使用的通用例程。它调用 中的同名方法StyledDocument,如 @StanislavL 所提议的。

附录:我无法重现您所描述的效果。当我设置选区的颜色时,样式属性保持不变。

附录:这些操作与 a或StyledEditorKit一样有效。JButtonJToolBar

new JButton(new StyledEditorKit.ForegroundAction("Red", Color.red))
Run Code Online (Sandbox Code Playgroud)

文本组件演示