![Java程序设计试验6学生版_第1页](http://file3.renrendoc.com/fileroot_temp3/2021-12/23/ea8ff5ee-c9cd-4224-a1f9-1849d05fe2ab/ea8ff5ee-c9cd-4224-a1f9-1849d05fe2ab1.gif)
![Java程序设计试验6学生版_第2页](http://file3.renrendoc.com/fileroot_temp3/2021-12/23/ea8ff5ee-c9cd-4224-a1f9-1849d05fe2ab/ea8ff5ee-c9cd-4224-a1f9-1849d05fe2ab2.gif)
![Java程序设计试验6学生版_第3页](http://file3.renrendoc.com/fileroot_temp3/2021-12/23/ea8ff5ee-c9cd-4224-a1f9-1849d05fe2ab/ea8ff5ee-c9cd-4224-a1f9-1849d05fe2ab3.gif)
![Java程序设计试验6学生版_第4页](http://file3.renrendoc.com/fileroot_temp3/2021-12/23/ea8ff5ee-c9cd-4224-a1f9-1849d05fe2ab/ea8ff5ee-c9cd-4224-a1f9-1849d05fe2ab4.gif)
![Java程序设计试验6学生版_第5页](http://file3.renrendoc.com/fileroot_temp3/2021-12/23/ea8ff5ee-c9cd-4224-a1f9-1849d05fe2ab/ea8ff5ee-c9cd-4224-a1f9-1849d05fe2ab5.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实用文档华北电力大学实验报告|实验名称 Application 及Applet程序设计学生姓名: 成 绩: 实验日期:课程名称Java程序设计专业班级:学 号:指导教师:张学斌、实验目的和要求1掌握Java Swing组件的使用方法;2理解委托代理事件处理模型掌握;3掌握窗口菜单和快捷菜单设计方式;4掌握在组件上绘图的方法;5掌握Applet应用程序的设计方法二、实验环境Windows2000/Windows XP JDK 1.21.6三、实验内容和步骤要求根据实验内容,写出详细实验过程和步骤,必要时截图.实验1算术测试1. 实验要求编写一个算术测试小软件,用来练习小学生的算术水平.程序有三个
2、类组成,其中Teacher对象充当监视器,负目,并判断答复者的答案是否正确;ComputerFrame对象负责为算术题目提供视图,比方用户可以通过Computed的GUI界面看到题目,并通过 GUI界面给出题目的答案; MailClass是软件的主类.2. 程序模板Mai nClass.javapublic class Main Class public static void main(String args) ComputerFrame frame;frame=new ComputerFrame.;frame.setTitle(算术测试");frame.setBou nds(10
3、0,100,650,180);ComputerFrame.javaimport java.awt.*;import java.awt.eve nt.*;import javax.swi ng.*;public class ComputerFrame exte nds JFrame JMe nuBar men ubar;JMe nu choiceGrade; /选择级别的菜单JMe nultem grade1,grade2;JTextField text On e,textTwo,textResult;JButt on getProblem,giveA nwser;JLabel operator
4、Label,message;Teacher teacherZhang;ComputerFrame() teacherZha ng=new Teacher();teacherZha ng.setMax In teger(20); setLayout (new FlowLayout(); men ubar = new JMe nuBar();choiceGrade = new JMenu(选择级别");grade1 = new JMenultem(幼儿级别);grade2 = new JMenultem(儿童级别);grade1.addActi on Liste ner(new Acti
5、 on Liste ner() public void action Performed(Act ionEvent e) teacherZha ng.setMax In teger(10););grade2.addActio nListe ner(new Acti on Liste ner() public void action Performed(Act ionEvent e) teacherZha ng.setMax In teger(50););choiceGrade.add(grade1);choiceGrade.add(grade2);men ubar.add(choiceGrad
6、e);setJMe nuBar(me nubar);【代码1】/创立textOne,其可见字符长是5textTwo=new JTextField(5);textResult= new JTextField(5);operatorLabel=new JLabel("+"); operatorLabel.setFo nt(new Fon t("Arial",Fo nt.BOLD,20); message=new JLabel("你还没有答复呢);getProblem=new JButton(获取题目);giveAnwser= new JButton
7、(确认答案");add(getProblem);add(textO ne);add(operatorLabel);add(textTwo);add(new JLabel("=");add(textResult);add(giveA nwser);add(message);textResult.requestFocus();text On e.setEditable(false);textTwo.setEditable(false);getProblem.setActio nComma nd("getProblem"); textResult.s
8、etActio nComma nd("a nswer"); giveA nwser.setActio nComma nd("a nswer");teacherZha ng.setJTextField(text On e,textTwo,textResult);teacherZha ng.setJLabel(operatorLabel,message);【代码2】/将teacherZhang【代码3】/将teacherZhang【代码4】/将teacherZhang注册为getProblem的 注册为giveAnwser 的 注册为textResult 的
9、Actio nEve nt事件监视器Actio nEve nt事件监视器Actio nEve nt事件监视器setVisible(true);validate();setDefaultCloseOperatio n( DISPOSE_ON_CLOSE);Techaer.javaimport java.util.Ra ndom;import java.awt.eve nt.*;import javax.swi ng.*;public class Teacher impleme nts Action Liste ner int nu mberO ne,nu mberTwo;String opera
10、tor=""boolea n isRight;Ra ndom ran dom; / 用于给出随机数 int maxI nteger; /题目中最大的整数JTextField text On e,textTwo,textResult;JLabel operatorLabel,message;Teacher() ran dom = new Ran dom();public void setMax In teger(i nt n) maxln teger =n;public void action Performed(Act ionEvent e) String str = e.
11、getAct ion Comma nd(); if(str.equals("getProblem") 至maxinteger 之间的随机数nu mberO ne = random.n ext In t(max In teger)+1;/1nu mberTwo=ra ndom. nextl nt(maxl nteger)+1;double d=Math.random(); /获取(0,1)之间的随机数if(d>=0.5)operator="+" elseoperator="-"text On e.setText("&qu
12、ot;+numberO ne); textTwo.setText(""+nu mberTwo); operatorLabel.setText(operator); message.setText("请答复");textResult.setText (n ull);else if(str.equals("a nswer") Stri ng an swer=textResult.getText();try int result= In teger.parse In t(a nswer);if(operator.equals("+
13、")if(result=nu mber One+nu mberTwo) message.setText(你答复正确);elsemessage.setText(你答复错误");else if(operator.equals("-")if(result=nu mber One-nu mberTwo)message.setText(你答复正确);elsemessage.setText(你答复错误");catch(NumberFormatExcepti on ex) message.setText(请输入数字字符);public void setJTe
14、xtField(JTextField . t) textO ne=t0;textTwo=t1;textResult=t2;public void setJLabel(JLabel .l abel) operatorLabel=label0;message=label1;3. 实验指导Jbutton 对象可触发 ActionEvent事件.为了能监视到此类事件,事件源必须使用addActionListener方法获得监视器的类必须实现接口Actio nListe ner.4. 实验扩展(1) 模仿本实验代码,再增加“小学生级别.(2) 给出上述程序增加测试乘法的功能.实验2布局与日历1. 实验要
15、求编写一个应用程序,有一个窗口,该窗口的布局为BorderLayout 布局.窗口的中央添口一个JPanel容器 pCe的布局是7行7列的GriderLayout布局,pCenter中放置49个标签,用来显示日历.窗口的北面添加一个 JPane 其布局是 FlowLayout布局,pNorth放置两个按钮:nextMonth和previousMonth.单击nextMonth按钮,可以 下一月的日历;单击 previousMonth按钮,可以显示当前月的上一月的日历;窗口的南面添加一个 Jpanel容器局是FlowLayout布局,pSouth中放置一个标签用来显示一些信息.2. 程序模板Ca
16、le ndarMa in Class.javapublic class Cale ndarMa in Class public static void main(String args) Cale ndarFrame frame=new Cale ndarFrame();frame.setBou nds(100,100,360,300);frame.setVisible(true);frame.setYearA ndMo nth(2021,5);Cale ndarBea n.javaimport java.util.Cale ndar;public class Cale ndarBea n S
17、tri ng day;int year=2005,m on th=0;public void setYear(i nt year) this.year=year;public int getYear() retur n year;public void setM on th(i nt mon th) this.m on th=mo nth;public int getMonth() return mon th;public String getCalendar() Stri ng a=new Stri ng42;Cale ndar日历=Cale ndar.get In sta nce();日历
18、.set(year,month-1,1);int 星期几=日历.get(Calendar.DAY_OF_WEEK)-1;int day=0;if(mon th=1|m on th=3|m on th=5|m on th=7|m on th=8|m on th=10|m on th=12)day=31;if(mon th=4|m on th=6|m on th=9|m on th=11)day=30;if(mon th=2) if(year%4=0)&&( year%100!=0)|(year%400=0)day=29;elseday=28;for(int i=星期几,n=1;i
19、< 星期几 +day;i+) ai=String.valueOf(n);n+;return a;Cale ndarFrame.javaimport java.util.*;import java.awt.*;import java.awt.eve nt.*;import javax.swi ng.*;import javax.swi ng.border.*;public class Cale ndarFrame exte nds JFrame impleme nts Action Liste ner JLabel labelDay=new JLabel42;JButton titleNa
20、me=new JButton7;String name="日","一","二","三","四","五","六"JButt on n extM on th,previousM on th;Cale ndarBea n cale ndar;JLabel showMessage=new JLabel("",JLabel.CENTER);int year=2021,mo nth=2;public Cale ndarFrame() JPa nel
21、 pCe nter=new JPa nel();布局.【代码1】/将pCenter的布局设置为7行7列的GridLayout for(int i=0;i<7;i+) titleNamei=new JButton(namei);titleNamei.setBorder(new SoftBevelBorder(BevelBorder.RAISED); pCe nter.add(titleNamei);for(int i=0;i<42;i+) labelDayi=new JLabel("",JLabel.CENTER);labelDayi.setBorder( new
22、 SoftBevelBorder(BevelBorder 丄OWERED);【代码 2 /pCenter 添加组件 labelDayi.cale ndar=new Cale ndarBea n();nextMonth=new JButton(下月");previousMonth=new JButton(上月");n extMo nth.addActio nListe ner(this);previousM on th.addActio nListe ner(this);JPanel pNorth=new JPa nel(),pSouth=new JPa nel();pNor
23、th.add(previousM on th);pNorth.add( nextM on th);pSouth.add(showMessage);add(pCe nter,BorderLayout.CENTER);【代码3】/窗口添加pNorth在北面区域【代码4】/窗口添加pSouth在南区域.setYearA ndMon th(year,m on th);setDefaultCloseOperati on (DISPOSE_ON_CLOSE);public void setYearA ndMon th(i nt y,i nt m) cale ndar.setYear(y);cale nda
24、r.setM on th(m);String day=cale ndar.getCale ndar();for(int i=0;i<42;i+)labelDayi.setText(dayi);showMessage.setText(日历:"+cale ndar.getYear()+"年"+ cale ndar.getMo nth()+"月);public void actio nPerformed(Actio nEvent e) if(e.getSource()=n extM on th) mon th=mon th+1;if(mo nth>
25、12)mon th=1;cale ndar.setM on th(m on th);String day=cale ndar.getCale ndar();for(int i=0;i<42;i+) labelDayi.setText(dayi);else if(e.getSource()=previousM on th) mon th=mo nth-1;if(mon th<1)mon th=12;cale ndar.setM on th(m on th);String day=cale ndar.getCale ndar();for(int i=0;i<42;i+)label
26、Dayi.setText(dayi);showMessage.setText( 日历:"+calendar.getYear()+"年"+calendar.getMonth()+"月);3. 实验扩展在CalendarFrame类中增加一个JTextField 文本框,用户可以通过在文本框中输入年份来修改calendar对象的实验3英语单词拼写练习1. 实验要求编写一个应用程序,要求如下:窗口有一个TextField 对象和一个按钮对象,将这两个对象添加到一个面板中,然后将该面板添加到窗口的上 用户在TextField对象中输入一个英文单词,然后按Ente
27、r或单击“确定按钮,程序将创立假设干个不可编辑个文本框随机显示英文单词中的一个字母.要求将这些文本框按一行添加到一个面板中,然后将该面板添加用户用鼠标单击一个文本框后,通过按下键盘上的和“T键交换相邻文本框中的字母,使得这些文本 排列顺序和英文单词中字母的顺序相同.2. 程序模板WordMai nClass.javapublic class WordMai nClass public static void main(String args) new Spelli ngWordFrame();Ron domStri ng.javapublic class Ron domStri ng /负责随
28、机排列单词中的字母Stri ng str=""public String getRondomString(String s) Stri ngBuffer strBuffer =new Stri ngBuffer(s);int m=strBuffer.le ngth();for(int k=0;k<m;k+) intindex=(int)(Math.random()*strBuffer.length();/Math.random()返回(0,1)之间的随机数char c=strBuffer.charAt(i ndex);str=str+c;strBuffer=strBu
29、ffer.deleteCharAt(i ndex);return str;LetterLabel.javaimport java.awt.*;import java.awt.eve nt.*;import javax.swi ng.*;public class LetterLabel exte nds JTextField impleme nts FocusListe ner LetterLabel() setEditable(false);addFocusListener(this);/【代码1】/将当前对象注册为自身的焦点视器setBackgro un d(Color.white);set
30、Font(new Font("Arial",Font.PLAIN,30);public static LetterLabel getLetterLabel(int n) LetterLabel a=new LetterLabel n;for(i nt k=O;k<aen gth;k+)ak=new LetterLabel();return a;public void focusGa in ed(FocusEve nt e) setBackgro un d(Color.cya n);public void focusLost(FocusEve nt e) setBack
31、gro un d(Color.white);public void setText(char c) setText(""+c);Spelli ngWordFrame.javaimport java.awt.*;import java.awt.eve nt.*;import javax.swi ng.*;public class Spelli ngWordFrame exte nds JFrame impleme nts KeyListe ner,Actio nListe ner JTextField in putWord;JButton butt on;LetterLabe
32、l label;JPa nel no rthP,ce nterP;Box wordBox;Stri ng hi ntMessage="用鼠标单击字母,按左右箭头交换字母,将其排列成所输入的单词JLabel messaageLabel=new JLabel(h in tMessage);String word=""Spelli ngWordFrame() inputWord=new JTextField(12);butto n=new JButt on (确定");butt on .addAct ion Liste ner(this);in putWord
33、.addAct ion Liste ner(this);n orthP=new JPa nel();northP.add(new JLabel("输入单词:");n orthP.add(i nputWord);northP.add(butto n);cen terP=new JPa nel();wordBox=Box.createHoriz on talBox();cen terP.add(wordBox);add( northP,BorderLayout.NORTH);add(ce nterP,BorderLayout.CENTER);add(messaageLabel,
34、BorderLayout.SOUTH);setBou nds(100,100,350,180);setVisible(true);validate.;setDefaultCloseOperatio n( DISPOSE_ON_CLOSE);public void acti on Performed(Act ion Eve nt e) word=i nputWord.getText();int n=word .len gth();Ron domStri ng ron dom=new Ron domStri ng();Stri ng ran domWord=ro ndom.getR on domS
35、tri ng(word);wordBox.removeAll();messaageLabel.setText(h in tMessage);if(n >0) label=LetterLabel.getLetterLabel( n);for(i nt k=0;k<label.le ngth;k+) labelk.setText(""+ra ndomWord.charAt(k); wordBox.add(labelk);的键盘监视器【代码2】/将当前窗口注册为labelkvalidate();in putWord.setText( null);label0.requ
36、estFocus();public void keyPressed(KeyEve nt e) LetterLabel sourceLabel=(LetterLabel)e.getSource(); int in dex=-1;if(e.getKeyCode()=KeyEvent.VK_LEFT) for(int k=0;k<label.length;k+) if(labelk=sourceLabel) in dex=k;break;if(in dex!=0) /交换文本框中的字母Stri ng temp=labeli ndex.getText();labeli ndex.setText(
37、labeli ndex-1.getText();label in dex-1.setText(temp);label in dex-1.requestFocus();else if( 【代码3】) /判断按下的是否是宀键for(int k=0;k<label.length;k+) if(labelk=sourceLabel) in dex=k;break;if(in dex!=label.le ngth-1) Stri ng temp=labeli ndex.getText();labeli ndex.setText(labeli ndex+1.getText();label in de
38、x+1.setText(temp);label in dex+1.requestFocus();validate();public void keyTyped(KeyEve nt e)public void keyReleased(KeyEve nt e) String success=""for(int k=0;k<label.length;k+) Stri ng str=labelk.getText();success=success+str;if(success.equals(word) messaageLabel.setText(恭喜你,你成功了 );for(
39、int k=0;k<label.length;k+) labelk.removeKeyListe ner(this);labelk.removeFocusListe ner(labelk);labelk.setBackgro un d(Color.white);in putWord.requestFocus();3.实验扩展增加记录用户移动字母次数的功能,即当用户拼写成功后,messageLabel标签显示的信息中包含用户移动字母的实验4 字体对话框1.实验要求编写一个FontFamily类,该类对象可以获取当前机器可用的全部字体名称.编写一个JDialog的子类FontDialog,该
40、类为FontFamily对象维护的数据提供视图,要求 FontDialog 对象使 示FontFamily对象维护的全部字体的名称,中选择下拉列表中某个字体名称后,FontDialog 对象用标签显示该字求对话框提供返回下拉列表中所选择的字体名称的方法.创立的编写一个窗口,该窗口中有"设置字体按钮和一文本区对象,当单击该按钮时,弹出一个Fon tDialog根据用户在对话框下拉列表中选择的字体来显示文本区中的文本.2.程序模板Fon tDialogMa in Class.javapublic class Fon tDialogMa in Class public static voi
41、d main(String args) FrameHaveDialog win=new FrameHaveDialog(); Fon tFamilyNames.javaimport java.awt.GraphicsE nviro nment;public class Fon tFamilyNames Stri ng allFo ntNames;public String getFo ntName() GraphicsE nvironment ge=GraphicsE nviro nmen t.getLocalGraphicsE nvironmen t();allF on tNames=ge.
42、getAvailableF on tFamilyNames();return allF on tNames;Fon tDialog.javaimport java.awt.eve nt.*;import java.awt.*;import javax.swi ng.*;public class Fon tDialog exte nds JDialog impleme nts ItemListe ner,Actio nListe ner Fon tFamilyNames fon tFamilyNames;int fon tSize=38;Stri ng fon tName;JComboBox f
43、on tNameList,fo ntSizeList;JLabel label;Font font;JButt on yes,ca ncel;static int YES=1,NO=0;int state=-1;Fon tDialog(JFrame f) super(f);setTitle(字体对话框");font=new Font(宋体",Font.PLAIN,12);fon tFamilyNames=new Fon tFamilyNames();【代码1】/当前对话框调用setModal(boolean b)设置为有模式yes=new JButt on ("Y
44、es");can cel=new JButt on ("ca ncel");yes.addAct ion Liste ner(this);can cel.addActio nListe ner(this);label=new JLabel("hello,奥运 ,JLabel.CENTER);fon tNameList=new JComboBox();fon tSizeList=new JComboBox();String name=fontFamilyNames.getFontName();fon tNameList.addltem(字体");
45、for(i nt k=O;k <n ameen gth;k+)fon tNameList.addItem( namek);fon tSizeList.addltem(大小");for(int k=8;k<72;k=k+2)fon tSizeList.addltem( new In teger(k);fon tNameList.addItemListe ner(this);fon tSizeList.addItemListe ner(this);JPa nel pNorth=new JPa nel();pNorth.add(fo ntNameList);pNorth.add
46、(fo ntSizeList);add(pNorth,BorderLayout.NORTH); add(label,BorderLayout.CENTER); JPa nel pSouth=new JPan el();pSouth.add(yes);pSouth.add(ca ncel); add(pSouth,BorderLayout.SOUTH);setBou nds(100,100,280,170);setDefaultCloseOperatio n( DISPOSE_ON_CLOSE); validate();public void itemStateCha nged(ltemEve
47、nt e) if(e.getSource()=fontNameList) fon tName=(Stri ng)fo ntNameList.getSelectedltem(); font=new Fon t(fo ntName,Fo nt. PLAIN,fo ntSize);else if(e.getSource()=fontSizeList) In teger m=(l nteger)fo ntSizeList.getSelectedltem();fon tSize=m.i ntValue();font=new Fon t(fo ntName,Fo nt. PLAIN,fo ntSize);
48、label.setF on t(fo nt);label.repa in t();validate();public void action Performed(Act ionEvent e) if(e.getSource()=yes) state=YES;【代码2】/对话框设置为不可见else if(e.getSource()=ca ncel) state=NO;【代码3】/对话框设置为不可见public in t getState() return state;public Font getF on t() return fon t;FrameHaveDialog.javaimport j
49、ava.awt.eve nt.*;import java.awt.*;import javax.swi ng.*;public class FrameHaveDialog exte nds JFrame impleme nts Action Liste ner JTextArea text;JButton butt on Fo nt;FrameHaveDialog.buttonFont= new JButton(设置字体");text =new JTextArea("Java 2实用教程(第四版) );butt onFon t.addAct ion Liste ner(th
50、is);add(butto nFo nt,BorderLayout.NORTH);add(text);setBou nds(60,60,300,300);setVisible(true);validate();setDefaultCloseOperatio n( DISPOSE_ON_CLOSE);public void action Performed(Act ionEvent e) if(e.getSource()=butt onFont) Fon tDialog dialog=new Fon tDialog(this);dialog.setVisible(true);if(dialog.
51、getState()=FontDialog.YES) text.setF on t(dialog.getF on t();text.repai nt();if(dialog.getState()=FontDialog.NO) text.repai nt();实验5华容道游戏1. 实验要求首先,编写一个按钮的子类,该子类创立的对象代表华容道中的任务.通过焦点事件限制人物的颜色,当人物 色为蓝色,当失去焦点时颜色为灰色.我们通过键盘事件和鼠标事件来实现曹操,关羽等人物的移动.当人物 件或键盘事件时,如果鼠标指针的位置是在人物的下方(也就是组件的下半局部)或按键盘上的键,人 向左,向右和向上移动原理
52、类似.2. 程序模板MoveExample.javaimport java.awt.*;import java.applet.*;import java.awt.eve nt.*;import javax.swi ng.*;public class MoveExample public static void main(String args) new Hua_R on g_Road();class Pers on exte nds JButt on impleme nts FocusListe ner int nu mber;Color c;Person (i nt nu mber,Stri
53、 ng s) super(s);this .nu mber =nu mber; c=getBackgro un d();setFont(new Font("宋体 ,Font.CENTER_BASELINE,14);【代码1】/当前按钮注册为本身的监视器public void focusGa in ed(FocusEve nt e) setBackgro un d(Color.cya n);public void focusLost(FocusEve nt e) setBackgro un d(c);class Hua_R on g_Road exte nds JFrame imple
54、me nts KeyListe ner,MouseListe ner,Actio nListe ner Pers on pers on =new Pers on 10;JButt on left,right,above,below;JButton restart=new JButton(重新开始");Container con;public Hua_Ro ng_Road() in it();setBou nds(100,100,320,360);setVisible(true);validate.;setDefaultCloseOperatio n(JFrame.EXIT_ON_CL
55、OSE);public void in it() con=getC onten tPa ne();con. setLayout (n ull);con. add(restart);restart.setBou nds(100,5,120,25);restart.addAct ion Liste ner(this);String name="曹操","关羽","张","刘",马","许","兵","兵",兵","兵"for(int i=0;i<name.length;i+) pers on i=new Pers on (i ,n amei);【代码2】/将当前窗口注册为personi的KeyEvent事件监视器【代码3】/将当前窗口注册为personi的Mou
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论