import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Test extends JFrame implements ListSelectionListener{
JList jlName,jlSize;
JLabel lbl;
String [] strSize={"9","10","12","14","16","18","20","22","24","26","28","30","32"};
public Test(){
jlName=new JList();
setTitle("设置字体示例");
lbl=new JLabel("字体示例Simple Text",JLabel.CENTER);
GraphicsEnvironment env=GraphicsEnvironment.getLocalGraphicsEnvironment();
String [] strName=env.getAvailableFontFamilyNames();
jlName.setListData(strName);
jlSize=new JList(strSize);
JScrollPane js=new JScrollPane();
js.getViewport().add(jlName);
Container con=getContentPane();
con.setLayout(new BorderLayout());
con.add(lbl,BorderLayout.NORTH);
JPanel p=new JPanel();
con.add(p);
p.setLayout(new GridLayout(1,2));
p.add(js);
p.add(jlSize);
jlName.addListSelectionListener(this);
jlSize.addListSelectionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,400);
setVisible(true);
}
public void valueChanged(ListSelectionEvent e){
if(e.getSource()==jlName){
String name=(String)jlName.getSelectedValue();
Font f=lbl.getFont();
Font newFont=new Font(name,f.getStyle(),f.getSize());
lbl.setFont(newFont);
}
else if(e.getSource()==jlSize){
int size=Integer.parseInt((String)jlSize.getSelectedValue());
Font f=lbl.getFont();
Font newFont=new Font(f.getName(),f.getStyle(),size);
lbl.setFont(newFont);
}
}
public static void main(String args[]){
new Test();
}
}