- //程序名:Ziti.java
- //功能:设置字体的字形、风格、大小、颜色。
- //程序员:李显福
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- import javax.swing.event.*;
- class Ziti
- {
- JFrame frame=new JFrame("字体演示");
- JPanel panel1=new JPanel();
- JPanel panel2=new JPanel();
- JPanel panel3=new JPanel();
-
- JLabel label1=new JLabel("字体");
- JLabel label2=new JLabel("字号");
- JLabel label3=new JLabel("Color");
- JLabel label4=new JLabel("我们热爱生活!");
-
- String ziti[]={"华文新魏","宋体","楷体","黑体","仿宋","新宋体","微软雅黑","华文琥珀","华文彩云","华文行楷","幼圆"};
- String zihao[]={"2","4","6","8","10","12","14","16","18","20","22","24","26","28","30","32","34","36"};
- String color[]={"black","cyan","gray","green","red","white","yellow","pink","orange","magenta","lightGray","darkGray"};
- Color c[]={Color.black,Color.cyan,Color.gray,Color.green,Color.red,Color.white,Color.yellow,Color.pink,Color.orange,Color.magenta,Color.lightGray,Color.darkGray};
-
- JComboBox combobox1=new JComboBox(ziti);
- JComboBox combobox2=new JComboBox(zihao);
- JComboBox combobox3=new JComboBox(color);
-
- JRadioButton radiobutton1=new JRadioButton("常规",true);
- JRadioButton radiobutton2=new JRadioButton("粗体");
- JRadioButton radiobutton3=new JRadioButton("斜体");
- JRadioButton radiobutton4=new JRadioButton("粗斜体");
- ButtonGroup buttongroup=new ButtonGroup();
-
- Myziti zitilistener=new Myziti();//创建事件监听器
- Ziti()
- {
- frame.setLayout(new GridLayout(3,1));
-
- frame.add(panel1);
- frame.add(panel2);
- frame.add(panel3);
-
- panel1.setBorder(BorderFactory.createTitledBorder("字形:"));
- panel1.add(label1);
- panel1.add(combobox1);
- panel1.add(label2);
- panel1.add(combobox2);
- panel1.add(label3);
- panel1.add(combobox3);
-
- panel2.setBorder(BorderFactory.createTitledBorder("预览效果:"));
- panel2.add(label4);
-
- panel3.setBorder(BorderFactory.createTitledBorder("字体风格:"));
- panel3.add(radiobutton1);
- panel3.add(radiobutton2);
- panel3.add(radiobutton3);
- panel3.add(radiobutton4);
- buttongroup.add(radiobutton1);
- buttongroup.add(radiobutton2);
- buttongroup.add(radiobutton3);
- buttongroup.add(radiobutton4);
- //---------------------------------------------------------------------------------------
- //注册事件监听器
- combobox1.addItemListener(zitilistener);
- combobox2.addItemListener(zitilistener);
- combobox3.addItemListener(zitilistener);
- radiobutton1.addItemListener(zitilistener);
- radiobutton2.addItemListener(zitilistener);
- radiobutton3.addItemListener(zitilistener);
- radiobutton4.addItemListener(zitilistener);
- //---------------------------------------------------------------------------------------
- frame.setSize(400,280);
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLocationRelativeTo(null);
- frame.setResizable(false);
- }
-
- class Myziti implements ItemListener
- {
- public void itemStateChanged(ItemEvent e)
- {
-
- if(e.getSource()==(JComboBox)combobox1)
- {
- Font font=new Font(ziti[combobox1.getSelectedIndex()],Font.BOLD,20);
- label4.setFont(font);
- }
- if(e.getSource()==(JComboBox)combobox2)
- {
- Font font=new Font(ziti[combobox1.getSelectedIndex()],Font.BOLD,Integer.parseInt(zihao[combobox2.getSelectedIndex()]));
- label4.setFont(font);
- }
- if(e.getSource()==(JComboBox)combobox3)
- {
-
- label4.setForeground(c[combobox3.getSelectedIndex()]);
- }
- if(e.getSource()==(JRadioButton)radiobutton1)
- {
- Font font=new Font(ziti[combobox1.getSelectedIndex()],Font.PLAIN,Integer.parseInt(zihao[combobox2.getSelectedIndex()]));
- label4.setFont(font);
- }
- if(e.getSource()==(JRadioButton)radiobutton2)
- {
- Font font=new Font(ziti[combobox1.getSelectedIndex()],Font.BOLD,Integer.parseInt(zihao[combobox2.getSelectedIndex()]));
- label4.setFont(font);
- }
- if(e.getSource()==(JRadioButton)radiobutton3)
- {
- Font font=new Font(ziti[combobox1.getSelectedIndex()],Font.ITALIC,Integer.parseInt(zihao[combobox2.getSelectedIndex()]));
- label4.setFont(font);
- }
- if(e.getSource()==(JRadioButton)radiobutton4)
- {
- Font font=new Font(ziti[combobox1.getSelectedIndex()],Font.BOLD+Font.ITALIC,Integer.parseInt(zihao[combobox2.getSelectedIndex()]));
- label4.setFont(font);
- }
- }
- }
- }
- class Zitishezhi
- {
- public static void main(String args[])
- {
- Ziti z=new Ziti();
- }
- }
复制代码
|
|