//设置字体,总是不成功,自己试了下,怎么弄字体就是不变,感觉setFont方法有问题import java.awt.*;          
import java.awt.event.*;    
 import java.awt.Component; 
 class H extends Frame implements ActionListener
 {
  static H f1=new H();          
  static TextArea ta=new TextArea();          
  static Button b1=new Button("黑体");          
  static Button b2=new Button("楷体");          
  static Button b3=new Button("宋体");                  
   public static void main (String as[])
   {
    b1.addActionListener(f1);               
        b2.addActionListener(f1);          
        b3.addActionListener(f1);          
        f1.setBounds(0,0,200,200);          
        f1.setLayout(new FlowLayout());          
        f1.add(b1);          
        f1.add(b2);          
        f1.add(b3);          
        f1.add(ta);          
        f1.setVisible(true);          
        }
  public void actionPerformed(ActionEvent e)
  {
   Button b=(Button)e.getSource();          
   if(b==b1)
   ta.setFont(new Font("黑体",Font.PLAIN,20));         
    else if(b==b2)
   ta.setFont(new Font("楷体",Font.PLAIN,20));          
   else
   ta.setFont(new Font("宋体",Font.PLAIN,20));      
     }
}java

解决方案 »

  1.   

    把代码里面的awt包内容换成swing的就可以了,即把Button和TextArea换成JButton和JTextArea。AWT组件只支持逻辑字体,能改变大小。swing就能修改字体了。
    package csdn.programbbs_605;import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;@SuppressWarnings("serial")
    public class FontTest extends JFrame {
    static JTextArea ta = new JTextArea("哈哈");
    static JButton b1 = new JButton("隶书");
    static JButton b2 = new JButton("华文行楷");
    static JButton b3 = new JButton("宋体");
    public FontTest(){
    setBounds(0, 0, 200, 200);
    setLayout(new FlowLayout());
    add(b1);
    add(b2);
    add(b3);
    add(ta);
    b1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    ta.setFont(new Font("隶书", Font.PLAIN, 20));
    }
    });
    b2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    ta.setFont(new Font("华文行楷", Font.PLAIN, 50));
    }
    });
    b3.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    ta.setFont(new Font("宋体", Font.PLAIN, 70));
    }
    });

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } public static void main(String[] args) {
    FontTest ft = new FontTest();
    ft.setVisible(true);
    }
    }