第十一章java练习.doc

第十一章java练习.doc

ID:60787005

大小:17.50 KB

页数:2页

时间:2020-12-18

第十一章java练习.doc_第1页
第十一章java练习.doc_第2页
资源描述:

《第十一章java练习.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、1.编写窗体应用程序,统计一个文本域输入文本的行数、单词数和字符数。可在图形界面中安排一个按钮、一个文本域和一个标签,点击按钮开始统计,在标签中显示结果。packagea.b.c;importjava.awt.*;importjava.awt.event.*;importjava.applet.*;publicclassex11_1extendsFrameimplementsActionListener{TextAreatextInput;Labelres;publicex11_1(){textIn

2、put=newTextArea(5,10);Panelp=newPanel();p.setLayout(newGridLayout(3,1,2,2));ButtoncountButton=newButton("ProcesstheText");countButton.addActionListener(this);p.add(countButton);res=newLabel("result......");p.add(res);setLayout(newBorderLayout(2,2));add

3、(textInput,BorderLayout.CENTER);add(p,BorderLayout.SOUTH);setSize(300,300);setVisible(true);}publicvoidactionPerformed(ActionEventevt){Stringtext;intcharCt,wordCt,lineCt;text=textInput.getText();charCt=text.length();eordCt=0;for(inti=0;i

4、oleanstartOfWord;if(ICharacter.isLetter(text.charAt(i)))startOfWord=false;elseif(i==0)startOfWord=true;elseif(Character.isLetter(Text.charAt(i-1)))startOfWord=false;elsestartOfWord=true;if(startOfWord)wordCt++;}lineCt=1;for(inti=0;i

5、.charAt(i)=='')lineCt++;}res.setText("Lines:"+lineCt+",Words:"+wordCt+",Chars:"+charCt);}publicstaticvoidmain(Stringargs[]{newex11_1();}2.编写窗体应用程序,安排一个文本框、一个按钮和一个标签,从文本框录入一个数字(0~9),点击按钮将其对应的英文单词(如:zero,one等)显示在标签中。packagea.b.c;importjava.awt.*;import

6、java.awt.event.*;publicclassex11_2extendsFrameimplementsActionListener{Labelres;TextFieldmy;Stringword[]={"zero","one","two","three","four","five","six","seven","eight","nine"};publicex11_2(){my=newTextField(20);res=newLabel("英文单词");setLayout(newFlowLa

7、yout());add(my);add(res);my.addActionListener(this);}publicvoidactionPerformed(ActionEvente){Strings=my.getText();intn=Integer.parseInt(s);res.setText(word[n]);}publicstaticvoidmain(Stringargs[]){Framemy=newex11_2();my.setSize(300,300);my.setVisible(tr

8、ue);}}

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。