2.编写一个FontFamily类,该类对象获取当前机器可用的全部字体名称。
编写一个对话框FontDialog,该对话框是模式对话框,采用BorderLayout布局,包含一个JComboBox放在北面显示全部字体的名称,包含一个JLabel放在中间,显示字体的效果,包含两个按钮放在南面,点击YES,在对话框所依赖的窗口中设置字体的效果,点击Cancle取消。
编写一个窗口FrameHaveDialog,该窗口有一个按钮和一个文本区,当单击该按钮时,弹出对话框FontDialog,然后根据用户在对话框下拉列表中选择的为显示文本区中的文本。
最后编写一个程序执行入口进行测试。
import java.awt.GraphicsEnvironment;
public class FontFamily {
String[] fontName;
GraphicsEnvironment ge;
public FontFamily() {
super();
// TODO Auto-generated constructor stub
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
fontName = ge.getAvailableFontFamilyNames();
}
public String[] getFontName() {
return fontName;
}
}
import java.awt.BorderLayout;
import java.awt.Font;
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.JLabel;
import javax.swing.JPanel;
public class FontDialog extends JFrame implements ItemListener, ActionListener{
JComboBox jcom;
String[] fontName;
String name;
FontFamily fam;
JLabel label;
JButton yes,central;
JPanel panel;
public FontDialog() {
super();
// TODO Auto-generated constructor stub
fam = new FontFamily();
jcom = new JComboBox();
fontName = fam.getFontName();
for (int i = 0; i < fontName.length; i++) {
jcom.addItem(fontName[i]);
}
label = new JLabel();
Font f = new Font(null);
label.setFont(f);
label.setText("hello word");
yes = new JButton("yes");
central = new JButton("central");
panel = new JPanel();
panel.add(yes);
panel.add(central);
add(jcom, BorderLayout.NORTH);
add(label, BorderLayout.CENTER);
add(panel, BorderLayout.SOUTH);
jcom.addItemListener(this);
yes.addActionListener(this);
central.addActionListener(this);
setBounds(100, 100, 400, 300);
setVisible(true);
validate();
}
public String getName() {
return name;
}
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jcom)
{
name=(String) jcom.getSelectedItem();
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==yes)
{
Font f=new Font(name,Font.BOLD,32);
label.setFont(f);
label.setText("hello word");
}
if(e.getSource()==central)
{
Font f=new Font(null);
label.setFont(f);
label.setText("hello word");
}
}
}
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class FrameHaveDialog extends JFrame implements ActionListener{
JButton bt01;
JTextArea area;
FontDialog dia;
public FrameHaveDialog() throws HeadlessException {
super();
// TODO Auto-generated constructor stub
dia=new FontDialog();
bt01=new JButton("显示字体");
area=new JTextArea();
bt01.addActionListener(this);
add(bt01,BorderLayout.NORTH);
add(area,BorderLayout.CENTER);
setBounds(500,100, 400, 300);
setVisible(true);
validate();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==bt01)
{
String name=dia.getName();
Font font=new Font(name,Font.BOLD,32);
area.setFont(font);
}
}
}
public class TextMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// FontDialog f=new FontDialog();
FrameHaveDialog ha=new FrameHaveDialog();
}
}