主题:字体选择器(原创)
/*
* JFontChooser.java
*
* Created on 2007年9月3日, 下午4:56
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package common;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
* 字体选择器构造方法(可以传Font与Color的初始值,也可以是空构造函数,结果返回根据getFont与getColor得到相应的设置
* @author tewang
*/
public class JFontChooser extends JPanel implements ActionListener {
//六个控件
private Font font;
private Color color;
private JTextField jtFont = new JTextField(10);//字体
private JTextField jtFontStyle = new JTextField(10); //样式
private JTextField jtFontSize = new JTextField(10);//大小
private JList jlFontStyle;
private JList jlFontSize;
private JList jlFont;
private JDialog jd;
private JLabel showJLabel = new JLabel();//显示当前字体
private JLabel instructionJLabel = new JLabel();//对当前字体进行说明
private GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
private String[] jfontStr = ge.getAvailableFontFamilyNames();
private String[] fontStyle = {"常规","斜体","粗体","粗斜体"};
private String[] fontSize = {"8","9","10","11","12","14","16","18","20","22","24","26","28",
"36","48","72","初号","小初","一号","小一","二号","小二","三号","小三","四号","小四","五号","小五",
"六号","小六","七号","八号"};
private int[] sizeValue={8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72,42,36,26,24,22,18,16,15,
14,12,11,9,7,6,5,4};
private JButton ok = new JButton("确定");
private JButton cancel = new JButton("取消");
//删除线与下划线
private JCheckBox jcDelLine = new JCheckBox("删除线(K)");
private JCheckBox jcDownLine = new JCheckBox("下划线(U)");
//颜色
private Object[] o = {"黑色","蓝色","青色","深灰色","灰色","绿色","浅灰色","洋红色","桔黄色","粉红色","红色","白色","黄色"};
private Color[] colorValue = {Color.BLACK,Color.BLUE,Color.CYAN,Color.DARK_GRAY,Color.GRAY,Color.GREEN,Color.LIGHT_GRAY,Color.MAGENTA,Color.ORANGE,Color.PINK,Color.RED,Color.WHITE,Color.YELLOW};
private JComboBox jcb = new JComboBox(o);
/** Creates a new instance of JFontChooser */
public JFontChooser() {
initJFont();
initJFontStyle();
initJFontSize();
initOther();
initComponentUpdate();
initJDialog();
}
public JFontChooser(Font f,Color c){ //根据传进来的字体与颜色进行设置
initJFont();
initJFontStyle();
initJFontSize();
initOther();
this.font = f;
this.color = c;
setFontAndColor(f,c);
initComponentUpdate();
initJDialog();
}
private void initOther(){
//默认颜色与字体
updateFont();//设置一下默认字体与颜色
//效果与示例
JLabel jl4 = new JLabel();
jl4.setBorder(BorderFactory.createTitledBorder("效果"));
jl4.setBounds(10,160,150,130);
this.add(jl4);
JLabel jl5 = new JLabel();
jl5.setBorder(BorderFactory.createTitledBorder("示例"));
jl5.setBounds(170,160,220,130);
this.add(jl5);
//ok与取消按钮
ok.setBounds(400,30,80,25);
ok.addActionListener(this);
cancel.addActionListener(this);
cancel.setBounds(400,60,80,25);
this.add(ok);
this.add(cancel);
//示例
showJLabel.setText((String)jlFont.getSelectedValue());
showJLabel.setHorizontalAlignment(JLabel.CENTER);
showJLabel.setBounds(180,150,200,150);
this.add(showJLabel);
//效果里的 删除线与下划线
jcDelLine.setBounds(15,180,100,25);
jcDownLine.setBounds(15,200,100,25);
this.add(jcDelLine);
this.add(jcDownLine);
JLabel jl6 = new JLabel("颜色(C):");
jl6.setBounds(15,230,100,25);
//颜色
jcb.addActionListener(this);
jcb.setBounds(17,255,130,22);
jcDelLine.addActionListener(this);
jcDownLine.addActionListener(this);
this.add(jl6);
this.add(jcb);
//下面的状态说明
instructionJLabel.setText("该字体用于显示,打印时将使用最接近的匹配字体");
instructionJLabel.setBounds(17,290,400,25);
this.add(instructionJLabel);
}
private void initComponentUpdate(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
e.printStackTrace();
}
}
* JFontChooser.java
*
* Created on 2007年9月3日, 下午4:56
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package common;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
* 字体选择器构造方法(可以传Font与Color的初始值,也可以是空构造函数,结果返回根据getFont与getColor得到相应的设置
* @author tewang
*/
public class JFontChooser extends JPanel implements ActionListener {
//六个控件
private Font font;
private Color color;
private JTextField jtFont = new JTextField(10);//字体
private JTextField jtFontStyle = new JTextField(10); //样式
private JTextField jtFontSize = new JTextField(10);//大小
private JList jlFontStyle;
private JList jlFontSize;
private JList jlFont;
private JDialog jd;
private JLabel showJLabel = new JLabel();//显示当前字体
private JLabel instructionJLabel = new JLabel();//对当前字体进行说明
private GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
private String[] jfontStr = ge.getAvailableFontFamilyNames();
private String[] fontStyle = {"常规","斜体","粗体","粗斜体"};
private String[] fontSize = {"8","9","10","11","12","14","16","18","20","22","24","26","28",
"36","48","72","初号","小初","一号","小一","二号","小二","三号","小三","四号","小四","五号","小五",
"六号","小六","七号","八号"};
private int[] sizeValue={8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72,42,36,26,24,22,18,16,15,
14,12,11,9,7,6,5,4};
private JButton ok = new JButton("确定");
private JButton cancel = new JButton("取消");
//删除线与下划线
private JCheckBox jcDelLine = new JCheckBox("删除线(K)");
private JCheckBox jcDownLine = new JCheckBox("下划线(U)");
//颜色
private Object[] o = {"黑色","蓝色","青色","深灰色","灰色","绿色","浅灰色","洋红色","桔黄色","粉红色","红色","白色","黄色"};
private Color[] colorValue = {Color.BLACK,Color.BLUE,Color.CYAN,Color.DARK_GRAY,Color.GRAY,Color.GREEN,Color.LIGHT_GRAY,Color.MAGENTA,Color.ORANGE,Color.PINK,Color.RED,Color.WHITE,Color.YELLOW};
private JComboBox jcb = new JComboBox(o);
/** Creates a new instance of JFontChooser */
public JFontChooser() {
initJFont();
initJFontStyle();
initJFontSize();
initOther();
initComponentUpdate();
initJDialog();
}
public JFontChooser(Font f,Color c){ //根据传进来的字体与颜色进行设置
initJFont();
initJFontStyle();
initJFontSize();
initOther();
this.font = f;
this.color = c;
setFontAndColor(f,c);
initComponentUpdate();
initJDialog();
}
private void initOther(){
//默认颜色与字体
updateFont();//设置一下默认字体与颜色
//效果与示例
JLabel jl4 = new JLabel();
jl4.setBorder(BorderFactory.createTitledBorder("效果"));
jl4.setBounds(10,160,150,130);
this.add(jl4);
JLabel jl5 = new JLabel();
jl5.setBorder(BorderFactory.createTitledBorder("示例"));
jl5.setBounds(170,160,220,130);
this.add(jl5);
//ok与取消按钮
ok.setBounds(400,30,80,25);
ok.addActionListener(this);
cancel.addActionListener(this);
cancel.setBounds(400,60,80,25);
this.add(ok);
this.add(cancel);
//示例
showJLabel.setText((String)jlFont.getSelectedValue());
showJLabel.setHorizontalAlignment(JLabel.CENTER);
showJLabel.setBounds(180,150,200,150);
this.add(showJLabel);
//效果里的 删除线与下划线
jcDelLine.setBounds(15,180,100,25);
jcDownLine.setBounds(15,200,100,25);
this.add(jcDelLine);
this.add(jcDownLine);
JLabel jl6 = new JLabel("颜色(C):");
jl6.setBounds(15,230,100,25);
//颜色
jcb.addActionListener(this);
jcb.setBounds(17,255,130,22);
jcDelLine.addActionListener(this);
jcDownLine.addActionListener(this);
this.add(jl6);
this.add(jcb);
//下面的状态说明
instructionJLabel.setText("该字体用于显示,打印时将使用最接近的匹配字体");
instructionJLabel.setBounds(17,290,400,25);
this.add(instructionJLabel);
}
private void initComponentUpdate(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
e.printStackTrace();
}
}