2012-10-25 16 views
9

मैं थोड़ी देर के लिए इस के लिए खोज कर रहे हैं और अब तक सब मैं बहुत की तरह एक चरित्र के लिए एक शैली बनाने के लिए कैसे है साथ आते हैं और इसे लागू कर लिया है:मैं JTextPane में प्रत्येक वर्ण को एक अलग रंग/पृष्ठभूमि रंग में कैसे सेट कर सकता हूं?

StyledDocument doc = (StyledDocument) new DefaultStyledDocument(); 
JTextPane textpane = new JTextPane(doc); 
textpane.setText("Test"); 
javax.swing.text.Style style = textpane.addStyle("Red", null); 
StyleConstants.setForeground(style, Color.RED); 
doc.setCharacterAttributes(0, 1, textpane.getStyle("Red"), true); 

यह उपयोगी है अगर आपके पास अपने दस्तावेज़ में केवल कुछ शैलियों हैं और उन्हें नाम से स्टोर करना चाहते हैं ताकि आप उन्हें बाद में आसानी से लागू कर सकें। मेरे आवेदन में मैं पाठ में प्रत्येक चरित्र के लिए स्वतंत्र रूप से अग्रभूमि रंग (केवल कुछ मानों में से एक) और पृष्ठभूमि रंग (ग्रेस्केल, कई अलग-अलग मान) सेट करने में सक्षम होना चाहता हूं। यह इसके लिए संभावित रूप से सैकड़ों/हजारों शैलियों को बनाने के लिए एक विशाल अपशिष्ट की तरह लगता है। क्या हर बार एक नई शैली बनाने के बिना इन विशेषताओं को सेट करने का कोई तरीका है? अगर मुझे केवल पाठ प्रस्तुत करना होता तो यह बहुत आसान होगा लेकिन मुझे इसे संपादन योग्य बनाने की भी आवश्यकता है। क्या JTextPane के साथ ऐसा करने का कोई तरीका है, या क्या कोई और स्विंग क्लास है जो इस कार्यक्षमता प्रदान करती है?

उत्तर

14

आप हर किरदार के लिए शैली बदलना चाहते हैं एक टेक्स्टपेन में, यह करने के लिए यहां एक पूर्ण यादृच्छिक तरीका है। आप प्रत्येक चरित्र के लिए एक अलग विशेषता सेट बनाते हैं। उपयुक्त संयोजन खोजने के लिए आप (अग्रभूमि/पृष्ठभूमि विपरीत, वर्णों के आकार में बहुत अधिक अंतर नहीं है ...)। आप विभिन्न शैलियों को भी स्टोर कर सकते हैं जिन्हें आपने पहले ही लागू कर लिया है ताकि आप एक ही बार दो बार इसका उपयोग न करें।

enter image description here

import java.awt.Color; 
import java.util.Random; 

import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTextPane; 
import javax.swing.text.DefaultStyledDocument; 
import javax.swing.text.SimpleAttributeSet; 
import javax.swing.text.StyleConstants; 
import javax.swing.text.StyledDocument; 

public class TestDifferentStyles { 
    private void initUI() { 
     JFrame frame = new JFrame(TestDifferentStyles.class.getSimpleName()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     StyledDocument doc = new DefaultStyledDocument(); 
     JTextPane textPane = new JTextPane(doc); 
     textPane.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has " 
       + "been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of " 
       + "type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the " 
       + "leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the" 
       + " release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing " 
       + "software like Aldus PageMaker including versions of Lorem Ipsum."); 

     Random random = new Random(); 
     for (int i = 0; i < textPane.getDocument().getLength(); i++) { 
      SimpleAttributeSet set = new SimpleAttributeSet(); 
      // StyleConstants.setBackground(set, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); 
      StyleConstants.setForeground(set, new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); 
      StyleConstants.setFontSize(set, random.nextInt(12) + 12); 
      StyleConstants.setBold(set, random.nextBoolean()); 
      StyleConstants.setItalic(set, random.nextBoolean()); 
      StyleConstants.setUnderline(set, random.nextBoolean()); 

      doc.setCharacterAttributes(i, 1, set, true); 
     } 

     frame.add(new JScrollPane(textPane)); 
     frame.setSize(500, 400); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 

     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new TestDifferentStyles().initUI(); 
      } 
     }); 
    } 

} 
+0

पाठ के साथ कोई कॉपीराइट समस्या? स्विंगएक्स टेस्ट यूटिल में इसका इस्तेमाल करना अच्छा लगेगा :-) – kleopatra

+0

@ क्लोपेट्रा AFAIK "Lorem ipsum" सार्वजनिक डोमेन में है क्योंकि यह 500 से अधिक वर्षों से मौजूद है। –

+1

@ क्लोपेट्रा नहीं मैंने इसे यहां से लिया [http://www.lipsum.com/) –

9

मुझे यकीन है कि तुम क्या मतलब है नहीं कर रहा हूँ, लेकिन JtextPane में और सभी पत्रों के माध्यम से है कि पाश पुनरावृति के भीतर प्रत्येक चरित्र के माध्यम से पाश नहीं कर सकते/वर्ण आप को उजागर करने आदि चाहते हैं एक अगर बयान चरित्र और उसके बाद जाँच है तदनुसार Style सेट करें।

यहाँ एक उदाहरण मैं बनाया मैं केवल पात्रों और डब्ल्यू प्रदर्शन के उद्देश्य से के लिए इसे लागू किया है:

enter image description here

//necessary imports 
import java.awt.Color; 
import java.util.ArrayList; 
import javax.swing.JFrame; 
import javax.swing.JTextPane; 
import javax.swing.text.DefaultStyledDocument; 
import javax.swing.text.StyleConstants; 
import javax.swing.text.StyledDocument; 

public class Test { 

    /** 
    * Default constructor for Test.class 
    */ 
    public Test() { 
     initComponents(); 
    } 

    public static void main(String[] args) { 

     /** 
     * Create GUI and components on Event-Dispatch-Thread 
     */ 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       Test test = new Test(); 
      } 
     }); 
    } 

    /** 
    * Initialize GUI and components (including ActionListeners etc) 
    */ 
    private void initComponents() { 
     JFrame jFrame = new JFrame(); 
     jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     StyledDocument doc = (StyledDocument) new DefaultStyledDocument(); 
     JTextPane textPane = new JTextPane(doc); 
     textPane.setText("Hello, world! :)"); 

     //create necessary styles for various characters 
     javax.swing.text.Style style = textPane.addStyle("Red", null); 
     StyleConstants.setForeground(style, Color.RED); 
     javax.swing.text.Style style2 = textPane.addStyle("Blue", null); 
     StyleConstants.setForeground(style2, Color.BLUE); 

     //create array of characters to check for and style 
     String[] lettersToEdit = new String[]{"h", "w"}; 

     //create arraylist to hold each character in textPane 
     ArrayList<String> strings = new ArrayList<>(); 

     //get all text 
     String text = textPane.getText(); 

     //populate arraylist 
     for (int i = 0; i < text.length(); i++) { 
      strings.add(text.charAt(i) + ""); 
     } 

     //declare variabe to hold position 
     int position = 0; 

     for (String s1 : strings) {//for each character in the textpane text 
      for (String s2 : lettersToEdit) {//for each character in array to check (lettersToEdit) 
       if (s2.toLowerCase().equalsIgnoreCase(s1)) {//if there was a match 

        System.out.println("found a match: " + s1); 
        System.out.println("counter: " + position + "/" + (position + 1)); 

        //check which chacacter we matched 
        if (s1.equalsIgnoreCase(lettersToEdit[0])) { 
         //set appropriate style 
         doc.setCharacterAttributes(position, 1, textPane.getStyle("Red"), true); 
        } 
        if (s1.equalsIgnoreCase(lettersToEdit[1])) { 
         doc.setCharacterAttributes(position, 1, textPane.getStyle("Blue"), true); 
        } 
       } 
      } 
      //increase position after each character on textPane is parsed 
      position++; 
     } 

     jFrame.add(textPane); 
     //pack frame (size JFrame to match preferred sizes of added components and set visible 
     jFrame.pack(); 
     jFrame.setVisible(true); 
    } 
} 
+0

धन्यवाद डेविड। व्यक्तिगत रूप से प्रत्येक चरित्र को सेट करना एक समस्या का बहुत अधिक नहीं है, यह सिर्फ इतना है कि प्रत्येक चरित्र को एक नई शैली की आवश्यकता होती है जो संभवतः किसी अन्य पात्र के साथ साझा नहीं की जाती है। मुझे इसे कई हजार पात्रों के लिए करने की ज़रूरत है और इसलिए सैकड़ों शैलियों की आवश्यकता होगी। मैं हर बार एक नई नामित शैली जोड़ने के बिना ऐसा करना चाहता हूं। यद्यपि केवल कुछ अग्रभूमि रंगों में से एक होगा, इसलिए मुझे लगता है कि मैं टेक्स्ट को उस पृष्ठभूमि पर रख सकता हूं जिसे मैं स्वयं प्रस्तुत करता हूं। – JaredL

0

मुझे लगता है कि सबसे अच्छा तरीका है कि आप यह करना है जैसे हम प्रकाश डाला, पात्रों के लिए पीछा नहीं है, लेकिन एक पैटर्न होने के साथ संपादकों में है, उदाहरण के लिए:

private static HashMap<Pattern, Color> patternColors; 
private static String GENERIC_XML_NAME = "[A-Za-z]+[A-Za-z0-9\\-_]*(:[A-Za-z]+[A-Za-z0-9\\-_]+)?"; 
private static String TAG_PATTERN = "(</?" + GENERIC_XML_NAME + ")"; 
private static String TAG_END_PATTERN = "(>|/>)"; 
private static String TAG_ATTRIBUTE_PATTERN = "(" + GENERIC_XML_NAME + ")\\w*\\="; 
private static String TAG_ATTRIBUTE_VALUE = "\\w*\\=\\w*(\"[^\"]*\")"; 
private static String TAG_COMMENT = "(<\\!--[\\w ]*-->)"; 
private static String TAG_CDATA = "(<\\!\\[CDATA\\[.*\\]\\]>)"; 

private static final Color COLOR_OCEAN_GREEN = new Color(63, 127, 127); 
private static final Color COLOR_WEB_BLUE = new Color(0, 166, 255); 
private static final Color COLOR_PINK = new Color(127, 0, 127); 

static { 
    // NOTE: the order is important! 
    patternColors = new LinkedHashMap<Pattern, Color>(); 
    patternColors.put(Pattern.compile(TAG_PATTERN), Color.BLUE); // COLOR_OCEAN_GREEN | Color.BLUE 
    patternColors.put(Pattern.compile(TAG_CDATA), COLOR_WEB_BLUE); 
    patternColors.put(Pattern.compile(TAG_ATTRIBUTE_PATTERN), COLOR_PINK); 
    patternColors.put(Pattern.compile(TAG_END_PATTERN), COLOR_OCEAN_GREEN); 
    patternColors.put(Pattern.compile(TAG_COMMENT), Color.GRAY); 
    patternColors.put(Pattern.compile(TAG_ATTRIBUTE_VALUE), COLOR_OCEAN_GREEN); //Color.BLUE | COLOR_OCEAN_GREEN 
} 




public XmlView(Element element) { 

    super(element); 

    // Set tabsize to 4 (instead of the default 8). 
    getDocument().putProperty(PlainDocument.tabSizeAttribute, 4); 
} 
संबंधित मुद्दे

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