java上机 第十三周 任务二 设置字体
/* * 程序头部注释开始 * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称:设置字体 * 作 者:薛广晨 * 完成日期:2012 年 11 月 21日 * 版 本号:x1.0 * 对任务及求解方法的描述部分 * 输入描述: * 问题描述: 编写一个FontFamily类,该类对象获取当前机器可用的全部字体名称。* 编写一个对话框FontDialog,该对话框是模式对话框,采用BorderLayout布局,* 包含一个JComboBox放在北面显示全部字体的名称,包含一个JLabel放在中间,显示字体的效果,* 包含两个按钮放在南面,点击YES,在对话框所依赖的窗口中设置字体的效果,点击Cancle取消。* 编写一个窗口FrameHaveDialog,该窗口有一个按钮和一个文本区,当单击该按钮时,* 弹出对话框FontDialog,然后根据用户在对话框下拉列表中选择的为显示文本区中的文本* 程序输出: * 程序头部的注释结束 */ //FontFamily类package xue;import java.awt.GraphicsEnvironment;public class FontFamily {String fontName[];public String [] getFontName(){ GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); fontName = ge.getAvailableFontFamilyNames(); return fontName;}}//FontDialog类package xue;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class FontDialog extends Dialog implements ItemListener,ActionListener{FontFamily fontFamily; int fontSize=38; String fontName; Choice fontNameList; JLabel label; Font font; Button yes,cancel; static int YES=1, NO=0; int state=-1; FontDialog(Frame f) { super(f); setLayout(new BorderLayout()); fontFamily = new FontFamily(); fontNameList = new Choice(); fontNameList.addItemListener(this); setModal(true); yes = new Button("YES"); cancel = new Button("Cancel"); yes.addActionListener(this); cancel.addActionListener(this); label = new JLabel("hello,奥运", JLabel.CENTER); String[] names = fontFamily.getFontName(); for(int i = 0; i < names.length; i++) { fontNameList.add(names[i]); } add(fontNameList, BorderLayout.NORTH); add(label, BorderLayout.CENTER); Panel pSouth = new Panel(); pSouth.add(yes); pSouth.add(cancel); add(pSouth, BorderLayout.SOUTH); setBounds(100,100,280,170); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { state=NO; setVisible(false); }}); validate(); }public void itemStateChanged(ItemEvent e) {fontName = (String)fontNameList.getSelectedItem();font = new Font(fontName, Font.BOLD, fontSize);label.setFont(font);label.repaint();validate();}public void actionPerformed(ActionEvent e) {if(e.getSource()==yes) { state=YES; setVisible(false); } else if(e.getSource()==cancel) { state=NO; setVisible(false); }}public int getState() { return state; } public Font getFont() { return font; }}//FrameHaveDialog类package xue;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class FrameHaveDialog extends Frame implements ActionListener{JTextArea text;Button buttonFont;FrameHaveDialog() { buttonFont = new Button("设置字体"); text = new JTextArea("Java 2实用教程(第三版)"); buttonFont.addActionListener(this); add(buttonFont, BorderLayout.NORTH); add(text); setBounds(60,60,300,300); setVisible(true); validate(); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });} public void actionPerformed(ActionEvent e) { if(e.getSource() == buttonFont) { FontDialog dialog = new FontDialog(this); dialog.setVisible(true); dialog.setTitle("字体对话框"); if(dialog.getState() == FontDialog.YES) { text.repaint(); text.setFont(dialog.getFont()); } if(dialog.getState() == FontDialog.NO) { text.repaint(); } } }}//测试类FrameHaveDialogTestpackage xue;public class FrameHaveDialogTest {/** * @param args */public static void main(String[] args) { new FrameHaveDialog();}}
运行结果:
