欢迎来到天天文库
浏览记录
ID:60787005
大小:17.50 KB
页数:2页
时间:2020-12-18
《第十一章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;i4、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;i5、.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.*;import6、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(newFlowLa7、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(tr8、ue);}}
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;i5、.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.*;import6、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(newFlowLa7、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(tr8、ue);}}
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);}}
此文档下载收益归作者所有