第六章Java输入输出1、说明程序功能:importjava.io.*;publicclassCharWrite{publicstaticvoidmain(Stringargs[]){try{FileWriterfw=newFileWriter(charset.txt);for(inti=32;i126;i++)fw.write(i);fw.close();}catch(IOExceptione){}}}功能:将ASCII码32到125的字符都写入charset.txt,内容如下2、先用随机函数产生出任意的20个整数,再按由小到大的顺序排序,然后将结果写入一个文件中,最后从该文件中读出后显示出来。请分别用顺序文件和随机文件的读写形式进行编程测试。packagehomework6;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjava.util.*;publicclasstest6_2{/**随机产生20个数,并返回它的迭代器*/publicstaticIteratorIntegercreate_Random(){ArrayListIntegercoll=newArrayList();Randomran=newRandom();intnum;while(coll.size()=20){num=ran.nextInt(80)+10;//重复的不加入if(!coll.contains(num))coll.add(num);}//返回迭代器returncoll.iterator();}/**转化成字符串*/publicstaticStringproToString(IteratorIntegercoll){intnum,j=0;IntegerNum;Strings0=;while(coll.hasNext())s0=newString(s0+coll.next()+);returns0.substring(0,s0.length()-1);}//根据空格分割字符串publicstaticint[]splitString(Strings){Stringstr[]=s.split();int[]a=newint[str.length];for(inti=0;ia.length;i++)a[i]=Integer.parseInt(str[i]);returna;}publicstaticvoidmain(String[]args){System.out.println(这些数是:);Stringstr=proToString(create_Random());//随机产生的数转换成int[]int[]aa=splitString(str);System.out.println(这些数是:);System.out.println(str);System.out.println();//排序Arrays.sort(aa);//转换成StringStringstr1=Arrays.toString(aa).replace(,,).replace([,).replace(],);//写入文件,并从文件输出try{FileWriterfw=newFileWriter(random.txt);fw.write(str1);fw.close();FileReaderfr=newFileReader(random.txt);char[]s=newchar[200];fr.read(s);fr.close();System.out.println(str1.valueOf(s));}catch(IOExceptione){}}}3、统计一个文本文件中单词的个数。文本文件的名字从命令行中获得。packagehomework6;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjava.nio.ByteBuffer;publicclasstest6_3{publicstaticvoidmain(String[]arg){try{FileReaderfr=newFileReader(arg[0]);char[]s=newchar[1000];fr.read(s);fr.close();Stringstr=String.valueOf(s);//分割匹配,所有不是a-z和A-Z的字符都是分割符Stringss[]=str.split([^a-z^A-Z]);System.out.println(被分割成:);for(inti=0;iss.length;i++)System.out.println(ss[i]);System.out.println(单词数是:+ss.length);}catch(IOExceptione){}}}记事本上的程序输出第七章Java图形用户界面1、阅读下面的程序,回答问题。importjava.awt.*;importjavax.swing.*;publicclassTextendsJFrame{publicT(){super(GridLayout);Containercon=this.getContentPane();con.setLayout(newGridLayout(2,3));con.add(newJButton(a));con.add(newJButton(b));con.add(newJButton(c));con.add(newJButton(d));con.add(newJButton(e));con.add(newJButton(f));setSize(200,80);setVisible(true);}publicstaticvoidmain(Stringargs[]){newT();}}画图表示程序运行后的图形界面。如果程序通过实现某个接口处理按钮的动作事件,则该接口名为何?接口中的方法头声明如何?接口ActionListener方法publicvoidactionPerformed(ActionEvente)2、编写一个简单的计算器,要求图形用户界面如下图所示。packagehomework7;importjava.awt.*;importjavax.swing.*;publicclasstest7_2_panelextendsJPanel{publictest7_2_panel(){GridLayoutgla=newGridLayout(4,5);//setSize(newDimension(8,);setLayout(gla);gla.setHgap(2);gla.setVgap(2);add(newJButton(newImageIcon(src\\homework7\\CalIcon\\i7.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\i8.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\i9.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\ijia.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\ic.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\i4.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\i5.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\i6.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\ijian.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\igenhao.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\i1.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\i2.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\i3.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\icheng.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\im2.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\i0.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\ipoint.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\iequal.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\ichu.png)));add(newJButton(newImageIcon(src\\homework7\\CalIcon\\ixian.png)));setVisible(true);}publicstaticvoidmain(Stringsarg[]){JFramejf=newJFrame();jf.add(newtest7_2_panel());jf.setTitle(计算器);jf.setVisible(true);}}3、请编写一个简单的用户登录程序。要求使用图形用户界面,用户名和密码假定均为java,且密码输入时全部显示为星号(*)。packagehomework7;importjava.awt.Container;importjava.awt.FlowLayout;importjava.awt.Font;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.*;publicclasstest7_3extendsJFrame{privateJLabeluse,password;//用户名输入框privateJTextFieldk1;//密码输入框privateJPasswordFieldk2;privateJButtonb1,b2;publictest7_3(){use=newJLabel(用户名:);use.setFont(newFont(Serif,Font.PLAIN,25));password=newJLabel(密码:);password.setFont(newFont(Serif,Font.PLAIN,25));k1=newJTextField(12);k2=newJPasswordField(12);b1=newJButton(登录);b2=newJButton(退出);Containerc=getContentPane();c.setLayout(newFlowLayout());c.add(use);c.add(k1);c.add(p