Có cách nào đơn giản để phân tích màu Ansi trong tệp nhật ký và sử dụng nó trong trường văn bản trong Swing (JTextArea, JTextPAne, ...) không?ANSI màu sắc trong Java Swing trường văn bản
Trả lời
Không thử nó, nhưng có some code here (mà cần một số định dạng để nhìn đẹp) mà tuyên bố là một lớp con ANSI màu JTextPane ...
Đối với hậu thế, đây là lớp chạy qua NetBeans để định dạng mã
import javax.swing.*;
import javax.swing.text.*;
import java.awt.Color;
public class ColorPane extends JTextPane {
static final Color D_Black = Color.getHSBColor(0.000f, 0.000f, 0.000f);
static final Color D_Red = Color.getHSBColor(0.000f, 1.000f, 0.502f);
static final Color D_Blue = Color.getHSBColor(0.667f, 1.000f, 0.502f);
static final Color D_Magenta = Color.getHSBColor(0.833f, 1.000f, 0.502f);
static final Color D_Green = Color.getHSBColor(0.333f, 1.000f, 0.502f);
static final Color D_Yellow = Color.getHSBColor(0.167f, 1.000f, 0.502f);
static final Color D_Cyan = Color.getHSBColor(0.500f, 1.000f, 0.502f);
static final Color D_White = Color.getHSBColor(0.000f, 0.000f, 0.753f);
static final Color B_Black = Color.getHSBColor(0.000f, 0.000f, 0.502f);
static final Color B_Red = Color.getHSBColor(0.000f, 1.000f, 1.000f);
static final Color B_Blue = Color.getHSBColor(0.667f, 1.000f, 1.000f);
static final Color B_Magenta = Color.getHSBColor(0.833f, 1.000f, 1.000f);
static final Color B_Green = Color.getHSBColor(0.333f, 1.000f, 1.000f);
static final Color B_Yellow = Color.getHSBColor(0.167f, 1.000f, 1.000f);
static final Color B_Cyan = Color.getHSBColor(0.500f, 1.000f, 1.000f);
static final Color B_White = Color.getHSBColor(0.000f, 0.000f, 1.000f);
static final Color cReset = Color.getHSBColor(0.000f, 0.000f, 1.000f);
static Color colorCurrent = cReset;
String remaining = "";
public void append(Color c, String s) {
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
int len = getDocument().getLength(); // same value as getText().length();
setCaretPosition(len); // place caret at the end (with no selection)
setCharacterAttributes(aset, false);
replaceSelection(s); // there is no selection, so inserts at caret
}
public void appendANSI(String s) { // convert ANSI color codes first
int aPos = 0; // current char position in addString
int aIndex = 0; // index of next Escape sequence
int mIndex = 0; // index of "m" terminating Escape sequence
String tmpString = "";
boolean stillSearching = true; // true until no more Escape sequences
String addString = remaining + s;
remaining = "";
if (addString.length() > 0) {
aIndex = addString.indexOf("\u001B"); // find first escape
if (aIndex == -1) { // no escape/color change in this string, so just send it with current color
append(colorCurrent,addString);
return;
}
// otherwise There is an escape character in the string, so we must process it
if (aIndex > 0) { // Escape is not first char, so send text up to first escape
tmpString = addString.substring(0,aIndex);
append(colorCurrent, tmpString);
aPos = aIndex;
}
// aPos is now at the beginning of the first escape sequence
stillSearching = true;
while (stillSearching) {
mIndex = addString.indexOf("m",aPos); // find the end of the escape sequence
if (mIndex < 0) { // the buffer ends halfway through the ansi string!
remaining = addString.substring(aPos,addString.length());
stillSearching = false;
continue;
}
else {
tmpString = addString.substring(aPos,mIndex+1);
colorCurrent = getANSIColor(tmpString);
}
aPos = mIndex + 1;
// now we have the color, send text that is in that color (up to next escape)
aIndex = addString.indexOf("\u001B", aPos);
if (aIndex == -1) { // if that was the last sequence of the input, send remaining text
tmpString = addString.substring(aPos,addString.length());
append(colorCurrent, tmpString);
stillSearching = false;
continue; // jump out of loop early, as the whole string has been sent now
}
// there is another escape sequence, so send part of the string and prepare for the next
tmpString = addString.substring(aPos,aIndex);
aPos = aIndex;
append(colorCurrent, tmpString);
} // while there's text in the input buffer
}
}
public Color getANSIColor(String ANSIColor) {
if (ANSIColor.equals("\u001B[30m")) { return D_Black; }
else if (ANSIColor.equals("\u001B[31m")) { return D_Red; }
else if (ANSIColor.equals("\u001B[32m")) { return D_Green; }
else if (ANSIColor.equals("\u001B[33m")) { return D_Yellow; }
else if (ANSIColor.equals("\u001B[34m")) { return D_Blue; }
else if (ANSIColor.equals("\u001B[35m")) { return D_Magenta; }
else if (ANSIColor.equals("\u001B[36m")) { return D_Cyan; }
else if (ANSIColor.equals("\u001B[37m")) { return D_White; }
else if (ANSIColor.equals("\u001B[0;30m")) { return D_Black; }
else if (ANSIColor.equals("\u001B[0;31m")) { return D_Red; }
else if (ANSIColor.equals("\u001B[0;32m")) { return D_Green; }
else if (ANSIColor.equals("\u001B[0;33m")) { return D_Yellow; }
else if (ANSIColor.equals("\u001B[0;34m")) { return D_Blue; }
else if (ANSIColor.equals("\u001B[0;35m")) { return D_Magenta; }
else if (ANSIColor.equals("\u001B[0;36m")) { return D_Cyan; }
else if (ANSIColor.equals("\u001B[0;37m")) { return D_White; }
else if (ANSIColor.equals("\u001B[1;30m")) { return B_Black; }
else if (ANSIColor.equals("\u001B[1;31m")) { return B_Red; }
else if (ANSIColor.equals("\u001B[1;32m")) { return B_Green; }
else if (ANSIColor.equals("\u001B[1;33m")) { return B_Yellow; }
else if (ANSIColor.equals("\u001B[1;34m")) { return B_Blue; }
else if (ANSIColor.equals("\u001B[1;35m")) { return B_Magenta; }
else if (ANSIColor.equals("\u001B[1;36m")) { return B_Cyan; }
else if (ANSIColor.equals("\u001B[1;37m")) { return B_White; }
else if (ANSIColor.equals("\u001B[0m")) { return cReset; }
else { return B_White; }
}
}
tim_yates: Tôi đã kiểm tra mã từ liên kết của bạn và nó đang hoạt động :) – jacbar
Bạn có thể rất tử tế để đăng hoặc cung cấp liên kết đến phiên bản được định dạng đúng của lớp Java này không? –
@ r-campbell ở đó bạn đi ... –
Nếu bạn đang JTextPane là không thể chỉnh sửa sau đó tim_yates phương pháp sẽ không làm việc, để cải tiến phương pháp của ông tôi đã thay thế phương pháp append của mình với
public void append(Color c, String s) {
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
int len = getDocument().getLength();
try {getDocument().insertString(len, s, aset);}
catch (BadLocationException e) {e.printStackTrace();}
}
Phương pháp này có vẻ là nhiều mạnh mẽ hơn và làm việc tại tất cả các trường hợp tôi đã cố gắng, nhưng tôi đã không kiểm tra nó đầy đủ
Phương pháp này hoạt động bằng cách chèn văn bản màu ở phần cuối của tài liệu hơn thay thế văn bản không tồn tại, mà nếu nó không thể chỉnh sửa chỉ dẫn đến một âm thanh lỗi lặp đi lặp lại của cửa sổ khá khó chịu
- 1. Màu sắc nút Java Swing
- 2. qt vẽ văn bản đầy màu sắc
- 3. Thay đổi màu văn bản trong trường văn bản
- 4. Màu sắc trong bảng điều khiển Java
- 5. Codes ANSI Màu trong VIM
- 6. Màu sắc của văn bản trong một cảnh báo Javascript
- 7. Màu sắc một phần của văn bản trong matplotlib
- 8. matplotlib: thay đổi tiêu đề và màu sắc văn bản và màu sắc đánh dấu
- 9. UILabel với văn bản của hai màu sắc khác nhau
- 10. Android: chuyển đổi màu sắc văn bản của ToggleButton
- 11. Whats phông chữ và màu sắc của văn bản UITableViewCell?
- 12. Hộp văn bản hoàn toàn tự động trong Java Swing
- 13. Java - thiết lập Phông chữ/Màu sắc trong setBorder
- 14. Java Swing: Chuyển văn bản trên các mục JComboBox?
- 15. In nhiều hơn các giá trị màu ANSI trong Perl
- 16. Đọc và Viết văn bản trong định dạng ANSI
- 17. Văn bản màu JComboBox hai màu
- 18. Thay đổi màu và màu của văn bản trong hộp văn bản
- 19. Tìm ra màu sắc văn bản dựa trên quan điểm nền iOS màu
- 20. Đặt màu phông chữ Java trong trường văn bản bị tắt
- 21. Pylab: nhãn bản đồ cho màu sắc
- 22. Màu sắc trong MathJax
- 23. Làm thế nào để thay đổi màu sắc văn bản spinner
- 24. Thay đổi màu sắc văn bản tấn công qua trong css
- 25. Java Swing Text Editor
- 26. jQuery: màu văn bản animate cho trường nhập liệu?
- 27. Java Swing FlowLayout Alignments
- 28. Swing - MaskFormatter - Nhập số từ bên phải của trường văn bản
- 29. Màu văn bản của trường nhập liệu CSS của văn bản được nhập
- 30. Thêm trường văn bản vào Đồ họa trong java
Tôi nghi ngờ có một cách đơn giản *. Bạn đã xem xét một số trình giả lập thiết bị đầu cuối nguồn mở chưa? Ngoài ra, những gì đang tạo ra các tệp nhật ký? – Perception