汉诺塔问题——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,name;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);//桌子面板大小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.setBackground(Color.red);rightLeg.setBounds(115,30,30,50);name.setBounds(70,100,60,30);for(inti=0;imaxsize;i++)//记录型数组,记录该桌子上放的是哪些盘子,数组元素值为盘子下标。{record[i]=-1;}}publicvoidSetrecord(inty)//修改记录数组{record[num-1]=y;//记录最上面的盘子的序号}publicvoidDeleoneRecord()//当移走某个盘子时,置相应的数组值为-1{record[num-1]=-1;}publicvoidSetinitialxy(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;//移动速度intdefaultnum=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;kmaxsize;k++){plate[k]=newLabel();}a=newDesk(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,350);c.setLocation(580,350);showAnswer.setBounds(300,50,200,25);showAnswer.setEditable(false);this.proplate(defaultnum);//初始化时产生的默认数量的盘子this.setSize(850,650);this.setLayout(null);a.Setinitialxy(80);b.Setinitialxy(340);c.Setinitialxy(600);a.Settopxy();b.Settopxy();c.Settopxy();changnum=false;validate();}publicvoidChangeDefault(intx){this.defaultnum=x;changnum=true;}publicvoidRemoveAllPlate(){for(inti=0;imaxsize;i++){this.remove(plate[i]);}}publicvoidproplate(intsum){showAnswer.setText(结果显示区);steps=0;if(changnum==true){this.RemoveAllPlate();a.clear();b.clear();c.clear();a.Settopxy();b.Settopxy();c.Settopxy();}for(intsu=0;susum;su++){plate[su].setText(+su);plate[su].setBackground(Color.black);plate[su].setForeground(Color.red);plate[su].setVisible(true);this.add(plate[su]);a.addone();//A座盘子数加1a.Setrecord(su);//修改桌子A的记录数组plate[su].setBounds(initialx+su*5,initialy-su*plateHeight,bigestsize-10*su,plateHeight);}}publicvoidhanoi(intn,Deskone,Desktwo,Deskthree){if(n==1)move(one,three);else{hanoi(n-1,one,three,two);move(one,three);hanoi(n-1,two,one,three);}}publicvoidmove(Deskone,Desktwo){//首先实现移动最顶端的盘子//要做的事情包括修改相应的桌子上的盘子数,及下个盘子要放的位置,还有更新记录数组。intno;//正在搬运的盘子的序号doublework;//取得盘子上的序号//下面的工作是找到要移动的盘子的序号intmovewhich;movewhich=one.record[one.num-1];//定制移动路线//先向上运动到制高点intx;inty;x=plate[movewhich].getX();y=plate[movewhich].getY();two.Settopx(movewhich);intstepleftright=(two.topx-x)/Math.abs(two.topx-x);//while(y!=200){y=y-1;plate[movewhich].setLocation(x,y);try{Thread.sleep(moveSpeed);}catch(InterruptedExceptione){//TODO自动生成catch块e.printStackTrace();}}//水平方向运动while(x!=two.topx){x=x+stepleftright;plate[movewhich].setLocation(x,y);//this.repaint();try{Thread.sleep(moveSpeed);}catch(InterruptedExceptione){//TODO自动生成catch块e.printStackTrace();}}while(y!=two.topy){y=y+1;plate[movewhich].setLocation(x,y);try{Thread.sleep(moveSpeed);}catch(InterruptedExceptione){//TODO自动生成catch块e.printStackTrace();}}one.DeleoneRecord();//表示该盘子移走了work=Double.parseDouble(plate[movewhich].getText());no=(int)work;System.out.print(no);one.subone();//盘子数减少1two.addone();//盘子数加1two.Setrecord(no);//桌子2上的记录数组添加1个元素one.Settopxy();//修改1座上下个盘子的位置two.Settopxy();//修改2座......//设计函数实现下面的语句Stringstring=当前搬运动作为:+one.name.getText()+---+two.name.getText();showAnswer.setText(string);steps++;}}classMainFrameextendsFrame{MenuBarmenubar;Menudisplay,help,speed,num;MenuItemstart,quit,paush,declare,introdue,speed1,speed2,speed3,num1,num2,num3,num4,num5;MenuActionRereplay=newMenuActionRe();MainPanelmainpanel=newMainPanel();MainFrame(){}MainFrame(Strings){super(s);setTitle(s);menubar=newMenuBar();display=newMenu(演示);help=newMenu(帮助);start=newMenuItem(开始);speed=newMenu(选择速度);num=newMenu(盘子数量);quit=newMenuItem(退出);quit.addActionListener(replay);paush=newMenuItem(暂停);declare=newMenuItem(说明);introdue=newMenuItem(介绍);speed1=newMenuItem(快速完成);speed2=newMenuItem(适中速度);speed3=newMenuItem(较慢实现);num1=newMenuItem(3);num1.addActionListener(replay);num2=newMenuItem(5);num2.addActionListener(replay);num3=newMenuItem(7);num3.addActionListe