我正在扫描具有不同扩展名的文件,并将它们放在Java中的String ArrayList中(带扩展名的名称)。我希望通过查看例如.xls扩展名为绿色,.txt扩展名为蓝色的扩展名,以不同的颜色打印到JTextArea中。这是我的代码如下;
public void setScanResult(ArrayList<String> x)
{
textArea.append("|");
for (int i = 0; i < x.size(); i++) {
textArea.append("\n");
textArea.append((String) x.get(i));
}
x.clear();
}
答案 0 :(得分:1)
要使文字着色,请使用JTextPane
代替JTextArea
,这是一个简单的示例:
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
public class Example extends JFrame{
private JTextPane pane;
public Example(){
init();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private void init() {
JScrollPane p = new JScrollPane(pane = new JTextPane());
add(p);
List<String> files = new ArrayList<>();
files.add("1.txt");
files.add("2.txt");
files.add("3.doc");
files.add("4.xls");
for(String s : files){
addText(s);
}
}
private void addText(String s) {
Color color = getColor(s);
StyleContext style = StyleContext.getDefaultStyleContext();
AttributeSet attrs = style.addAttribute(SimpleAttributeSet.EMPTY,StyleConstants.Foreground, color);
pane.setCaretPosition(pane.getDocument().getLength());
pane.setCharacterAttributes(attrs , false);
pane.replaceSelection(s+"\n");
}
private Color getColor(String s) {
return s.endsWith(".txt") ? Color.RED : (s.endsWith(".doc") ? Color.GREEN : Color.CYAN );
}
public static void main(String... s){
new Example();
}
}
答案 1 :(得分:1)
好的,现在我可以识别文件扩展名,但是当我打印到TextArea时仍然无法给出颜色,是因为TextArea和所有字体在运行程序后变为 *“serif”,Font.ROMAN_BASELINE,5 * 强>
public void setScanResult(ArrayList<String> x) {
for (int i = 0; i < x.size(); i++) {
if (x.get(i).endsWith(".xls")) {
Font fono1 = new Font("sansserif", Font.BOLD, 50);
textArea.setFont(fono1);
textArea.append((String) x.get(i));
}
else if (x.get(i).endsWith(".exe")) {
Font fono2 = new Font("Monospaced", Font.ITALIC, 10);
textArea.setFont(fono2);
textArea.append((String) x.get(i));
} else {
Font fono3 = new Font("serif", Font.ROMAN_BASELINE, 5);
textArea.setFont(fono3);
textArea.append((String) x.get(i));
}
textArea.append("\n");
}
x.clear();
}
答案 2 :(得分:0)
以下是从fileName
识别扩展名的代码。在知道扩展名后,可以将颜色应用于文本,如@ alex2410
public class Test {
public static void main(String[] args) {
ArrayList<String> t = new ArrayList<>();
t.add("A.txt");
t.add("b.pdf");
t.add("c.exe");
t.add("d.xls");
t.add("e.xlsx");
for (String fileName : t) {
int code = getExtentionCode(fileName);
fillColor(fileName, code);
}
}
private static void fillColor(String fileName, int code) {
// change color as per requirement
switch (code) {
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
// unknown extention
break;
default:
break;
}
}
private static int getExtentionCode(String fileName) {
int code = 5;
if (fileName.endsWith(".txt")) {
code = 0;
} else if (fileName.endsWith(".pdf")) {
code = 1;
} else if (fileName.endsWith(".exe")) {
code = 2;
} else if (fileName.endsWith(".xls")) {
code = 3;
} else if (fileName.endsWith(".xlxs")) {
code = 4;
} else {
code = 5;
}
return code;
}
}