日期:2014-05-20  浏览次数:20768 次

用java写了一个记事本的字体对话框,可是当改变字体类型(style)时总不改变,不知道是哪里的问题,大家帮忙看看
Java code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FontWin extends Frame implements ItemListener,ActionListener
{
    Label lfont=new Label("字体:"),
          lglyph=new Label("字形:"),
          lsize=new Label("大小:"),
          label=new Label(),
          label_1 = new Label("预览:");
    Button submit=new Button("确定"),
           resert=new Button("取消");
    TextField 字体=new TextField(),
              字形=new TextField(),
              大小=new TextField();
    List  list1=new List(7,false),
          list2=new List(7,false),
          list3=new List(7,false);
    Font f;
    FontWin(String s)
    {
        super(s);
        setResizable(false);
        setBounds(100, 100, 486,475);

        Panel panel = new Panel();
        panel.setLayout(null);
        GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
        String fontName[]=ge.getAvailableFontFamilyNames();
        String[] fontGlyph={"常规","斜体","粗体","粗斜体"};
        String[] fontSize={"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"};
        for(int i=0;i<fontName.length;i++)
        {
            list1.add(fontName[i]);
        }
        for(int i=0;i<fontGlyph.length;i++)
        {
            list2.add(fontGlyph[i]);
        }
        for(int i=0;i<fontSize.length;i++)
        {
            list3.add(fontSize[i]);
        }
        lfont.setBounds(10, 11, 35, 20);
        panel.add(lfont);
        字体.setBounds(10, 32, 120, 21);
        panel.add(字体);
        list1.setBounds(10,54,120,200);
        panel.add(list1);
        lglyph.setBounds(151, 11, 35, 20);
        panel.add(lglyph);
        字形.setBounds(151, 32, 120, 21);
        panel.add(字形);
        list2.setBounds(151,54,120,200);
        panel.add(list2);
        label_1.setBounds(151, 255, 35, 20);
        panel.add(label_1);
        label.setBounds(151, 271, 207, 63);
        label.setBackground(new Color(255, 255, 255));
        label.setText("字体样式 AaBbCc");
        label.setBackground(Color.green);
        panel.add(label);
        lsize.setBounds(285, 12, 35, 20);
        panel.add(lsize);
        大小.setBounds(285, 32, 73, 21);
        panel.add(大小);
        list3.setBounds(285,54,120,200);
        panel.add(list3);
        submit.setBounds(154, 340, 100, 25);
        submit.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent arg0) {
                setVisible(false);   
               }
              });
        panel.add(submit);
        resert.setBounds(258, 340, 100, 25);
        panel.add(resert);
        add(panel, BorderLayout.CENTER);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        }); //关闭窗口事件
        list1.addItemListener(this);
        list2.addItemListener(this);
        list3.addItemListener(this);
        submit.addActionListener(this);
        resert.addActionListener(this);
        setVisible(true);
        validate();
    }
    public void itemStateChanged(ItemEvent e)
    {
        String name=list1.getSelectedItem();
        String style=list2.getSelectedItem();
        String size=list3.getSelectedItem();
        字体.setText(name);
        字形.setText(style);
        大小.setText(size);
        int intstyle=0;
        int intSize = Integer.parseInt(size);
        if(style.equals("常规"))
                intstyle=Font.PLAIN;
        if(style.equals("粗体"))
                intstyle=Font.BOLD;
        if(style.equals("斜体"))
                intstyle=Font.ITALIC;
        if(style.equals("粗斜体"))
                intstyle=Font.BOLD+Font.ITALIC;
         f = new Font("name",intstyle,intSize);
        label.setFont(f);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==resert)
            System.exit(0);
    }
}
public class w2
{
    public static void main(String[] args)
    {
        FontWin f=new FontWin("字体");
    } 
}