2012-08-22 13 views
5

बस JTextPane में पाठ को रंग देने का प्रयास कर रहा है - लेकिन इस मुद्दे में टेक्स्ट और अंडरलाइन के लिए अलग-अलग रंग नहीं हो सकते हैं। मुझे यह कैसे करना चाहिए या यह भी संभव है? नीचे दिया गया उदाहरण सभी पाठ प्रिंट करता है और लाल रंग में रेखांकित करता है।मैं JTextPane में टेक्स्ट और अंडरलाइन के लिए अलग-अलग रंग कैसे सेट करूं?

JTextPane pane = new JTextPane(); 

StyleContext context = new StyleContext(); 

Style style = pane.addStyle("Black", null); 
StyleConstants.setAlignment(style, StyleConstants.ALIGN_RIGHT); 
StyleConstants.setFontSize(style, 14); 
StyleConstants.setSpaceAbove(style, 4); 
StyleConstants.setSpaceBelow(style, 4); 
StyleConstants.setForeground(style, Color.BLACK); 

StyledDocument document = pane.getStyledDocument(); 


style = pane.addStyle("Red Underline", style); 
StyleConstants.setForeground(style, Color.RED); 
StyleConstants.setUnderline(style, true); 

pane.getDocument().insertString(0, "Test String", style); 
+0

+1 इस तथ्य के बावजूद कि स्टाइल एपीआई एक्स्टेंसिबल लगता है, मुझे कोई दस्तावेज नहीं मिला कि यह कैसे करें। –

+0

यहां जवाब मिला ... http: // stackoverflow।कॉम/प्रश्न/9502654/अंडरलाइन-स्टाइलकॉन्स्ट-इन-ए-अलग-रंग-साथ-विशेषताएँ –

+0

पोस्ट के रूप में पोस्ट करें –

उत्तर

2
  • for example, और for Html

  • Document रिटर्न देखने पर मॉडल, वहाँ सूचकांक जहां पंक्ति शुरू कर दिया निर्धारित करने के लिए संभव है/समाप्त हो गया

4

यहाँ समाधान मैंने पाया है। ..

Underline StyleConstant in a different colour with AttributeSet

यहाँ कोड नमूने के लिए

http://java-sl.com/tip_colored_strikethrough.html

सिर्फ बदलते निम्न पंक्ति यह हल करती है ...

int y = a.getBounds().y + a.getBounds().height + 2 ; 

लिंक है और यह ठीक काम करता है

4

असल में वहाँ 3 वर्गों है आप बनाने की आवश्यकता है:

  • यदि आप चाहें तो दृश्य को संशोधित करने के लिए आपको javax.swing.text.LabelView का विस्तार करने की आवश्यकता है (चाहे वह रंगीन अंडरलाइन जोड़ रहा हो या नहीं)। आप paint(Graphics, Shape) विधि को ओवरराइड कर देंगे। आप ओवरराइड क्लास में इस लाइन के साथ विशेषताओं तक पहुंच सकते हैं - गुण टेक्स्ट के लिए कुछ अतिरिक्त करने के लिए ट्रिगर होना चाहिए (जैसे अंडरलाइन जोड़ना)।

    getElement().getAttributes().getAttribute("attribute name");

  • आप एक नया ViewFactory बना सकते हैं और create विधि अधिलेखित करने के लिए की जरूरत है। यह महत्वपूर्ण है कि यदि आप ऐसा करते सभी तत्व प्रकार संभाल है (अन्यथा बातें बहुत सही प्रदर्शित करेगा नहीं।

  • आप फलक जो ViewFactory उपयोग करने के लिए बता बनाने के लिए एक StyledEditorKit बनाना होगा।

यहाँ इस का एक सरलीकृत और runnable उदाहरण:

012:

import java.awt.*; 
import javax.swing.*; 
import javax.swing.plaf.basic.BasicTextPaneUI; 
import javax.swing.text.*; 

public class TempProject extends JPanel{ 


    public static void main(String args[]) { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       //Adding pane 
       JTextPane pane = new JTextPane(); 
       pane.setEditorKit(new CustomEditorKit()); 
       pane.setText("Underline With Different Color"); 

       //Set Style 
       StyledDocument doc = (StyledDocument)pane.getDocument(); 
       MutableAttributeSet attrs = new SimpleAttributeSet(); 
       attrs.addAttribute("Underline-Color", Color.red); 
       doc.setCharacterAttributes(0, doc.getLength()-1, attrs, true); 

       JScrollPane sp = new JScrollPane(pane); 
       frame.setContentPane(sp); 
       frame.setPreferredSize(new Dimension(400, 300)); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 


      } 
     }); 
    } 

    public static class CustomEditorKit extends StyledEditorKit{ 

     public ViewFactory getViewFactory(){ 
      return new CustomUI(); 
     } 
    } 

    public static class CustomUI extends BasicTextPaneUI{ 
     @Override 
     public View create(Element elem){ 
      View result = null; 
      String kind = elem.getName(); 
      if(kind != null){ 
       if(kind.equals(AbstractDocument.ContentElementName)){ 
        result = new MyLabelView(elem); 
       } else if(kind.equals(AbstractDocument.ParagraphElementName)){ 
        result = new ParagraphView(elem); 
       }else if(kind.equals(AbstractDocument.SectionElementName)){ 
        result = new BoxView(elem, View.Y_AXIS); 
       }else if(kind.equals(StyleConstants.ComponentElementName)){ 
        result = new ComponentView(elem); 
       }else if(kind.equals(StyleConstants.IconElementName)){ 
        result = new IconView(elem); 
       } else{ 
        result = new LabelView(elem); 
       } 
      }else{ 
       result = super.create(elem); 
      } 

      return result; 
     } 
    } 

    public static class MyLabelView extends LabelView{ 

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

     public void paint(Graphics g, Shape a){ 
      super.paint(g, a); 
      //Do whatever other painting here; 
      Color c = (Color)getElement().getAttributes().getAttribute("Underline-Color"); 
      if(c != null){ 
       int y = a.getBounds().y + (int)getGlyphPainter().getAscent(this); 
       int x1 = a.getBounds().x; 
       int x2 = a.getBounds().width + x1; 

       g.setColor(c); 
       g.drawLine(x1, y, x2, y); 
      } 

     } 

    } 

} 

यहाँ एक और नमूना कोड के लिए लिंक है

यह उत्तर ज्यादातर पोस्टरिटी के लिए है, मैंने सोचा कि लिंक किए गए कोड का एक सरलीकृत संस्करण जोड़ना और स्पष्टीकरण चीजों को और समझने में मदद करेगा।

+0

अच्छा वर्णन +1 – mKorbel

संबंधित मुद्दे