1、JAVA图形界面程序汉诺塔演示程序代码汉诺塔问题JAVA实现import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JDialog;import javax.swing.JLabel;import javax.swing.JPanel;class Desk extends Panel Label t
2、opPanel,leftLeg,rightLeg,name; int num; / 桌子上现有盘子的数量 public int topy=340,topx=80; /第一个个盘子在哪里放下 int maxsize=12; int initialx=0,initialy=340; int record=new int12; Desk(String s) /构造函数 name=new Label(); name.setText(s); /桌子名称 topPanel=new Label(); topPanel.setBackground(Color.red); leftLeg=new Label()
3、; rightLeg=new Label(); this.setSize(180, 160); /桌子面板大小 this.setLayout(null); this.add(topPanel); this.add(leftLeg); this.add(rightLeg); this.add(name); this.setFont(new Font(宋体,Font.CENTER_BASELINE, 16); this.setForeground(Color.blue); topPanel.setBounds(10, 0,160, 30); leftLeg.setBackground(Color.
4、red); leftLeg.setBounds(35,30,30,50); rightLeg.setBackground(Color.red); rightLeg.setBounds(115,30,30, 50); name.setBounds(70, 100,60, 30); for (int i=0;imaxsize;i+) /记录型数组,记录该桌子上放的是哪些盘子,数组元素值为盘子下标。 recordi=-1; public void Setrecord(int y) /修改记录数组 recordnum-1=y; /记录最上面的盘子的序号 public void DeleoneRecor
5、d() /当移走某个盘子时,置相应的数组值为-1 recordnum-1=-1; public void Setinitialxy(int x ) /修改下一个盘子的盘放位置 initialx=x; public void Settopxy() topx=initialx+num*5; topy=initialy-num*10; public void Settopx(int x) topx=initialx+x*5; public void clear() num=0; public void addone() this.num+; public void subone() this.num
6、-; class MainPanel extends JPanel / Desk a,b,c; int speed; /移动速度 int defaultnum=3; / 初始盘子数 boolean changnum; int maxsize=12; Label plate=new Labelmaxsize; int bigestsize=140; int plateHeight=10; int initialx=80; int initialy=340; int moveSpeed=0; /默认为快速完成 int steps=0; /完成步数 public TextField showAnsw
7、er=new TextField(结果显示区); MainPanel() for(int k=0;kmaxsize;k+) platek=new Label(); a=new Desk(A 座); b=new Desk(B 座); c=new Desk(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.
8、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.Set
9、topxy(); changnum=false; validate(); public void ChangeDefault(int x) this.defaultnum=x; changnum=true; public void RemoveAllPlate() for(int i=0;imaxsize;i+) this.remove(platei); public void proplate(int sum) showAnswer.setText(结果显示区); steps=0; if(changnum=true) this.RemoveAllPlate(); a.clear(); b.c
10、lear(); c.clear(); a.Settopxy(); b.Settopxy(); c.Settopxy(); for(int su=0;su+two.name.getText(); showAnswer.setText(string); steps+; class MainFrame extends Frame MenuBar menubar; Menu display,help,speed,num; MenuItem start,quit,paush,declare,introdue,speed1,speed2,speed3,num1,num2,num3,num4,num5; M
11、enuActionRe replay=new MenuActionRe(); MainPanel mainpanel=new MainPanel(); MainFrame() MainFrame(String s) super(s); setTitle(s); menubar=new MenuBar(); display=new Menu(演示); help=new Menu(帮助); start=new MenuItem(开始); speed=new Menu(选择速度); num=new Menu(盘子数量); quit=new MenuItem(退出); quit.addActionLi
12、stener( replay); paush=new MenuItem(暂停); declare=new MenuItem(说明); introdue=new MenuItem(介绍); speed1=new MenuItem(快速完成); speed2=new MenuItem(适中速度); speed3=new MenuItem(较慢实现); num1=new MenuItem(3); num1.addActionListener(replay); num2=new MenuItem(5); num2.addActionListener(replay); num3=new MenuItem
13、(7); num3.addActionListener(replay); num4=new MenuItem(9); num4.addActionListener(replay); num5=new MenuItem(12); num5.addActionListener(replay); display.add(start); start.addActionListener(replay); display.add(speed); display.add(num); display.add(quit); speed.add(speed1); speed1.addActionListener(
14、replay); speed.add(speed2); speed2.addActionListener(replay); speed.add(speed3); speed3.addActionListener(replay); num.add(num1); num.add(num2); num.add(num3); num.add(num4); num.add(num5); help.add(declare); declare.addActionListener(replay); help.add(introdue); introdue.addActionListener(replay);
15、menubar.add(display); menubar.add(help); menubar.setFont(new Font(宋体,Font.BOLD, 14); setMenuBar(menubar); this.add(mainpanel,BorderLayout.CENTER); setVisible(true); setBounds(200,50,850,650); class MenuActionRe implements ActionListener public void actionPerformed(ActionEvent event) / TODO 自动生成方法存根
16、if(event.getSource()=quit) System.exit(0); /退出 else if(event.getSource()=start) if(mainpanel.a.num=0) /说明已经搬完了,是要重新开始啦 mainpanel.proplate(mainpanel.defaultnum); mainpanel.c.clear(); mainpanel.a.Settopxy(); mainpanel.b.Settopxy(); mainpanel.c.Settopxy(); mainpanel.hanoi(mainpanel.defaultnum, mainpane
17、l.a,mainpanel.b, mainpanel.c); mainpanel.showAnswer.setText(搬运结束,搬运次数为:+mainpanel.steps); else if(event.getSource()=declare) Help1 dialog=new Help1(); dialog.setVisible(true); else if(event.getSource()=introdue) Help2 dialog=new Help2(); dialog.setVisible(true); else if(event.getSource()=num1) mainp
18、anel.ChangeDefault(3); mainpanel.proplate(mainpanel.defaultnum); else if(event.getSource()=num2) mainpanel.ChangeDefault(5); mainpanel.proplate(mainpanel.defaultnum); else if(event.getSource()=num3) mainpanel.ChangeDefault(7); mainpanel.proplate(mainpanel.defaultnum); else if(event.getSource()=num4)
19、 mainpanel.ChangeDefault(9); mainpanel.proplate(mainpanel.defaultnum); else if(event.getSource()=num5) mainpanel.ChangeDefault(12); mainpanel.proplate(mainpanel.defaultnum); else if(event.getSource()=speed1) /快速完成 mainpanel.moveSpeed=0; else if(event.getSource()=speed2) /适中速度 mainpanel.moveSpeed=2;
20、else if(event.getSource()=speed3) /较慢完成 mainpanel.moveSpeed=6; class Help1 extends JDialog JPanel panel1 = new JPanel(); JLabel jLabel1 = new JLabel( 作者: 徐小龙(中国石油大学) 2010年11月20日); JLabel jLabel2 = new JLabel(); GridLayout gridLayout1 = new GridLayout(); Help1() super(new MainFrame(),程序说明,false); set
21、Bounds(400,150,360,260); setVisible(true); jLabel2.setText(使用说明: 通过演示菜单来控制程序的运行,可以选择不同的移动速度,也可以定制盘子的数量。); panel1.add(jLabel1,BorderLayout.NORTH); panel1.add(jLabel2,BorderLayout.CENTER); setResizable(false); getContentPane().add(panel1); class Help2 extends JDialog JPanel panel1 = new JPanel(); JLab
22、el jLabel2 = new JLabel(); GridLayout gridLayout1 = new GridLayout(); Help2() super(new MainFrame(),汉诺塔问题介绍,false); setBounds(400,150,360,260); setVisible(true); jLabel2.setText(汉诺塔问题: 有若干个大小各不相同的盘子,有三个桌子A、B、C,开始时,盘子全部位于A桌上,现要将盘子从 + A桌上移动到C桌上,过程中要保证大盘子不能放到小盘子上面,且每次只能移动一个盘子。); panel1.add(jLabel2,BorderLayout.CENTER); getContentPane().add(panel1); public class HanoiDisply public stat
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1