package fontsytle;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class fontstyle extends JFrame {
JButton jb;
JComboBox fontName;
JTextArea jta;
FontDialog fdg=new FontDialog();
String[] fontArr;
String strArr="设置字体练习";	
public static void main(String[] args) {
	// TODO Auto-generated method stub
	new fontstyle();
}
public fontstyle() {
	fdg.fontstyle=this;
	jta=new JTextArea(6,12);
	this.setLayout(new BorderLayout());
	jb=new JButton("设置字体");
	jb.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals("设置字体")) {
				fdg.setVisible(true);
			}
		}
	});
	jta.setText(strArr);
	this.add(jb,BorderLayout.NORTH);
	this.add(new JScrollPane(jta));
	this.setTitle("字体设置");
	this.setSize(600,500);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.setResizable(true);
	this.setLocation(300,300);
	this.setVisible(true);
}
void setArea(Font font) {
	jta.setFont(font);
}

}
package fontsytle;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class FontDialog extends JFrame implements ItemListener {
JComboBox fontName;
JTextArea fontExample;
Font font1;
JButton Done,Cancel;
final String[] fontArr;
final String strArr="字体样式展示";
fontstyle fontstyle;
public FontDialog() {
	fontExample=new JTextArea();
	fontExample.setText(strArr);
	JScrollPane jsp= new JScrollPane(fontExample);
	GraphicsEnvironment gev= GraphicsEnvironment.getLocalGraphicsEnvironment();
	fontArr=gev.getAvailableFontFamilyNames();
	fontName=new JComboBox(fontArr);
	fontName.addItemListener(this);
	fontName.setSelectedItem("宋体");
	FlowLayout layout=new FlowLayout(FlowLayout.CENTER,50,6);
	JPanel jp=new JPanel(layout);
	jp.add(fontName);
	Container c=this.getContentPane();
	Done= new JButton("确定");
	Cancel = new JButton("取消");
	Done.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			if(e.getActionCommand().equals("确定")) {
			fontstyle.setArea(font1);
			setVisible(false);
			}
		}
	});
	Cancel.addActionListener(new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getActionCommand().equals("取消")) {
				setVisible(false); 
			}				
		}	
	});
	JPanel jp1=new JPanel(layout);
	jp1.add(Done);
	jp1.add(Cancel);
	c.add(jp,BorderLayout.NORTH);
	c.add(jsp, BorderLayout.CENTER);
	c.add(jp1, BorderLayout.SOUTH);
	this.setBounds(610,265,410,310);
	this.setResizable(false);
	this.setLocation(400,400);
	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	this.setSize(300,300);
	this.setVisible(false);
}
public void itemStateChanged(ItemEvent e) {
	String s=(String)fontName.getSelectedItem();
	font1=new Font(s,Font.PLAIN,42);
	fontExample.setFont(font1);
}
}