欢迎来到天天文库
浏览记录
ID:60778710
大小:38.50 KB
页数:11页
时间:2020-12-18
《JAVA图形界面程序——汉诺塔演示程序代码.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、汉诺塔问题——JAVA实现importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjavax.swing.JDialog;importjavax.swing.JLabel;importjavax.swing.JPanel;classDeskextendsPanel{LabeltopPanel,leftLeg,rightLeg,n
2、ame;intnum;//桌子上现有盘子的数量publicinttopy=340,topx=80;//第一个个盘子在哪里放下intmaxsize=12;intinitialx=0,initialy=340;intrecord[]=newint[12];Desk(Strings)//构造函数{name=newLabel();name.setText(s);//桌子名称topPanel=newLabel();topPanel.setBackground(Color.red);leftLeg=newLabel();rightLeg=newLabel();this.setSize(180,160
3、);//桌子面板大小this.setLayout(null);this.add(topPanel);this.add(leftLeg);this.add(rightLeg);this.add(name);this.setFont(newFont("宋体",Font.CENTER_BASELINE,16));this.setForeground(Color.blue);topPanel.setBounds(10,0,160,30);leftLeg.setBackground(Color.red);leftLeg.setBounds(35,30,30,50);rightLeg.setBack
4、ground(Color.red);rightLeg.setBounds(115,30,30,50);name.setBounds(70,100,60,30);for(inti=0;i5、nitialxy(intx)//修改下一个盘子的盘放位置{initialx=x;}publicvoidSettopxy(){topx=initialx+num*5;topy=initialy-num*10;}publicvoidSettopx(intx){topx=initialx+x*5;}publicvoidclear(){num=0;}publicvoidaddone(){this.num++;}publicvoidsubone(){this.num--;}}classMainPanelextendsJPanel{//Deska,b,c;intspeed;//移动速度intdefa6、ultnum=3;//初始盘子数booleanchangnum;intmaxsize=12;Labelplate[]=newLabel[maxsize];intbigestsize=140;intplateHeight=10;intinitialx=80;intinitialy=340;intmoveSpeed=0;//默认为快速完成intsteps=0;//完成步数publicTextFieldshowAnswer=newTextField("结果显示区");MainPanel(){for(intk=0;k7、Desk("A座");b=newDesk("B座");c=newDesk("C座");this.setLayout(null);this.setSize(700,600);this.setBackground(Color.orange);this.add(a);this.add(b);this.add(c);this.add(showAnswer);a.setLocation(60,350);b.setLocation(320,35
5、nitialxy(intx)//修改下一个盘子的盘放位置{initialx=x;}publicvoidSettopxy(){topx=initialx+num*5;topy=initialy-num*10;}publicvoidSettopx(intx){topx=initialx+x*5;}publicvoidclear(){num=0;}publicvoidaddone(){this.num++;}publicvoidsubone(){this.num--;}}classMainPanelextendsJPanel{//Deska,b,c;intspeed;//移动速度intdefa
6、ultnum=3;//初始盘子数booleanchangnum;intmaxsize=12;Labelplate[]=newLabel[maxsize];intbigestsize=140;intplateHeight=10;intinitialx=80;intinitialy=340;intmoveSpeed=0;//默认为快速完成intsteps=0;//完成步数publicTextFieldshowAnswer=newTextField("结果显示区");MainPanel(){for(intk=0;k7、Desk("A座");b=newDesk("B座");c=newDesk("C座");this.setLayout(null);this.setSize(700,600);this.setBackground(Color.orange);this.add(a);this.add(b);this.add(c);this.add(showAnswer);a.setLocation(60,350);b.setLocation(320,35
7、Desk("A座");b=newDesk("B座");c=newDesk("C座");this.setLayout(null);this.setSize(700,600);this.setBackground(Color.orange);this.add(a);this.add(b);this.add(c);this.add(showAnswer);a.setLocation(60,350);b.setLocation(320,35
此文档下载收益归作者所有