Java--第13周实验--任务2--编写一个FontFamily类,该类对象获取当前机器可用的全部字体名称,编写一个对话框FontDialog,该对话(二)

2014-11-24 10:33:37 · 作者: · 浏览: 1
class FontDialog extends JDialog implements ItemListener, ActionListener{
JComboBox list;
JLabel lable;
Mypanel panel;
public FontDialog(JFrame f, String s, boolean b) {
super(f, s, b);
panel = new Mypanel();
list = new JComboBox();
lable = new JLabel("字体的效果");
FontFamily fontFamily = new FontFamily();
String[] s1 = fontFamily.getfont();
for (int i = 0; i < s1.length; i++) {
list.addItem(s1[i]);
}

add(list,BorderLayout.NORTH);
add(lable,BorderLayout.CENTER);
add(panel,BorderLayout.SOUTH);
list.addItemListener(this);
panel.button1.addActionListener(this);
panel.button2.addActionListener(this);
setBounds(380, 380, 380, 380);
}


public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==list)
{
String name = (String)list.getSelectedItem();
//UIManager.put("Label.font", new Font(name,Font.BOLD,12));
//System.out.print(list.getItemAt(6));
lable.setFont(new Font(name,Font.PLAIN,list.getFont().getSize()));
}

}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==panel.button1)
{
//int i = list.getSelectedIndex();
//FrameHaveDialog frameHaveDialog = new FrameHaveDialog();
//int index = list.getSelectedIndex();
//lable.setFont(new Font((String) list.getItemAt(index),Font.PLAIN,list.getFont().getSize()));
//System.out.print(0);
setVisible(false);
//System.exit(0);
}
else if(e.getSource()==panel.button2)
{
//System.out.print(1);
setVisible(false);
//System.exit(0);
}

}



}



运行结果: