您的位置:首页 > 编程语言

关于操作字体的代码

2007-10-02 01:46 127 查看
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class TxtFont extends JFrame implements ListSelectionListener{
    JLabel l=new JLabel("示例文本 Simple Text");
    JList j1=new JList();
    JList j2=new JList();
   
    JScrollPane jScrollPane1=new JScrollPane();
    JScrollPane jScrollPane2=new JScrollPane();
   
    JPanel p1=new JPanel();
    JPanel p2=new JPanel();
   
    TxtFont(){
        this.setLayout(new BorderLayout());
        this.add(p1,BorderLayout.NORTH);
        this.add(p2,BorderLayout.SOUTH);
        p1.add(l);
       
        jScrollPane1.getViewport().add(j1);
        p2.add(jScrollPane1);
        jScrollPane2.getViewport().add(j2);
        p2.add(jScrollPane2);
       
        GraphicsEnvironment env=GraphicsEnvironment.getLocalGraphicsEnvironment();
        String fontNames[]=env.getAvailableFontFamilyNames();
        j1.setListData(fontNames);
       
        String fontSizes[]={"9","10","11","12","14","16","18","20","24","26","28","30","32"};
        j2.setListData(fontSizes);
       
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setTitle("设置文本字体");
        this.setSize(350,300);
        this.setVisible(true);
        j1.addListSelectionListener(this);
        j2.addListSelectionListener(this);
    }
   
    public void valueChanged(ListSelectionEvent e){
        if(e.getSource().equals(j2))
        {
            int newFontSize=Integer.parseInt((String)j2.getSelectedValue());
            Font f=l.getFont();
            Font newFont=new Font(f.getName(),f.getStyle(),newFontSize);
            l.setFont(newFont);
        }
        else
        {
            String newFontName=(String)j1.getSelectedValue();
            Font f=l.getFont();
            Font newFont=new Font(newFontName,f.getStyle(),f.getSize());
            l.setFont(newFont);
        }
    }
   
    public static void main(String[] args){
        new TxtFont();
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string import class
相关文章推荐