2012-08-22 58 views
5

Chỉ cần cố gắng tô màu văn bản trong JTextPane - nhưng vấn đề không thể có các màu khác nhau cho văn bản và gạch dưới. Làm thế nào tôi nên làm điều đó hoặc là thậm chí có thể? Ví dụ dưới đây in tất cả các văn bản và gạch chân trong RED.Làm cách nào để đặt các màu khác nhau cho văn bản và gạch dưới trong 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 Mặc dù API kiểu dường như có thể mở rộng, nhưng tôi không thể tìm thấy bất kỳ tài liệu nào về cách thực hiện. –

+0

Tìm thấy câu trả lời ở đây ... http: // stackoverflow.com/questions/9502654/underline-styleconstant-in-a-different-color-với-attributeset –

+0

Đăng câu trả lời –

Trả lời

2
  • for example, và for Html

  • Document mô hình lợi nhuận để xem, có thể xác định chỉ số nơi hàng bắt đầu/kết thúc

4

Về cơ bản có 3 lớp bạn cần tạo:

  • Bạn cần mở rộng javax.swing.text.LabelView để thực hiện sửa đổi chế độ xem theo bất kỳ cách nào bạn muốn (cho dù đó là thêm gạch chân có màu hay không). Bạn sẽ ghi đè phương thức paint(Graphics, Shape). Bạn có thể truy cập các thuộc tính với dòng này trong lớp được ghi đè - các thuộc tính sẽ là trình kích hoạt để thực hiện một số thứ bổ sung cho văn bản (như thêm một gạch dưới).

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

  • Bạn cần phải tạo ra một mới ViewFactory và ghi đè lên các phương pháp create. Điều quan trọng là khi làm điều này bạn xử lý tất cả các loại nguyên tố là (nếu không mọi thứ sẽ không hoàn toàn

hiển thị đúng. Bạn cần phải tạo một StyledEditorKit để làm cho cửa sổ mà ViewFactory để sử dụng. Dưới đây là một ví dụ đơn giản và Runnable điều này:

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

     } 

    } 

} 

Dưới đây là các liên kết đến một mẫu mã:

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

Câu trả lời này chủ yếu dành cho hậu thế, tôi cho rằng việc thêm phiên bản đơn giản của mã được liên kết và giải thích sẽ giúp mọi thứ dễ hiểu hơn.

+0

được mô tả tốt +1 – mKorbel

Các vấn đề liên quan