现在的位置: 首页 > 综合 > 正文

使用JTextPane代替JTextArea实现每一行的字的颜色不一样

2018年01月30日 ⁄ 综合 ⁄ 共 1070字 ⁄ 字号 评论关闭

 

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;

public class TestJTextPane extends JFrame {
   private JTextPane textPane = new JTextPane(); 
   
   public TestJTextPane () {
      this.add(new JScrollPane(textPane), BorderLayout.CENTER);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setSize(800, 600);
      this.setLocationRelativeTo(null);

        Style def = textPane.getStyledDocument().addStyle(null, null);
        StyleConstants.setFontFamily(def, "verdana");
        StyleConstants.setFontSize(def, 12);
        Style normal = textPane.addStyle("normal", def);
        Style s = textPane.addStyle("red", normal);
        StyleConstants.setForeground(s, Color.RED);
        textPane.setParagraphAttributes(normal, true);
      
      for(int i=0; i<1000; i++){
           try {
            textPane.getDocument().insertString(textPane.getDocument().getLength(),
                  "hello, ", textPane.getStyle(i%2 == 0 ? "red" : "normal"));
         } catch (BadLocationException e1) {
         }
      }
   }
   
   public static void main(String[] args) {
      new TestJTextPane().setVisible(true);
   }

}

 

抱歉!评论已关闭.