我现在做个聊天室,想改变下字体,用那QQ那设置字体的代码怎么写?

解决方案 »

  1.   

    看这个帖子
    java字体设置,包括大小,颜色,加粗,下划线,对齐,斜体的设置,很全!!
      

  2.   

    应该有个字体什么的包!类似于C:\Windows\Fonts写的东西
      

  3.   


    这个看来对你有用哈~~~
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */package changefront;import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.EventQueue;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.GraphicsEnvironment;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JColorChooser;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;/**
     *
     * @author Administrator
     */
    public class ArtFont extends JFrame implements ActionListener{
    JComboBox fontType,fontSize;
    JCheckBox boldBx;//粗体按钮
    JCheckBox italicBx;//斜体按钮
    JButton colorBtn;//颜色按钮
    String[] fontNames;//字体名称
    String[] fontSizes;//字体尺寸
    JLabel label;//输入提示标签
    JTextField inputText;//文字输入框
    JTextArea txtArea;//文字显示区
    JPanel fontPanel;//字体设置
    JPanel showPanel;//显示效果区
    Font font;
    int boldStyle,italicStyle,underlineStyle;
    int fontSizeStyle;
    String fontNameStyle;
    Color colorStyle=Color.black;//设置字体的默认颜色为黑色
    public ArtFont(){
    super("字体设置");//设置默认字体
    boldStyle=0;
    italicStyle=0;
    underlineStyle=0;
    fontSizeStyle=10;
    fontNameStyle="宋体";
    font=new Font(fontNameStyle,boldStyle+italicStyle,fontSizeStyle);
    fontPanel=new JPanel();
    //String Art="com.sun.java.swing.plaf.business.BusinessLookAndFeel";
        // String lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
         //UIManager.setLookAndFeel(lookAndFeel);
        /* UIManager.LookAndFeelInfo[] infos=UIManager.getInstalledLookAndFeels();
    for (UIManager.LookAndFeelInfo info:infos )
        makeButton(info.getName(),info.getClassName());
         add(fontPanel);*/
    fontPanel.setLayout(new FlowLayout());
    GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
    fontNames=ge.getAvailableFontFamilyNames();//获得系统中所有字体的名字
    fontType=new JComboBox(fontNames);
    fontType.setEditable(false);
    fontType.setMaximumRowCount(10);
    fontType.addItemListener(new ItemListener(){
       public void itemStateChanged(ItemEvent e){
        int stateChange=e.getStateChange();//实现监听字体名字改变的事件
       }
    });
    //设置字体大小
    fontSizes=new String[63];
    for(int i=0;i<63;i++){
       fontSizes[i]=Integer.toString((i+10));
    }
    fontSize=new JComboBox(fontSizes);
    fontSize.setEditable(false);
    fontSize.setMaximumRowCount(10);
    fontSize.addItemListener(new ItemListener(){
       public void itemStateChanged(ItemEvent e){
        int stateChange=e.getStateChange();//实现监听字体大小改变的方法
       }
    });
    //设置粗体选择按钮
    boldBx=new JCheckBox("粗体");
    boldBx.addItemListener(new ItemListener(){
       public void itemStateChanged(ItemEvent e){
        int stateChange=e.getStateChange(); //实现监听选择粗体状态改变的方法
       }
    });
    //设置斜体选择按钮
    italicBx=new JCheckBox("斜体");
    italicBx.addItemListener(new ItemListener(){
       public void itemStateChanged(ItemEvent e){
        int stateChange=e.getStateChange(); //实现监听选择斜体状态改变的方法
       }});
    //设置颜色选择
    colorBtn=new JButton("颜色");
    colorBtn.addActionListener(this);
    //public void actionPerformed(ActionEvent e){
    /*if(e.getSource()==colorBtn)
    {//改变颜色
       colorStyle=JColorChooser.showDialog(this,"选择字体颜色",colorStyle);
       colorBtn.setForeground(colorStyle);
       txtArea.setForeground(colorStyle);
    }
    }
    });*/
    //设置字体面板
    fontPanel.add(fontType);
    fontPanel.add(fontSize);
    fontPanel.add(boldBx);
    fontPanel.add(italicBx);
    fontPanel.add(colorBtn);
    //设置输入提示标签
    label=new JLabel("输入");
    //设置文本输入框
    inputText=new JTextField(30);
    inputText.addActionListener(this);
    //设置文本显示区
    txtArea=new JTextArea(10,80);//20行80列
    txtArea.setFont(font);
    //设置文本面板
    showPanel=new JPanel();
    showPanel.add(label);
    showPanel.add(inputText);
    showPanel.setLayout(new FlowLayout());
    showPanel.add(new JScrollPane(txtArea));
    //设置容器
    Container container=getContentPane();
    container.setLayout(new BorderLayout());
    container.add(fontPanel,BorderLayout.NORTH);
    container.add(showPanel,BorderLayout.CENTER);
    setSize(500,300);
    setVisible(true);
    }
    public void actionPerformed(ActionEvent e){
    if(e.getSource()==colorBtn){//改变颜色
    colorStyle=JColorChooser.showDialog(this,"选择字体颜色",colorStyle);
    colorBtn.setForeground(colorStyle);
    txtArea.setForeground(colorStyle);
    }
    else if(e.getSource()==inputText){//将输入文字在文字显示区表示
    txtArea.setText(inputText.getText());
    }
    }
    void makeButton(String name,final String artName)
    {
       JButton button=new JButton(name);
       fontPanel.add(button);
       button.addActionListener(new ActionListener()
       {
        public void actionPerformed(ActionEvent event)
        {
         String art="com.sun.java.swing.plaf.motif.MotifLookAndFeel";
         try
         {
          UIManager.setLookAndFeel(artName);
          SwingUtilities.updateComponentTreeUI(ArtFont.this);
         }
         catch(Exception e)
         {
          e.printStackTrace();
         }
        }
       });
    //private JPanel fontPanel;
    };
    public static void main(String[] args){
    EventQueue.invokeLater(new Runnable()
    {
       public void run()
       {
            JFrame.setDefaultLookAndFeelDecorated(true);
               /*JDialog.setDefaultLookAndFeelDecorated(true);*/
        ArtFont artFont=new ArtFont();
            artFont.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
    });
    }
    }