java - JTextPane - 具有两种样式的短语

标签 java swing

我刚刚遇到了一件有趣的事情。

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

这是问题片段

  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();

    }

...和 Action 监听器(粗体)...

 private void boldButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
         this.getTextPane().requestFocusInWindow();
         this.setFontBold();
    }                                          

...和颜色

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

例如,当我想更改整个不同样式的所选文本颜色时,我非常需要您的建议,了解如何保持所选文本样式不变(如粗体或字体系列)?

更清楚...

例如我有文本

My Hello World is not pretty :)

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

非常感谢任何有用的评论

最佳答案

TextComponentDemo , 在 How to Use Editor Panes and Text Panes 中讨论, 是一个很好的例子,说明如何管理这个以及其他文本组件功能。

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

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();
    ...
}

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

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

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

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

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

TextComponentDemo

关于java - JTextPane - 具有两种样式的短语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7624247/

相关文章:

java - Retrofit API Post 调用返回错误 500,与 Postman 配合良好

java - 在 JFrame 中使用 BoxLayout

java - 如何更改JTabbed Panel中箭头键的默认功能?

java - 跨多个项目共享单个 .editorconfig 文件

java - 我可以在 MDB 中获取源 JMS session 吗?

java - 多项式 GUI 不起作用

java - 在 jFrame 中禁用最大化并使用鼠标调整大小

java - 新波士顿GUI-编译错误

java - JBOSS/WildFly : CreateProcess error=193, %1 不是有效的 Win32 程序,我该如何解决这个问题?

Java EXP4J : Correct precedence of factorials?