请教高手,我模拟windows的记事本,做了一个界面,目前只做了个字体设置对话框,但怎样才能在NoteEdit中调用FontDialog中的Font字体设置呢?用以下方法不起作用:
tArea.setFont(fontSet.font);
请问该如何解决?
NoteEdit文件:
import java.awt.*;
import java.awt.event.*;
public class NoteEdit extends Frame implements ActionListener
{
/*定义主界面,一个菜单,一个文本区*/
TextArea tArea;
MenuBar mbr;
FontDialog fontSet; //引用字体设置对话框
NoteEdit()
{
super("记事本");
setSize(521,700);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{ System.exit(0); }
});
tArea=new TextArea();
add("Center",tArea);
mbr=new MenuBar();
/*设置文件菜单项*/
Menu file=new Menu("文件");
MenuItem newFile=new MenuItem("新建");
MenuItem open=new MenuItem("打开");
MenuItem save=new MenuItem("保存");
MenuItem saveAs=new MenuItem("另存为");
MenuItem print=new MenuItem("打印");
MenuItem exit=new MenuItem("退出");
file.add(newFile);
file.add(open);
file.add(save);
file.add(saveAs);
file.addSeparator();
file.add(print);
file.addSeparator();
file.add(exit);
newFile.addActionListener(this);
open.addActionListener(this);
save.addActionListener(this);
saveAs.addActionListener(this);
exit.addActionListener(this);
mbr.add(file);
/*设置编辑菜单项*/
Menu edit=new Menu("编辑");
MenuItem cut=new MenuItem("剪切");
MenuItem copy=new MenuItem("复制");
MenuItem paste=new MenuItem("粘贴");
MenuItem delete=new MenuItem("删除");
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(delete);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
delete.addActionListener(this);
mbr.add(edit);
/*设置格式菜单项*/
Menu model=new Menu("格式");
MenuItem font=new MenuItem("字体...");
model.add(font);
font.addActionListener(this);
mbr.add(model);
/*设置帮助菜单项*/
Menu help=new Menu("帮助");
MenuItem aboutHelp=new MenuItem("关于记事本");
help.add(aboutHelp);
aboutHelp.addActionListener(this);
mbr.add(help);
setMenuBar(mbr);
setVisible(true);
}
public static void main(String[] args)
{
new NoteEdit();
}
/*响应菜单顼事件*/
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="退出")
System.exit(0);
if(e.getActionCommand()=="字体...")
{
fontSet=new FontDialog(this);
fontSet.setVisible(true); //显示字体设置对话框
tArea.setFont(fontSet.font);
}
}
}
FontDialog文件:
import java.awt.*;
import java.awt.event.*;
public class FontDialog extends Dialog implements ItemListener,ActionListener
{
Panel pnl;
Label labSty,labShape,labSize,labColor;
List listSty,listShape,listSize;
Choice choColor;
TextField teffort;
Button butOk,butCancel;
Font font;
Color c;
int sty,shape,size,color; //记录以下数组的下标值
/*列表框和选择框中的项并非字体设置所需参数,以下数组记录字体设置参数信息*/
String[] fontSty={"TimeRoman","Helvetica","Courier","宋体","隶书","楷体","华文彩云"};
int[] fontShape={Font.PLAIN,Font.BOLD,Font.ITALIC,Font.BOLD+Font.ITALIC};
int[] fontSize={8,10,12,14,16,24,30,36,40,48,56,72};
Color[] fontColor= {Color.red,Color.yellow,Color.green,Color.blue,Color.orange,Color.cyan,Color.pink,Color.gray,Color.black};
FontDialog(Frame f)
{
super(f);
setTitle("字体设置");
setSize(400,300);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{ setVisible(false); }
});
pnl=new Panel();
pnl.setLayout(null);
/*字体风格的设置*/
labSty=new Label("字体风格");
listSty=new List(7);
for(int i=0;i<fontSty.length;i++)
listSty.add(fontSty[i]);
labSty.setBounds(30,10,50,20);
listSty.setBounds(20,30,110,100);
listSty.addItemListener(this);
pnl.add(labSty);
pnl.add(listSty);
/*字形的设置*/
labShape=new Label("字形");
listShape=new List();
listShape.add("常规");
listShape.add("粗体");
listShape.add("斜体");
listShape.add("粗斜体");
listShape.addItemListener(this);
labShape.setBounds(185,10,30,20);
listShape.setBounds(150,30,100,100);
pnl.add(labShape);
pnl.add(listShape);
/*字号的设置*/
String[] strSize={"8","10","12","14","16","24","30","36","40","48","56", "72"};
labSize=new Label("字体大小");
listSize=new List();
for(int i=0;i<strSize.length;i++)
listSize.add(strSize[i]);
labSize.setBounds(290,10,50,20);
listSize.setBounds(270,30,100,100);
listSize.addItemListener(this);
pnl.add(labSize);
pnl.add(listSize);
/*字体颜色的设置*/
labColor=new Label("字体颜色");
choColor=new Choice();
String[] colorName={"红色","黄色","绿色","蓝色","橙色","青色","粉色","灰色","黑色"};
for(int i=0;i<colorName.length;i++)
choColor.add(colorName[i]);
labColor.setBounds(30,145,50,20);
choColor.setBounds(20,170,110,30);
choColor.addItemListener(this);
pnl.add(labColor);
pnl.add(choColor);
/*效果预览的设置*/
teffort=new TextField("效果预览");
teffort.setBounds(150,165,120,50);
pnl.add(teffort);
/*按钮的设置*/
butOk=new Button("确定");
butCancel=new Button("取消");
butOk.setBounds(310,160,50,20);
butCancel.setBounds(310,190,50,20);
butOk.addActionListener(this);
butCancel.addActionListener(this);
pnl.add(butOk);
pnl.add(butCancel);
add("Center",pnl);
setVisible(false);
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==listSty)
{
sty=listSty.getSelectedIndex();
font=new Font(fontSty[sty],fontShape[shape],fontSize[size]);
}
if(e.getSource()==listShape)
{
shape=listShape.getSelectedIndex();
font=new Font(fontSty[sty],fontShape[shape],fontSize[size]);
}
if(e.getSource()==listSize)
{
size=listSize.getSelectedIndex();
font=new Font(fontSty[sty],fontShape[shape],fontSize[size]);
}
teffort.setFont(font);
if(e.getSource()==choColor)
{
color=choColor.getSelectedIndex();
c=fontColor[color];
teffort.setForeground(c);
}
}
public void actionPerformed(ActionEvent e)
{
mainWin=new NoteEdit();
if(e.getActionCommand()=="确定")
{
setVisible(false);
}
else if(e.getActionCommand()=="取消")
{
font=new Font("宋体",Font.PLAIN,12);
setVisible(false);
}
}
}
有错误的地方敬请指正,不胜感激!