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();
}
}