创世雨天 2007-11-28 13:16
java怎么改变字体
[attach]17500[/attach]
jLabel3.setFont(new java.awt.Font("宋体", Font.BOLD | Font.ITALIC, 12));
jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
jLabel3.setText("MyQQ密码");
用Borland JBuilder软件 可以更方便的自动生成代码 来调试
[attach]17501[/attach]
[attach]17502[/attach]
[color=blue]以下是手工代码[/color]
可以使用[color=red]setFont(Font font)方法:[/color]
其中:
setFont(Font font)的方法是:javax.swing.JComponent类中的方法;
Font是:Fontjava.awt 类中的Class Font类;
Font构造器:Font(String name, int style, int size)
@param name 设置字体
@param style 设置字体模式:BOLD,ITALIC....
@param size 设置字体大小
通过JComponent派生的类,都可以使用setFont来设置文字.
举例:
JLabel label = new JLael("Welcome to ChinaJavaWorld!");
label.setFont(new Font("宋体",Font.BOLD,20));
[color=red]JtextPane[/color]
部分代码,主要是insertString必须对应一种Style
_doc = _textPane.getStyledDocument();
addStylesToDocument(_doc); //风格设置
_docStyle = _doc.getStyle(Chat.DEFAULT_STYLE); //默认风格
public static void addStylesToDocument(StyledDocument doc) {
Style def = StyleContext.getDefaultStyleContext().getStyle(
StyleContext.DEFAULT_STYLE);
Style parent = doc.addStyle("parent",def);
StyleConstants.setFontFamily(def,"宋体");
StyleConstants.setFontSize(def,12);
Style s = doc.addStyle(Chat.DEFAULT_STYLE,parent);
StyleConstants.setForeground(s,Color.BLACK);
s = doc.addStyle(Chat.ADMIN_STYLE,parent);
StyleConstants.setForeground(s,Color.GREEN.brighter());
StyleConstants.setBold(s,true);
s = doc.addStyle(Chat.RED_STYLE,parent);
StyleConstants.setForeground(s,Color.RED.brighter());
s = doc.addStyle(Chat.BLUE_STYLE,parent);
StyleConstants.setForeground(s,Color.BLUE.brighter());
s = doc.addStyle(Chat.ORG_STYLE,parent);
StyleConstants.setForeground(s,Color.orange.brighter());
}
_doc.insertString(_doc.getLength(), line.substring(1) + "\n",
_docStyle);