字体对话框

FontDialogMainClass.java

public class FontDialogMainClass {

    public static void main(String args[]) {

       FrameHaveDialog win=new FrameHaveDialog();

    }

}

 FontFamilyNames.java

import java.awt.GraphicsEnvironment;

public class FontFamilyNames {

    String allFontNames[];

    public String [] getFontName() {

      GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();

      allFontNames=ge.getAvailableFontFamilyNames();

      return allFontNames;

    }

}

FontDialog.java 

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

public class FontDialog extends JDialog implements ItemListener,ActionListener {

   FontFamilyNames fontFamilyNames; 

   int fontSize=38;

   String fontName;

   JComboBox fontNameList,fontSizeList;

   JLabel label;

   Font font;

   JButton yes,cancel;

   static int YES=1,NO=0;

   int state=-1;

   FontDialog(JFrame f) { 

      super(f);

      setTitle("字体对话框");

      font=new Font("宋体",Font.PLAIN,12);

      fontFamilyNames=new FontFamilyNames();

      setModal(true); //当前对话框调用setModal(boolean b)设置为有模式

      yes=new JButton("Yes"); 

      cancel=new JButton("cancel");  

      yes.addActionListener(this);

      cancel.addActionListener(this);

      label=new JLabel("hello,奥运",JLabel.CENTER);

      fontNameList=new JComboBox();

      fontSizeList=new JComboBox();

      String name[]=fontFamilyNames.getFontName();

      fontNameList.addItem("字体");

      for(int k=0;k<name.length;k++)

         fontNameList.addItem(name[k]);

      fontSizeList.addItem("大小");

      for(int k=8;k<72;k=k+2)

         fontSizeList.addItem(new Integer(k));

      fontNameList.addItemListener(this);

      fontSizeList.addItemListener(this);

      JPanel pNorth=new JPanel();

      pNorth.add(fontNameList);

      pNorth.add(fontSizeList);

      add(pNorth,BorderLayout.NORTH);

      add(label,BorderLayout.CENTER);

      JPanel pSouth=new JPanel();

      pSouth.add(yes); 

      pSouth.add(cancel);

      add(pSouth,BorderLayout.SOUTH);

      setBounds(100,100,280,170);

      setDefaultCloseOperation(DISPOSE_ON_CLOSE);

      validate();

   }

   public void itemStateChanged(ItemEvent e) {

      if(e.getSource()==fontNameList) {

         fontName=(String)fontNameList.getSelectedItem();

         font=new Font(fontName,Font.PLAIN,fontSize);

      }

      else if(e.getSource()==fontSizeList) { 

         Integer m=(Integer)fontSizeList.getSelectedItem();

         fontSize=m.intValue();

         font=new Font(fontName,Font.PLAIN,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.java 

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

public class FrameHaveDialog extends JFrame implements ActionListener {

   JTextArea text;

   JButton buttonFont;

   FrameHaveDialog() { 

      buttonFont=new JButton("设置字体"); 

      text=new JTextArea("面向对象程序设计(JAVA)");

      buttonFont.addActionListener(this);

      add(buttonFont,BorderLayout.NORTH);

      add(text);

      setBounds(60,60,300,300);

      setVisible(true);

      validate();

      setDefaultCloseOperation(DISPOSE_ON_CLOSE);

   }

   public void actionPerformed(ActionEvent e) {

      if(e.getSource()==buttonFont) {

         FontDialog dialog=new FontDialog(this);      

          dialog.setVisible(true);

          if(dialog.getState()==FontDialog.YES) {

             text.setFont(dialog.getFont());

             text.repaint();

          }

          if(dialog.getState()==FontDialog.NO) {

             text.repaint();

          }        

      }

   }

}

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值