Java Swing中可以给每个控件设置字体格式和其他属性的设置,示例如下:
submit= new JButton("登陆");
三个参数分别表示: 字体,样式(粗体,斜体等),字号
submit.setForeground(Color.RED);
这个表示给组件上的文字设置颜色Color.RED表示红色
Java设置label字体代码如下:
ublic?class?SetColor?extends?JFrame{
JLabel?jlabel?=?new?JLabel("颜色,大小");
public?SetColor(){
this.setLayout(null);
jlabel.setForeground(Color.BLUE);//设置字体颜色
this.add(jlabel);
this.setVisible(true);
}
/**
*/
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
SetColor?sc?=?new?SetColor();
}}
用样式定义,如:对p标签下的字体:p {font-family: "微软雅黑,新宋体,宋体";},而且可以定义多个字体,如果第一个字体支持就用第一个,第一个没有用第二个,依次类推.
①.、window --Preference
复制以下代码,修改相应地方即可:
private?static?void?loadIndyFont()
{
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Insets;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
public class main {
static String tf_str=null;
public static void main(String[] args) {
Frame a = new Frame("打印");
a.setLayout(new FlowLayout());
Button c = new Button("确定");
Button e = new Button("红色");
Button f = new Button("蓝色");
JTextPane d=new JTextPane();
d.setMargin(new Insets(100,100, 100, 100));
a.add(b);
a.add(c);
a.add(d);
a.add(e);
a.add(f);
a.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
});
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
d.setText("");
tf_str = b.getText().trim();
b.setText("");
appendToPane(d, tf_str, Color.black);
b.requestFocus();
e.addActionListener(new ActionListener() {
appendToPane(d, tf_str, Color.RED);
f.addActionListener(new ActionListener() {
appendToPane(d, tf_str, Color.BLUE);
a.setVisible(true);
private static void appendToPane(JTextPane tp, String msg, Color c) {
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
aset = sc.addAttribute(aset, StyleConstants.FontFamily, "宋体");
aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);
int len = tp.getDocument().getLength();
tp.setCaretPosition(len);
tp.setCharacterAttributes(aset, false);
tp.replaceSelection(msg);
以上就是土嘎嘎小编为大家整理的java程序改变字体代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!