Java JTextPane-具有两种风格的短语

Java JTextPane-具有两种风格的短语,java,swing,Java,Swing,我只是面对一件有趣的事情 我正在更改选定的文本样式。问题是,当我一个接一个地改变一个单词的样式时,这很好,但是接下来,如果我选择一个完整的样式短语并更改其字体颜色,整个短语只会变成一个样式(所选文本中的第一个样式):( 下面是问题片段 private void setFontColorStyle() { JTextPane editor=this.getTextPane(); String text=this.getTextPane().getSele

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

我正在更改选定的文本样式。问题是,当我一个接一个地改变一个单词的样式时,这很好,但是接下来,如果我选择一个完整的样式短语并更改其字体颜色,整个短语只会变成一个样式(所选文本中的第一个样式):(

下面是问题片段

  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) {

        }
    }
下面是粗体制作方法代码…() 斜体和下划线都是相同的逻辑,所以我想这很清楚

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...


    }
以下是粗体方法调用代码:

private void setFontBold()
    {
        this.setFontBoldStyle(); 
    }
…和颜色方法调用

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

    }
…和动作听众(粗体)

…还有颜色

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);
    }                                           
我非常需要你的建议,当我想改变一个完全不同的选定文本颜色时,如何保持选定的文本样式不变(如粗体或字体系列)

更清楚地说

例如,我有文本

我的你好世界不漂亮:)

接下来,我选择整个短语并将其颜色从黑色更改为红色。下一个文本变为红色,但根据第一种样式,整个短语变为粗体。但问题是,保留粗体和斜体样式会很有趣,但同时有一个短语红色:)非常简单,但我刚刚混淆了如何在选定文本区域的框架中控制多个样式

非常感谢中讨论的任何有用的注释

,它是如何管理此功能以及其他文本组件功能的一个很好的示例

附录:依赖预定义的
操作
对象来处理编辑任务。方便地,它包含一系列从
StyledTextAction
派生的嵌套类。作为一个具体的例子,下面是如何在方法
createStyleMenu()
中的
TextComponentDemo
Style
菜单中添加
AlignmentAction

其余(任意)对齐操作名称在中定义

附录:是嵌套编辑操作使用的通用例程。它调用了@StanislavL提出的
StyledDocument
中同名的方法

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

附录:
StyledEditorKit
操作与
JButton
JToolBar
一样有效

new JButton(new StyledEditorKit.ForegroundAction("Red", Color.red))

使用这个.getTextPane().getStyledDocument().setCharacterAttributes()

考虑创建并发布一个允许我们修改和测试代码的文件。不知道!这个问题很好+1糟糕的解决方案可能是逐个字符添加新属性。Emm。。。这是整个jbuttonactionperformed方法体。我刚刚编辑了一段代码,请注意,一段代码比一段代码或整个代码都好。请阅读链接。如果你没有得到一个像样的答案,你可以考虑创建和张贴其中之一,因为它会增加你得到良好的帮助的机会。你的意思是获得一个字符的字符?你的意思是一个字符一个风格?但是怎么办呢?我已经读了很多遍教程,但是没有找到任何提示。如果我错过了什么,请把它指给我看。我也看了片段。问题不在于“使文本左对齐”或仅一次,而在于如何保持对齐,例如选择所有文本并使其为“绿色”。我看了很多例子,但没有一个展示了如果需要的话如何保存样式历史:(可能问题是在选定文本的框架中逐个符号更改样式符号,但我不确定如何:(事实上,这就是我问这个问题的原因。在这个使用编辑器工具包操作的示例中,更改任何属性都会使其余属性保持不变。为什么不使用它们呢?因为问题不是完全逐字更改,而是选择整个短语(n个单词长度)设置为绿色,但同时保持粗体或斜体。IIUC,这正是示例中动作的行为方式。
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();
    ...
}
new JButton(new StyledEditorKit.ForegroundAction("Red", Color.red))