JTextPane - 具有两种样式的短语 [英] JTextPane - a phrase with TWO styles

查看:117
本文介绍了JTextPane - 具有两种样式的短语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚面对一件有趣的事情。

I just faced an interesting thing.

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

I was changing selected text style. The thing is when I change style for ONE WORD one by one it's fine but next if I select a whole styled phrase and change its font color the whole phrase becomes ONE styled (the first style within the selected text) only :(

这是问题片段

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

        }
    }

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

And here is the bold making method code... () Italic and underlined all the same logic so I guess it is quite clear

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


    }

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

Here is the bold method invokation code:

private void setFontBold()
    {
        this.setFontBoldStyle(); 
    }

...和颜色方法调用

... and color method invokation

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

    }

...和动作监听器(粗体).. 。

... and action listeners (for bold)...

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

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

I dearly need your advice about how to keep selected text styles unchanged (like bold or font family) when I want to change a whole different styled selected text color for example?

更清楚......

例如我有文字


我的 Hello 世界不漂亮:)

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

Next I select the whole phrase and change its color from black to lets say red. Next text becomes red but the whole phrase becomes bold according to first style. But the thing is it would be interesting to keep bold and italic styles but at the same time have the phrase red :) So quite simple but I have just confused how to control more than one style in the frames of selected text area?

非常感谢任何有用的评论

Any useful comment is much appreciated

推荐答案

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

TextComponentDemo, discussed in How to Use Editor Panes and Text Panes, is a good example of how to manage this as well as other text component features.

附录: TextComponentDemo 依赖在预先定义了 Action 用于处理编辑任务的对象。方便地, StyledEditorKit 包含一系列嵌套类,这些类派生自 StyledTextAction 。作为一个具体的例子,这里是如何将 AlignmentAction 添加到 TextComponentDemo的 Style 菜单中方法中的 createStyleMenu()

Addendum: TextComponentDemo relies on pre-defined Action objects to handle editing tasks. Conveniently, StyledEditorKit contains a series of nested classes that derive from StyledTextAction. As a concrete example, here's how one might add an AlignmentAction to the Style menu of TextComponentDemo in the method 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中私下定义

The remaining (arbitrary) alignment action names are defined privately in StyledEditorKit.

附录: setCharacterAttributes () 是嵌套编辑操作使用的常用例程。它按照@StanislavL的建议调用 StyledDocument 中的同名方法。

Addendum: setCharacterAttributes() is the common routine used by the nested editing actions. It invokes a method of the same name in StyledDocument, as proposed by @StanislavL.

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

Addendum: I am unable to reproduce the effect you describe. When I set the color of the selection, the style attributes remain unchanged.

附录: StyledEditorKit 操作仅适用以及 JButton JToolBar

Addendum: The StyledEditorKit actions work just as well with a JButton or JToolBar.

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

这篇关于JTextPane - 具有两种样式的短语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆