java课程设计模版_第1页
java课程设计模版_第2页
java课程设计模版_第3页
java课程设计模版_第4页
java课程设计模版_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

、项目内容、要求与分组情况总体介绍本次实验的目标是制作一个计算器,可以进行简单的四则运算(加、减、乘、除)。小组的具体分工情况见下表:2、概要设计2.1开发工具及环境文本编辑器:记事本;运行环境:命令提示符(DOS环境)。2.2任务及需求分析设计简易计算器程序,实现简单的运算(加减乘除),具体任务如下:(1)计算器窗口界面布局设计;(2)各种计算功能的算法分析;(3)编程实现具体的计算功能,实现类的封装处理;(4)运行测试程序,调试纠正运行错误。2.3工作原理(1)工作过程点击相应的数字键,即会在显示文档中显示该数字。进行相应的运算,首先输入第一个计算数字,然后输入运算符,再输入第二个计算数字,点击“=”按钮即得计算结果。同时,还有归零功能,点击该按钮,显示栏中即可归零。(2)工作原理利用图形界面编程实现,利用到的知识点包括:窗体的创建和应用界面布局;事件处理,包括按钮的动作事件处理,窗体事件。接口知识:事件处理中通过接口约束事件监听者必须实现相应接口中定义的方法。流程控制语句:通过条件判断区分不同按钮,作不同操作处理。3、具体实现3.1类设计(1)类名:jisuanqi作用:实现窗口的布局设定,事件注册。该类继承WindowAdapter类。类中含有的方法介绍:frame()方法:实现窗体的界面布局及按钮的事件注册。main()方法:应用程序的入口,完成创建对象并调用frame()方法。(2)类名:close该类为jisuanqi的内嵌类,作用是实现对关闭按钮的动作事件监听处理。该类实现ActionListener接口。类中含有的方法介绍:actionPerformed方法:实现关闭按钮的动作事件处理。(3)类名:buttonlistener该类为jisuanqi的内嵌类,作用是实现计算器中数字按钮和各类运算按钮等的动作事件监听处理。该类实现ActionListener接口。类中含有的方法介绍:actionPerformed方法:实现各类按钮的动作事件处理。jisuan()方法:实现具体运算操作。3.2关键技术处理(1)窗体界面设计及注册事件监听Framefm=newFrame("简单计算器");for(inti=0;i<=16;i++){b[i]=newButton(ss[i]);}for(inti=0;i<=15;i++){p2.add(b[i]);}//创建按钮并添加到P2b[16].setBackground(Color.yellow);txt=newTextField(15);txt.setEditable(false);for(inti=0;i<=16;i++){b[i].addActionListener(newbuttonlistener());……}注:此方法主要设置窗口、面板、各个按键。对各个按键进行定义(定义好各按键该实现什么功能)、排布,将各个按键注册到监听器上。(2)按钮事件处理区分事件源:通过事件对象的getSource()方法得到事件源。关键代码如下:Buttonbtn=(Button)e.getSource();if(btn.getLabel()=="="){……}改变计算器的结果。更改文本框的显示,先读原有的数据。txt.setText(txt.getText()+btn.getLabel());清除计算器的显示if(btn.getLabel()=="归零")txt.setText("");(3)窗体关闭classcloseimplementsActionListener{//退出publicvoidactionPerformed(ActionEvente){System.exit(0);}}【注】也可以执行窗体的dispose()方法实现窗体关闭。(4)记住前一个运算符引入一个字符串类型的类变量s记录前一个运算符,每次按运算符是先执行前一个运算符的结果。4、运行调试与分析讨论调试运行4*5运算,运算过程如下所示:测试程序,点击数字键可将数据输入到文本框,点击“+”、“-”、“*”、“/”按钮可进行前一个运算,点击“=”可完成整个运算。从而达到能进行加、减、乘、除运算的简易计算器的功能。点击关闭图标可关闭计算器。设计体会与小结通过独立完成五子棋对弈系统的后,发现自己在进行软件开发方面提高不少,同时积极利用所学到的新技术用于自己的设计开发过程。另外,在整个开发的过程中,时间也比较仓促。因此,该系统必然会存在一些缺陷和不足。如:没有讨论五子棋禁手的问题。另一个就是电脑按即定的算法去与玩家下子。这种算法有“固定”。不太会变,玩家若是仔细观察,可以掌握其规律。还有就是界面不是很华丽有待改进。尽管本系统存在着很多不足,但其实现了最重要的功能就是有人工智能。这也让我对计算机中的人工智能领域有一定的了解。另一个就在做系统的过程中,我学到了Visual

C++的一些基本结构,尤其对于MFC有一定的了解。还有就是对于C++有更深一步的认识。6、参考文献[1]丁振凡,《JAVA语言程序设计》,清华大学出版社.[2]丁振凡,《JAVA语言程序设计实验指导与习题解答》,清华大学出版社.[3][4]/附录:(源程序)importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;classChessFrameextendsJFrameimplementsActionListener{privateString[]strsize={"标准棋盘","大号棋盘","超大号棋盘"};privateString[]strmode={"人机对弈","人人对战"};publicstaticbooleaniscomputer=true,checkcomputer=true;privateintwidth,height;privateChessModelcm;privateMainPanelmp;publicChessFrame(){this.setTitle("五子棋游戏");cm=newChessModel(1);mp=newMainPanel(cm);Containercon=this.getContentPane();con.add(mp,"Center");this.setResizable(false);this.addWindowListener(newChessWindowEvent());MapSize(14,14);JMenuBarmbar=newJMenuBar();this.setJMenuBar(mbar);JMenugameMenu=newJMenu("游戏");mbar.add(makeMenu(gameMenu,newObject[]{"再来一局",null,"棋盘",null,"模式",null,"认输",null,"悔棋",null,"退出"},this));JMenulookMenu=newJMenu("外观");mbar.add(makeMenu(lookMenu,newObject[]{"类型一","类型二","类型三"},this));JMenuhelpMenu=newJMenu("版本");mbar.add(makeMenu(helpMenu,newObject[]{"关于"},this));}publicJMenumakeMenu(Objectparent,Objectitems[],Objecttarget){JMenum=null;if(parentinstanceofJMenu)m=(JMenu)parent;elseif(parentinstanceofString)m=newJMenu((String)parent);elsereturnnull;for(inti=0;i<items.length;i++)if(items[i]==null)m.addSeparator();elseif(items[i]=="棋盘"){JMenujm=newJMenu("棋盘");ButtonGroupgroup=newButtonGroup();JRadioButtonMenuItemrmenu;for(intj=0;j<strsize.length;j++){rmenu=makeRadioButtonMenuItem(strsize[j],target);if(j==0)rmenu.setSelected(true);jm.add(rmenu);group.add(rmenu);}m.add(jm);}elseif(items[i]=="模式"){JMenujm=newJMenu("模式");ButtonGroupgroup=newButtonGroup();JRadioButtonMenuItemrmenu;for(inth=0;h<strmode.length;h++){rmenu=makeRadioButtonMenuItem(strmode[h],target);if(h==0)rmenu.setSelected(true);jm.add(rmenu);group.add(rmenu);}m.add(jm);}elsem.add(makeMenuItem(items[i],target));returnm;}publicJMenuItemmakeMenuItem(Objectitem,Objecttarget){JMenuItemr=null;if(iteminstanceofString)r=newJMenuItem((String)item);elseif(iteminstanceofJMenuItem)r=(JMenuItem)item;elsereturnnull;if(targetinstanceofActionListener)r.addActionListener((ActionListener)target);returnr;}publicJRadioButtonMenuItemmakeRadioButtonMenuItem(Objectitem,Objecttarget){JRadioButtonMenuItemr=null;if(iteminstanceofString)r=newJRadioButtonMenuItem((String)item);elseif(iteminstanceofJRadioButtonMenuItem)r=(JRadioButtonMenuItem)item;elsereturnnull;if(targetinstanceofActionListener)r.addActionListener((ActionListener)target);returnr;}publicvoidMapSize(intw,inth){setSize(w*24,h*27);if(this.checkcomputer)this.iscomputer=true;elsethis.iscomputer=false;mp.setModel(cm);mp.repaint();}publicbooleangetiscomputer(){returnthis.iscomputer;}publicvoidrestart(){intmodeChess=cm.getModeChess();if(modeChess<=3&&modeChess>=0){cm=newChessModel(modeChess);MapSize(cm.getWidth(),cm.getHeight());}}publicvoidactionPerformed(ActionEvente){Stringarg=e.getActionCommand();try{if(arg.equals("类型三"))UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");elseif(arg.equals("类型二"))UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");elseUIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");SwingUtilities.updateComponentTreeUI(this);}catch(Exceptionee){}if(arg.equals("标准棋盘")){this.width=14;this.height=14;cm=newChessModel(1);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("大号棋盘")){this.width=18;this.height=18;cm=newChessModel(2);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("超大号棋盘")){this.width=22;this.height=22;cm=newChessModel(3);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人机对弈")){this.checkcomputer=true;this.iscomputer=true;cm=newChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人人对战")){this.checkcomputer=false;this.iscomputer=false;cm=newChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("再来一局")){restart();}if(arg.equals("悔棋")){if(!checkcomputer){intresult=JOptionPane.showConfirmDialog(this,(FiveChessAppletDemo.isBlack==false?"白方悔棋,黑方是否同意?":"黑方悔棋,白方是否同意?"));if(result==0){JOptionPane.showMessageDialog(this,"双方同意悔棋决定,棋盘清空!请继续...");restart();}}}if(arg.equals("认输")){intresult=JOptionPane.showConfirmDialog(this,"是否认输?");if(result==0){JOptionPane.showMessageDialog(this,"游戏结束,"+(FiveChessAppletDemo.isBlack==true?"黑方认输,白方获胜!":"白方认输,黑方获胜!"));}}if(arg.equals("关于"))JOptionPane.showMessageDialog(null,"制作者:\n制作时间:2015年6月25日","课程设计制作介绍",JOptionPane.PLAIN_MESSAGE);if(arg.equals("退出"))System.exit(0);}}classChessModel{privateintwidth,height,modeChess;privateintx=0,y=0;privateint[][]arrMapShow;privatebooleanisOdd,isExist;publicChessModel(){}publicChessModel(intmodeChess){this.isOdd=true;if(modeChess==1){PanelInit(14,14,modeChess);}if(modeChess==2){PanelInit(18,18,modeChess);}if(modeChess==3){PanelInit(22,22,modeChess);}}privatevoidPanelInit(intwidth,intheight,intmodeChess){this.width=width;this.height=height;this.modeChess=modeChess;arrMapShow=newint[width+1][height+1];for(inti=0;i<=width;i++)for(intj=0;j<=height;j++)arrMapShow[i][j]=-1;}publicbooleangetisOdd(){returnthis.isOdd;}publicvoidsetisOdd(booleanisodd){if(isodd)this.isOdd=true;elsethis.isOdd=false;}publicbooleangetisExist(){returnthis.isExist;}publicintgetWidth(){returnthis.width;}publicintgetHeight(){returnthis.height;}publicintgetModeChess(){returnthis.modeChess;}publicint[][]getarrMapShow(){returnarrMapShow;}privatebooleanbadxy(intx,inty){if(x>=width+20||x<0)returntrue;returny>=height+20||y<0;}publicbooleanchessExist(inti,intj){if(this.arrMapShow[i][j]==1||this.arrMapShow[i][j]==2)returntrue;returnfalse;}publicvoidreadyplay(intx,inty){if(badxy(x,y))return;if(chessExist(x,y))return;this.arrMapShow[x][y]=3;}publicvoidplay(intx,inty){if(badxy(x,y))return;if(chessExist(x,y)){this.isExist=true;return;}elsethis.isExist=false;if(getisOdd()){setisOdd(false);this.arrMapShow[x][y]=1;}else{setisOdd(true);this.arrMapShow[x][y]=2;}}publicvoidcomputerDo(intwidth,intheight){intmax_black,max_white,max_temp,max=0;setisOdd(true);System.out.println("计算机走棋...");for(inti=0;i<=width;i++){for(intj=0;j<=height;j++){if(!chessExist(i,j)){max_white=checkMax(i,j,2);max_black=checkMax(i,j,1);max_temp=Math.max(max_white,max_black);if(max_temp>max){max=max_temp;this.x=i;this.y=j;}}}}setX(this.x);setY(this.y);this.arrMapShow[this.x][this.y]=2;}publicvoidsetX(intx){this.x=x;}publicvoidsetY(inty){this.y=y;}publicintgetX(){returnthis.x;}publicintgetY(){returnthis.y;}publicintcheckMax(intx,inty,intblack_or_white){intnum=0,max_num,max_temp=0;intx_temp=x,y_temp=y;intx_temp1=x_temp,y_temp1=y_temp;for(inti=1;i<5;i++){x_temp1+=1;if(x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}x_temp1=x_temp;for(inti=1;i<5;i++){x_temp1-=1;if(x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}if(num<5)max_temp=num;x_temp1=x_temp;y_temp1=y_temp;num=0;for(inti=1;i<5;i++){y_temp1-=1;if(y_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}y_temp1=y_temp;for(inti=1;i<5;i++){y_temp1+=1;if(y_temp1>this.height)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;x_temp1=x_temp;y_temp1=y_temp;num=0;for(inti=1;i<5;i++){x_temp1-=1;y_temp1-=1;if(y_temp1<0||x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}x_temp1=x_temp;y_temp1=y_temp;for(inti=1;i<5;i++){x_temp1+=1;y_temp1+=1;if(y_temp1>this.height||x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;x_temp1=x_temp;y_temp1=y_temp;num=0;for(inti=1;i<5;i++){x_temp1+=1;y_temp1-=1;if(y_temp1<0||x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}x_temp1=x_temp;y_temp1=y_temp;for(inti=1;i<5;i++){x_temp1-=1;y_temp1+=1;if(y_temp1>this.height||x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;max_num=max_temp;returnmax_num;}publicbooleanjudgeSuccess(intx,inty,booleanisodd){intnum=1;intarrvalue;intx_temp=x,y_temp=y;if(isodd)arrvalue=2;elsearrvalue=1;intx_temp1=x_temp,y_temp1=y_temp;for(inti=1;i<6;i++){x_temp1+=1;if(x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}x_temp1=x_temp;for(inti=1;i<6;i++){x_temp1-=1;if(x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}if(num==5)returntrue;x_temp1=x_temp;y_temp1=y_temp;num=1;for(inti=1;i<6;i++){y_temp1-=1;if(y_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}y_temp1=y_temp;for(inti=1;i<6;i++){y_temp1+=1;if(y_temp1>this.height)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}if(num==5)returntrue;x_temp1=x_temp;y_temp1=y_temp;num=1;for(inti=1;i<6;i++){x_temp1-=1;y_temp1-=1;if(y_temp1<0||x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}x_temp1=x_temp;y_temp1=y_temp;for(inti=1;i<6;i++){x_temp1+=1;y_temp1+=1;if(y_temp1>this.height||x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}if(num==5)returntrue;x_temp1=x_temp;y_temp1=y_temp;num=1;for(inti=1;i<6;i++){x_temp1+=1;y_temp1-=1;if(y_temp1<0||x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}x_temp1=x_temp;y_temp1=y_temp;for(inti=1;i<6;i++){x_temp1-=1;y_temp1+=1;if(y_temp1>this.height||x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}if(num==5)returntrue;returnfalse;}publicvoidshowSuccess(JPaneljp){JOptionPane.showMessageDialog(jp,"你赢了","结果",JOptionPane.INFORMATION_MESSAGE);}publicvoidshowDefeat(JPaneljp){JOptionPane.showMessageDialog(jp,"你输了","结果",JOptionPane.INFORMATION_MESSAGE);}}classChessWindowEventextendsWindowAdapter{publicvoidwindowClosing(WindowEvente){System.exit(0);}ChessWindowEvent(){}}publicclassFiveChessAppletDemo{publicstaticBooleanisBlack=true;publicstaticvoidmain(Stringargs[]){ChessFramecf=newChessFrame();cf.show();}}classMainPanelextendsJPanelimplementsMouseListener,MouseMotionListener{privateintwidth,height;privateChessModelcm;MainPanel(ChessModelmm){cm=mm;width=cm.getWidth();height=cm.getHeight();addMouseListener(this);}publicvoidsetModel(ChessModelmm){cm=mm;width=cm.getWidth();height=cm.getHeight();}publicvoidpaintComponent(Graphicsg){super.paintComponent(g);for(intj=0;j<=height;j++){for(inti=0;i<=width;i++){

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论