有没有什么简单的方法来分析日志文件中的ANSI颜色,并在Swing使用它在文本字段(的JTextArea,JTextPane中,...)?ANSI颜色的Java Swing文本字段
5
A
回答
7
没有尝试过,但有some code here(这需要一些格式化,以看起来不错),它声称自己是一个ANSI彩色JTextPane的子类...
对于后人,这里是通过NetBeans运行类格式化代码
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; }
}
}
2
如果youre的JTextPane是不可编辑,然后tim_yates方法是行不通的,在他的方法来提高我取代他的append方法与
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();}
}
这种方法似乎更加稳健,并在所有的我都试过了的情况下工作,但我还没有完全
这种方法在文件末尾的工作原理是将彩色文本,而不是测试它替换文本不存在的,如果它是不可编辑刚刚导致重复的Windows错误的声音是很烦人
相关问题
- 1. 在Swing文本字段中延迟文本颜色更改
- 2. iOS 8文本字段色调颜色
- 3. Java在禁用SWT文本字段中设置文本颜色
- 4. Java Swing按钮颜色
- 5. Java Swing文本字段高度
- 6. 文本字段的背景颜色:focus
- 7. Java-Swing:使用HTML设置选定的文本颜色
- 8. Java - Swing将颜色设置为JTextArea中的文本
- 9. iphone ipad更改文本字段颜色
- 10. MaterialiseCSS Angular2文本字段颜色
- 11. 文本字段输入颜色css
- 12. VBScript到颜色输入文本字段
- 13. Jenkins + Python + MSBuild =否ANSI颜色
- 14. 更改Javafx文本字段中文本的颜色WHILE打字
- 15. ANSI颜色C和ncurses的
- 16. Python ANSI颜色代码
- 17. 的Java Swing:改变的JProgressBar的颜色
- 18. emacs ansi-term背景颜色
- 19. IntelliJ呈现纯文本文件中的ANSI颜色
- 20. 使用bash删除文本文件中的ANSI颜色代码
- 21. 用Qt4渲染ANSI颜色?
- 22. 更改JTextPane中段落的背景颜色(Java Swing)
- 23. JButton更改文本字段的文本颜色
- 24. 如何更改InfoPath文本字段的文本颜色?
- 25. 更改文本字段的文本颜色
- 26. 动态更改文本字段的文本颜色
- 27. CSS输入文本的输入字段文本颜色
- 28. 文本字段的文本颜色变化
- 29. Java Swing - 数字的良好文本字段组件?
- 30. 用JComboBox Java Swing挑选颜色
我怀疑是有* *简单的方式。你看过那里的一些开源终端模拟器吗?另外,什么是生成日志文件? – Perception