2011-03-17 6 views
2

기본적으로 텍스트 색상이 파란색으로 설정된 JTextPane이 있습니다. 이제 텍스트에 취소 선을 추가 한 다음 취소 선 색상이 텍스트 (파란색)와 동일하게됩니다. 나는 텍스트와 파업의 색을 다르게 원한다. 예 : 텍스트 색상이 파란색이면 취소 선이 달라야합니다.스윙 : JTextPane의 글꼴 색상 및 경고 색상

제게 아이디어를주십시오. 당신이 StyledLabels에 모습을 가질 수

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

답변

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

Hi StanislavL,이 예제는 이미 코딩에서 설정된 정적 값을 사용합니다. 하드 코딩 텍스트 대신 입력 텍스트에 취소 선을 적용하고 싶습니다. – Bibhaw

+0

속성 이름에 상수를 정의 할 수 있습니다. – StanislavL

+0

이미이 문제를 해결했습니다. 어쨌든 당신의 도움에 감사드립니다. – Bibhaw

0

아니면이 트릭을 할해야한다고 생각

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