问题描述
下面的程序通过修改WindowBox类的num和gap修改盘子个数和时间间隔可以正确运行,但是为什么在菜单的设置选项里,设置之后,画布就不再变化了,而是一直保持原来运行的结果呢?令外,运行完以后,最大化最小化,窗口,盘子的大小为什么会变化呢?急啊代码如下:importjavax.swing.*;importjava.awt.*;importjavax.swing.border.*;importjava.awt.event.*;publicclasshanoi{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubWindowBoxwb=newWindowBox();}}//获取移动结果classHanoiResult{intarray[][];//移动步数intstate[][];//每次移动各个柱子的盘子intcount;//移动步数intnum;//盘子个数HanoiResult(){array=newint[200][3];state=newint[2000][3];}//获取移动的结果voidgetResult(){init_array();hanoi(num,1,2,3);/*for(inti=0;i<=count;i++){for(intj=0;j<3;j++)System.out.print(array[i][j]);System.out.println();}*//*for(inti=0;i<num*(count+1);i++){for(intj=0;j<3;j++)System.out.print(state[i][j]);System.out.println();if((i+1)%num==0)System.out.println("*******");}*//*for(inti=0;i<num;i++)System.out.println(state[i][0]+""+state[i][1]+""+state[i][2]);*/}//初始化数组voidinit_array(){array[0][0]=num;array[1][0]=num;for(inti=0;i<1000;i++)for(intj=0;j<3;j++)state[i][j]=0;for(inti=0;i<num;i++)state[i][0]=i+1;for(inti=num;i<2*num;i++)state[i][0]=i-num+1;}//获取n号柱子上的最大盘子intgetbiggest(intm,intn){inti,flag;for(i=0;i<num&&state[m+i][n]!=0;i++);flag=state[m+i-1][n];state[m+i-1][n]=0;returnflag;};//将j号盘子放到第n号柱子上voidins(intm,intn,intj){inti;for(i=0;i<num&&state[m+i][n]!=0;i++);state[m+i][n]=j;};voidmov(inta,intb){array[count+1][--a]--;array[count+1][--b]++;for(inti=0;i<3;i++)array[count+2][i]=array[count+1][i];ins((count+1)*num,b,getbiggest((count+1)*num,a));for(inti=0;i<num;i++)for(intj=0;j<3;j++)state[(count+2)*num+i][j]=state[(count+1)*num+i][j];count++;}voidhanoi(intn,inta,intb,intc){if(n==1)mov(a,c);else{hanoi(n-1,a,c,b);mov(a,c);hanoi(n-1,b,a,c);}}voidsetnum(intnum){this.num=num;}}//窗口类classWindowBoxextendsFrameimplementsActionListener{HanoiResulthr;//hanoi对象doublet;//计时intgap;//时间间隔Boxhb,vb;mycanvasca1,ca2,ca3;//画布对戏那个MenuBarmenubar;Menumenu1,menu2;MenuItemitem1,item3,item4;Labelbottom;NumDialogdialog1;intnum;WindowBox(){num=3;gap=500;t=0;ca1=newmycanvas();ca2=newmycanvas();ca3=newmycanvas();ca1.setBackground(Color.orange);ca2.setBackground(Color.cyan);ca3.setBackground(Color.green);dialog1=newNumDialog(this,"设定运行参数",true);menubar=newMenuBar();menu1=newMenu("文件");menu2=newMenu("选项");item1=newMenuItem("设定参数…");item3=newMenuItem("退出");item4=newMenuItem("关于");menu1.add(item1);menu1.add(item3);menu2.add(item4);menubar.add(menu1);menubar.add(menu2);setMenuBar(menubar);setTitle("汉诺塔问题演示");setBackground(Color.pink);hr=newHanoiResult();bottom=newLabel("已运行时间0已移动步数0(时间间隔尚未设定)");hb=Box.createHorizontalBox();vb=Box.createVerticalBox();hb.add(Box.createHorizontalStrut(8));hb.add(ca1);hb.add(Box.createHorizontalStrut(4));hb.add(ca2);hb.add(Box.createHorizontalStrut(4));hb.add(ca3);hb.add(Box.createHorizontalStrut(8));vb.add(hb);vb.add(bottom);setLayout(newFlowLayout());add(vb);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});setBounds(200,20,624,620);setVisible(true);item1.addActionListener(this);item3.addActionListener(this);item4.addActionListener(this);run();}//运行函数publicvoidrun(){hr.setnum(num);ca1.setnum(num);ca2.setnum(num);ca3.setnum(num);hr.getResult();try{item1.setEnabled(false);}catch(Exceptione){};for(inti=0;i<=hr.count;i++){redraw(ca1,ca2,ca3,hr.array[i][0],hr.array[i][1],hr.array[i][2],i);}item1.setEnabled(true);}//菜单监听@OverridepublicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubif(e.getSource()==item1){dialog1.setVisible(true);hr=newHanoiResult();t=0;this.num=dialog1.getnum();this.gap=(int)(dialog1.gettime()*1000);//System.out.println("num"+num);//System.out.println("time"+gap);run();}elseif(e.getSource()==item3)System.exit(0);elseif(e.getSource()==item4){hr=newHanoiResult();t=0;this.num=4;this.gap=500;run();}}//重画函数publicvoidredraw(mycanvasca1,mycanvasca2,mycanvasca3,inta,intb,intc,intcs){inti;bottom.setText("已运行时间"+t+"秒已移动步数"+(cs)+"(时间间隔为0.5s)");for(i=0;i<num;i++){ca1.state[i]=hr.state[cs*num+i][0];}//重画画布1ca1.draw(a);for(i=0;i<num;i++){ca2.state[i]=hr.state[cs*num+i][1];}//重画画布2ca2.draw(b);for(i=0;i<num;i++){ca3.state[i]=hr.state[cs*num+i][2];}//重画画布3ca3.draw(c);t=t+gap/1000.0;//延时gaptry{Thread.sleep(gap);}catch(InterruptedExceptione){}}}classmycanvasextendsCanvas{intnum,x,y,width;intstate[];voidsetnum(intnum){this.num=num;}voiddraw(intnum){this.num=num;this.repaint();}mycanvas(){state=newint[10];setBounds(10,10,200,550);}//每多一个x+8,y-60,width-16;底层(30,490,140,50,20,20)publicvoidpaint(Graphicsg){for(inti=0;i<num;i++)state[i]--;inty=490;for(inti=0;i<num;i++){g.drawRoundRect(30+8*state[i],y,140-16*state[i],50,20,20);y-=60;}}}//对话框类classNumDialogextendsDialogimplementsActionListener{Stringnum_str;Stringtime_str;intnum;doubletime;Buttonconfirm,cancel;TextFieldnum_input;TextFieldtime_input;Labeltip1,tip2,num_label,time_label;Boxhr1,hr2,hr3,hv;NumDialog(Framef,Strings,booleanb){super(f,s,b);hr1=Box.createHorizontalBox();hr2=Box.createHorizontalBox();hr3=Box.createHorizontalBox();tip1=newLabel("必须输入1-8的整数");tip2=newLabel("为获得最好视觉效果时间最好不要超过1s");num_label=newLabel("盘子个数");time_label=newLabel("时间间隔");hv=Box.createVerticalBox();num_input=newTextField();time_input=newTextField();confirm=newButton("运行");cancel=newButton("取消");confirm.addActionListener(this);cancel.addActionListener(this);setLayout(newFlowLayout());hr1.add(num_label);hr1.add(Box.createHorizontalStrut(5));hr1.add(num_input);hr2.add(time_label);hr2.add(Box.createHorizontalStrut(5));hr2.add(time_input);hr3.add(confirm);hr3.add(Box.createHorizontalStrut(10));hr3.add(cancel);hv.add(tip1);hv.add(tip2);hv.add(Box.createVerticalStrut(20));hv.add(hr1);hv.add(Box.createVerticalStrut(10));hv.add(hr2);hv.add(Box.createVerticalStrut(15));hv.add(hr3);add(hv);setBounds(400,100,400,300);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){setVisible(false);}});}publicintgetnum(){returnnum;}publicdoublegettime(){returntime;}@OverridepublicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubif(e.getSource()==confirm){num_str=num_input.getText();time_str=time_input.getText();try{num=Integer.valueOf(num_str);time=Float.valueOf(time_str);}catch(NumberFormatExceptionexce){tip1.setText("输入有误:必须输入1-8的整数!");}setVisible(false);}elseif(e.getSource()==cancel){setVisible(false);}}}
解决方案
解决方案二:
学会用调试来解决问题吧,学会自己分析和把问题表达清楚吧,总贴一大段代码上来,头都痛了。
解决方案三:
不是啊,我分析了好久,没弄出个所以然来,所以只有求助大虾了,另外这个程序只有这一个类,只要建一个hanoi类就可运行了,hanoi.java的代码就在上面,希望大家帮帮我……