2011-03-17 98 views
2

我有一個JTextPane,默認情況下文本顏色設置爲藍色。現在,我在文字上添加了透視效果,透視顏色變得與文字(藍色)相同。我希望文字的顏色和透視不同。例如如果文字顏色是藍色的,那麼穿透效果必須不同。Swing:JTextPane中的字體顏色和着色顏色

請給我一些想法。

JTextPane text = new JTextPane(); 

    Font font = new Font("Serif", Font.ITALIC, 20); 
    text.setFont(font); 

    text.setForeground(Color.BLUE); 

    Style style = text.addStyle("Bold", null); 
    StyleConstants.setStrikeThrough(style, true); 

    text.setCharacterAttributes(style, false); 

回答

2
import java.awt.*; 
import javax.swing.*; 
import javax.swing.text.*; 

public class Test { 

    public Test() { 
     JFrame fr = new JFrame("TEST"); 
     fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JEditorPane pane = new JEditorPane(); 
     pane.setEditorKit(new NewEditorKit()); 
     pane.setText("test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test "); 

     StyledDocument doc = (StyledDocument) pane.getDocument(); 
     MutableAttributeSet attr = new SimpleAttributeSet(); 
     attr.addAttribute("strike-color", Color.red); 
     doc.setCharacterAttributes(0, 9, attr, false); 

     attr.addAttribute("strike-color", Color.blue); 
     doc.setCharacterAttributes(10, 19, attr, false); 
     JScrollPane sp = new JScrollPane(pane); 

     fr.getContentPane().add(sp); 
     fr.setSize(300, 300); 
     fr.setLocationRelativeTo(null); 
     fr.setVisible(true); 
    } 

    public static void main(String[] args) { 
     Test test = new Test(); 
    } 
} 

class NewEditorKit extends StyledEditorKit { 
    public ViewFactory getViewFactory() { 
     return new NewViewFactory(); 
    } 
} 

class NewViewFactory implements ViewFactory { 
    public View create(Element elem) { 
     String kind = elem.getName(); 
     if (kind != null) { 
      if (kind.equals(AbstractDocument.ContentElementName)) { 
       return new MyLabelView(elem); 
      } 
      else if (kind.equals(AbstractDocument.ParagraphElementName)) { 
       return new ParagraphView(elem); 
      } 
      else if (kind.equals(AbstractDocument.SectionElementName)) { 
       return new BoxView(elem, View.Y_AXIS); 
      } 
      else if (kind.equals(StyleConstants.ComponentElementName)) { 
       return new ComponentView(elem); 
      } 
      else if (kind.equals(StyleConstants.IconElementName)) { 
       return new IconView(elem); 
      } 
     } 

     // default to text display 
     return new LabelView(elem); 
    } 
} 

class MyLabelView extends LabelView { 

    public MyLabelView(Element elem) { 
     super(elem); 
    } 

    public void paint(Graphics g, Shape allocation) { 
     super.paint(g, allocation); 
     paintStrikeLine(g, allocation); 
    } 

    public void paintStrikeLine(Graphics g, Shape a) { 
     Color c=(Color)getElement().getAttributes().getAttribute("strike-color"); 
     if (c!=null) { 
      int y = a.getBounds().y + a.getBounds().height - (int) getGlyphPainter().getDescent(this); 
      y = y - (int) (getGlyphPainter().getAscent(this) * 0.3f); 
      int x1 = (int) a.getBounds().getX(); 
      int x2 = (int) (a.getBounds().getX() + a.getBounds().getWidth()); 

      Color old = g.getColor(); 
      g.setColor(c); 
      g.drawLine(x1, y, x2, y); 
      g.setColor(old); 
     } 
    } 
} 
+0

嗨StanislavL,這個例子使用已經在編碼中設置的靜態值。我想對輸入文本應用透視而不是硬編碼文本。 – Bibhaw 2011-03-18 05:58:22

+0

您可以爲屬性名稱定義一個常量。 – StanislavL 2011-03-25 10:32:15

+0

嘿已經解決了這個問題。無論如何感謝您的幫助。 – Bibhaw 2011-03-28 03:22:47

1

我想這應該做的伎倆......

MutableAttributeSet attributes = text.getInputAttributes(); 
StyleConstants.setStrikeThrough(attributes , true); 
StyleConstants.setForeground(attributes , Color.BLack); 

StyledDocument doc = text.getStyledDocument(); 
doc.setCharacterAttributes(0, doc.getLength() + 1, attributes, false); 
+1

嗨Velter,我試過了它之前並沒有做任何改變。 – Bibhaw 2011-03-17 10:14:14