[java]代码库
public class font implements ActionListener,ListSelectionListener{
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JList list_1;
private JList list_2;
private JList fontList;
JButton button;
JButton button_1;
JLabel label_4;
private JFrame frame;
String fontName="宋体";
int fontStyle=Font.PLAIN, fontSize=12;
Font font1;
public font(){
frame=new JFrame();
frame.getContentPane().setLayout(null);
frame.setTitle("字体");
frame.setVisible(true);
frame.setBounds(100, 100, 425, 330);
final JLabel label = DefaultComponentFactory.getInstance().createLabel("字体(F):");
label.setBounds(10, 0, 62, 18);
frame.getContentPane().add(label);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 54, 129, 133);
frame.getContentPane().add(scrollPane);
fontList = new JList();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = ge.getAvailableFontFamilyNames();
fontList.setListData(fonts);
scrollPane.setViewportView(fontList);
fontList.addListSelectionListener(this);
final JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(145, 54, 99, 133);
frame.getContentPane().add(scrollPane_1);
list_1 = new JList();
scrollPane_1.setViewportView(list_1);
String []fontStyle1={"粗体","斜体","常规","粗斜体"};
list_1.setListData(fontStyle1);
list_1.addListSelectionListener(this);
final JScrollPane scrollPane_2 = new JScrollPane();
scrollPane_2.setBounds(250, 54, 66, 133);
frame.getContentPane().add(scrollPane_2);
list_2 = new JList();
scrollPane_2.setViewportView(list_2);
String []size={"12","14","16","18","20","22","24","26","28","36","48","72"};
list_2.setListData(size);
list_2.addListSelectionListener(this);
final JLabel label_1 = new JLabel();
label_1.setText("字形(Y):");
label_1.setBounds(146, 0, 66, 18);
frame.getContentPane().add(label_1);
final JLabel label_2 = new JLabel();
label_2.setText("大小(S):");
label_2.setBounds(250, 0, 66, 18);
frame.getContentPane().add(label_2);
button = new JButton();
button.setText("确定");
button.setBounds(345, 26, 62, 28);
frame.getContentPane().add(button);
button.addActionListener(this);
button_1 = new JButton();
button_1.setText("取消");
button_1.setBounds(345, 80, 62, 28);
frame.getContentPane().add(button_1);
button_1.addActionListener(this);
textField_1 = new JTextField();
textField_1.setBounds(10, 26, 129, 28);
frame.getContentPane().add(textField_1);
textField_2 = new JTextField();
textField_2.setBounds(145, 24, 99, 28);
frame.getContentPane().add(textField_2);
textField_3 = new JTextField();
textField_3.setBounds(250, 24, 66, 30);
frame.getContentPane().add(textField_3);
final JPanel panel = new JPanel();
panel.setBackground(Color.LIGHT_GRAY);
panel.setLayout(null);
panel.setBounds(49, 222, 267, 64);
frame.getContentPane().add(panel);
final JLabel label_3 = new JLabel();
label_3.setBounds(10, 198, 56, 20);
frame.getContentPane().add(label_3);
label_3.setFont(new Font("", Font.BOLD, 14));
label_3.setText("示例:");
label_4 = new JLabel();
label_4.setBounds(0, 0, 267, 64);
panel.add(label_4);
label_4.setText("AaBbCc");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object s = e.getSource();
if (s.equals(button)) {
textArea.setFont(font1);
frame.setVisible(false);
} else if (s.equals(button_1)) {
frame.setVisible(false);
}
}
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
Object s=e.getSource();
if(s.equals(fontList)){
fontName = (String) fontList.getSelectedValue();
font1 = new Font(fontName, fontStyle, fontSize);
label_4.setFont(font1);
textField_1.setText(fontName);
}else if(s.equals(list_1)){
int shape;
String style=list_1.getSelectedValue().toString();
if(style=="粗体"){
shape=Font.BOLD;
}else if(style=="斜体"){
shape=Font.ITALIC;
}else if(style=="常规"){
shape=Font.PLAIN;
}else{
shape=Font.BOLD+Font.PLAIN;
}
fontStyle=shape;
font1 = new Font(fontName, fontStyle, fontSize);
label_4.setFont(font1);
textField_2.setText(style);
}else if(s.equals(list_2)){
String size1=list_2.getSelectedValue().toString();
fontSize=Integer.parseInt(size1);
font1 = new Font(fontName, fontStyle, fontSize);
label_4.setFont(font1);
textField_3.setText(size1);
}
}
}