Java实验--字体设计

来源:互联网 发布:残差网络 编辑:程序博客网 时间:2024/04/30 03:18

编写一个Java程序,实现字体设计窗体,请设计一个字体设计的窗体,使得用户可以根据选择,将字体按照的字体名称,字体大小,粗体和斜体风格进行设置。还可以设置位子文字的颜色,运行结果如下图所示:

 

源代码:

//FontSet.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FontSet extends JFrame{

JPanel headPanel,cenPanel,footPanel;
JComboBox fontstyle,fontsize;
JCheckBox boldface,italic;
JButton fontcolor=new JButton("颜色");
JLabel input=new JLabel("输入");
JTextField info=new JTextField(20);
JTextArea infoout=new JTextArea(6,30);

String fontsty="华文行楷";
int fontsi=20,bf=0,ita=0;
Font f;
Color color=Color.black;
FontSet(){
   String fonts[]={"华文行楷","宋体","楷体","华文彩云","黑体","TimesRoman","Courier"};
   fontstyle=new JComboBox(fonts);
   fontstyle.addItemListener(new fontstyleEvent());
   fontsize=new JComboBox();
   for(int i=10;i<=70;i+=2)
   fontsize.addItem(new Integer(i));
   fontsize.addItemListener(new fontsizeEvent());
   boldface=new JCheckBox("粗体");
   boldface.addItemListener(new boldfaceEvent());
   italic=new JCheckBox("斜体");
   italic.addItemListener(new italicEvent());
   fontcolor.setForeground(Color.RED);
   fontcolor.addActionListener(new fontcolorEvent());
   info.addActionListener(new infoEvent());
   infoout.setEditable(false);
   headPanel=new JPanel();
   headPanel.add(fontstyle);
   headPanel.add(fontsize);
   headPanel.add(boldface);
   headPanel.add(italic);
   headPanel.add(fontcolor);
   add(headPanel,BorderLayout.NORTH);
  
   cenPanel=new JPanel();
   cenPanel.add(input);
   cenPanel.add(info);
   add(cenPanel,BorderLayout.CENTER);
  
   footPanel=new JPanel();
   JScrollPane js=new JScrollPane(infoout);
   footPanel.add(js);
   add(footPanel,BorderLayout.SOUTH);
}

class fontstyleEvent implements ItemListener
{
   public void itemStateChanged(ItemEvent e){
    fontsty=fontstyle.getSelectedItem().toString();
    fontChange();
   }
}

class fontsizeEvent implements ItemListener
{
   public void itemStateChanged(ItemEvent e){
    fontsi=new Integer(fontsize.getSelectedItem().toString()).intValue();
    fontChange();
   }
}

class boldfaceEvent implements ItemListener
{
   public void itemStateChanged(ItemEvent e){
    if(e.getStateChange()==e.SELECTED)
     bf=1;
    else
     bf=0;
    fontChange();
   }
}

class italicEvent implements ItemListener
{
   public void itemStateChanged(ItemEvent e){
    if(e.getStateChange()==e.SELECTED)
     ita=1;
    else
     ita=0;
    fontChange();
   }
}

class fontcolorEvent implements ActionListener
{
   public void actionPerformed(ActionEvent e){
    color = JColorChooser.showDialog(FontSet.this,"字体颜色选择",color);
      if(color == null )
      color = Color.BLACK;
      fontChange();
   }
}

class infoEvent implements ActionListener
{
   public void actionPerformed(ActionEvent e){
    infoout.setText(info.getText());
    fontChange();
   }
}

void fontChange(){
   if(bf==1&&ita==1)
   f=new Font(fontsty,Font.BOLD+Font.ITALIC,fontsi);
   else if(bf==1)
    f=new Font(fontsty,Font.BOLD,fontsi);
   else if(ita==1)
    f=new Font(fontsty,Font.ITALIC,fontsi);
   else
    f=new Font(fontsty,Font.PLAIN,fontsi);
   infoout.setFont(f);
   infoout.setForeground(color);
}
public static void main(String[] args) {
   FontSet fs=new FontSet();
   fs.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
     System.exit(0);
    }
   });
   fs.setTitle("字体设置");
   fs.setSize(400, 220);
   fs.setLocation(300,200);
   fs.setResizable(false);
   fs.setVisible(true);

}

}

原创粉丝点击