java的控制台输出红色字体的解决办法:
创新互联建站是一家专注于成都做网站、网站制作与策划设计,墨玉网站建设哪家好?创新互联建站做网站,专注于网站建设十多年,网设计领域的专业建站公司;建站业务涵盖:墨玉等地区。墨玉做网站价格咨询:13518219792
打开MyEclipse的window -- Preferences-- Run/Debug--console,如下图,自行设置下颜色即可:
字体大小及颜色
a:Java代码区域的字体大小和颜色:
window -- Preferences -- General -- Appearance -- Colors And Fonts -- Java修改 -- Java Edit Text Font
b:控制台
window -- Preferences -- General -- Appearance -- Colors And Fonts -- Debug -- Console font
c:其他文件
window -- Preferences -- General -- Appearance -- Colors And Fonts -- Basic -- Text Font
简单设置如下:
jlabel.setFont(new java.awt.Font("Dialog", 1, 15));
“dialog”代表字体,1代表样式(1是粗体,0是平常的)15是字号
//设置字体
jlabel.setForeground(Color.red);
//设置颜色
java读取word文件,并输出红色字体,主要采用的是开源的读取框架,例如Jword,示例如下:
package com.xxx.common;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
public class WordUtil {
private static Document document;
private static BaseFont baseFont;
//创建word,并设置纸张文档
private static void openWordFile(String fileName) throws DocumentException,
IOException {
document = new Document(PageSize.A4);
RtfWriter2.getInstance(document, new FileOutputStream(fileName));
document.open();
baseFont = BaseFont.createFont();
}
//设置标题
private static boolean setTitle(String title) throws DocumentException {
Font font = new Font(baseFont, 12, Font.BOLD);
Paragraph pTitle = new Paragraph(title + "\n");
pTitle.setFont(font);
pTitle.setAlignment(Element.ALIGN_CENTER);
return document.add(pTitle);
}
//设置文档内容,以及字体颜色
private static boolean setContent(String content) throws DocumentException {
Font font = new Font(baseFont, 10, Font.NORMAL);
Paragraph pContent = new Paragraph(content);
pContent.setFont(font);
pContent.setAlignment(Element.ALIGN_LEFT);
pContent.setSpacingAfter(5);
pContent.setFirstLineIndent(20);
return document.add(pContent);
}
//对外使用的接口
public static boolean CreateWordFile(String url, String title,
ListString contents) {
boolean returnValue = false;
try {
openWordFile(url);
returnValue = setTitle(title);
for (int i = 0; i contents.size(); i++) {
returnValue = returnValue setContent(contents.get(i));
}
document.close();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return returnValue;
}
//对外使用的接口
public static boolean CreateWordFile(String url, String title,
String content) {
boolean returnValue = false;
try {
openWordFile(url);
returnValue = setTitle(title);
returnValue = returnValue setContent(content);
document.close();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return returnValue;
}
public static void main(String[] args ) {
WordUtil wordUtil = new WordUtil();
ListString strList = new ArrayListString();
//传入内容为字符串
wordUtil.CreateWordFile("e:\\word.doc", "标题居中", "我爱Java");
//传入内容为字符串List
//wordUtil.CreateWordFile("e:\\word.doc", "标题居中", strList);
}
}
使用 e.getActionCommand().equals("蓝色") 判断。
另外,两层if判断是多余的,应该去掉。直接使用text.setForeground(Color.BLUE)这样子。