| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2237 人关注过本帖
标题:java实验八 谁有实验八答案
只看楼主 加入收藏
小黑白
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-5-21
收藏
 问题点数:0 回复次数:0 
java实验八 谁有实验八答案
模板代码
FontFamilyNames.java
import java.awt.GraphicsEnvironment;
public class FontFamilyNames
{  String allFontName[];
   public String [] getFontName()
   {  GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
      allFontName=ge.getAvailableFontFamilyNames();
      return allFontName;
   }
}

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 fontSizeList,fontNameList;
    JLabel label;
    Font font;
    JButton yes,cancel;
    static int YES=1,NO=0;
    int state=-1;
    FontDialog(Frame f){
        super(f);
    setTitle("字体");
    font=new Font("宋体",Font.PLAIN,12);
        fontFamilyNames=new FontFamilyNames();
        //【代码1】//当前对话框调用setModal(boolean b)设置为有模式
        yes=new JButton("Yes");
        cancel=new JButton("cancel");  
        yes.addActionListener(this);
        cancel.addActionListener(this);
        label=new JLabel("hello,奥运",JLabel.CENTER);   
        fontSizeList=new JComboBox();
        fontNameList=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;  
             //【代码2】//对话框设置为不可见
         }
    else if(e.getSource()==cancel)
    {    state=NO;  
        //【代码3】//对话框设置为不可见
    }
    }
    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);//【代码4】//创建对话框
          dialog.setVisible(true);//【代码5】//对话框设置为可见
          //【代码6】 //对话框设置设置标题为“字体对话框”
          if(dialog.getState()==FontDialog.YES)
          { text.setFont(dialog.getFont());
            text.repaint();
          }
         if(dialog.getState()==FontDialog.NO)
          { text.repaint();
          }        
      }
    }
}

FontDialogMainClass.java
public class FontDialogMainClass
{   public static void main(String args[])
    {  FrameHaveDialog win=new FrameHaveDialog();
    }
}
搜索更多相关主题的帖子: public return import java 
2017-05-21 23:52
快速回复:java实验八 谁有实验八答案
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.077148 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved