import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;public class FontChooser extends JFrame{
private Container container;
private JPanel selectPanel,southPanel;
private JPanel fontPanel,stylePanel,sizePanel;
private JPanel showPanel,buttonPanel;
private JTextArea showArea;
private static JTextField fontShowField,sizeShowField,styleShowField;
private static JButton okButton,cancelButton;
private JLabel fontLabel,styleLabel,sizeLabel;
private JList fontList,styleList,sizeList;
private GraphicsEnvironment environment;//获取系统字体
private String[] font,size,style = {"常规","斜体","粗体","粗斜体"};
private static Font fontBack;

public FontChooser(){
super("字体选择");
container = getContentPane();//获取内容面板
selectPanel = new JPanel();
selectPanel.setLayout(new GridLayout(1,3,5,3));
//构造选择字体的JList
fontLabel = new JLabel("字体",SwingConstants.LEFT);
fontPanel = new JPanel();
fontPanel.setLayout(new BorderLayout());
fontShowField = new JTextField(14);
fontShowField.setEditable(false);
environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
font = environment.getAvailableFontFamilyNames();

fontList = new JList(font);
fontList.addListSelectionListener(new fontListListener());
try{
fontList.setSelectedIndex(0);
}
catch(Exception e){
}
fontPanel.add(fontLabel,BorderLayout.NORTH);
fontPanel.add(fontShowField);
fontPanel.add(new JScrollPane(fontList),BorderLayout.SOUTH);

//构造字形JList
styleLabel = new JLabel("字形",SwingConstants.LEFT);
stylePanel = new JPanel();
stylePanel.setLayout(new BorderLayout());
styleShowField = new JTextField(5);
styleShowField.setEditable(false);
styleList = new JList(style);
styleList.addListSelectionListener(new styleListListener());
try{
styleList.setSelectedIndex(0);
}
catch(Exception e){
}
stylePanel.add(styleLabel,BorderLayout.NORTH);
stylePanel.add(styleShowField);
stylePanel.add(new JScrollPane(styleList),BorderLayout.SOUTH);

//构造大小JList
sizeLabel = new JLabel("大小",SwingConstants.LEFT);
sizePanel = new JPanel();
sizePanel.setLayout(new BorderLayout());
sizeShowField = new JTextField(3);
sizeShowField.setEditable(false);
size = new String[51];
for(int i=0;i<=50;i++)
size[i] = String.valueOf(i+15);
sizeList = new JList(size);
sizeList.addListSelectionListener(new sizeListListener());
try{
sizeList.setSelectedIndex(0);
}
catch(Exception e){
}
sizePanel.add(sizeLabel,BorderLayout.NORTH);
sizePanel.add(sizeShowField);
sizePanel.add(new JScrollPane(sizeList),BorderLayout.SOUTH);

selectPanel.add(fontPanel);
selectPanel.add(stylePanel);
selectPanel.add(sizePanel);

//south面板
southPanel = new JPanel();
southPanel.setLayout(new GridLayout(2,1));
showPanel = new JPanel();
showPanel.setLayout(new FlowLayout());
showArea = new JTextArea("微软中文字体 AaBbCc",1,5);
showArea.setMargin(new Insets(0,170,0,0)); //使字体大概居中
showArea.setEditable(false);
showPanel.add(new JScrollPane(showArea));
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER,35,2));
okButton = new JButton("确定");
cancelButton = new JButton("取消");
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);

southPanel.add(showArea);
southPanel.add(buttonPanel);

container.add(selectPanel);
container.add(southPanel,BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static Font showSelectDialog(){
FontChooser fontChooser = new FontChooser();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = fontChooser.getSize();
fontChooser.setLocation((screenSize.width - frameSize.width)/2,(screenSize.height - frameSize.height)/2);
fontChooser.setResizable(false);
cancelButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
System.exit(0);
}
}
);
okButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
fontBack = getMyFont();
}
}
);
fontChooser.setVisible(true);
fontChooser.pack();
if(fontBack!=null)
return fontBack;
else 
return new Font("monospaced",Font.PLAIN,12);
}

private static Font getMyFont(){
return new Font(fontShowField.getText(),getStyle(styleShowField.getText()),Integer.parseInt(sizeShowField.getText()));
}

private static int getStyle(String str){
if(str.equals("常规")){
return Font.PLAIN;
}
else if(str.equals("斜体")){
return Font.ITALIC;
}
else if(str.equals("粗体")){
return Font.BOLD;
}
else if(str.equals("粗斜体")){
return Font.BOLD+Font.ITALIC;
}
else
return Font.PLAIN;
}

public static void main(String args[]){
showSelectDialog();
}

private class fontListListener implements ListSelectionListener{
public void valueChanged(ListSelectionEvent event){
fontShowField.setText(font[fontList.getSelectedIndex()]);
showArea.setFont(getMyFont());
}
} private class styleListListener implements ListSelectionListener{
public void valueChanged(ListSelectionEvent event){
styleShowField.setText(style[fontList.getSelectedIndex()]);
showArea.setFont(getMyFont());
}
}

private class sizeListListener implements ListSelectionListener{
public void valueChanged(ListSelectionEvent event){
sizeShowField.setText(size[fontList.getSelectedIndex()]);
showArea.setFont(getMyFont());
}
}
}