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