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);
+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. –
Tìm thấy câu trả lời ở đây ... http: // stackoverflow.com/questions/9502654/underline-styleconstant-in-a-different-color-với-attributeset –
Đăng câu trả lời –