• 大小: 19.43 KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2024-10-21
  • 语言: Java
  • 标签: java  源码  

资源简介

java GUI版汉诺塔源码。
使用java swing .
可以自动演示搬运过程。

资源截图

代码片段和文件信息

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.Jframe;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;

public class AutoMoveDisc extends JDialog implements ActionListener {

int amountOfDisc = 3;
TowerPoint[] pointA pointB pointC;
char[] towerName;
Container con;
StringBuffer moveStep;
JTextArea showStep;

// Disc disc=null;

public void setAmountOfDisc(int amountOfDisc) {
this.amountOfDisc = amountOfDisc;
}

public void setPointA(TowerPoint[] pointA) {
this.pointA = pointA;
}

public void setPointB(TowerPoint[] pointB) {
this.pointB = pointB;
}

public void setPointC(TowerPoint[] pointC) {
this.pointC = pointC;
}

public void setTowerName(char[] towerName) {
if (towerName[0] == towerName[1] || towerName[1] == towerName[2]
|| towerName[0] == towerName[2]) {
this.towerName[0] = ‘A‘;
this.towerName[1] = ‘B‘;
this.towerName[2] = ‘C‘;
} else
this.towerName = towerName;
}

JButton bStart bContinue bStop bClose;
Timer timer;
int i = 0 number = 0;

public AutoMoveDisc(Container con) {
super();
this.con = con;
moveStep = new StringBuffer();
timer = new javax.swing.Timer(1000 this);
timer.setInitialDelay(10);
showStep = new JTextArea(10 15);
showStep.setEditable(false);

bStart = new JButton(“Start“);
bContinue = new JButton(“Continue“);
bClose = new JButton(“Close“);
bStop = new JButton(“Stop“);
bStart.addActionListener(this);
bContinue.addActionListener(this);
bClose.addActionListener(this);
bStop.addActionListener(this);

JPanel pSouth = new JPanel();
pSouth.setLayout(new GridLayout(1 4));
pSouth.add(bStart);
pSouth.add(bStop);
pSouth.add(bContinue);
pSouth.add(bClose);
this.add(pSouth BorderLayout.SOUTH);

this.add(new JScrollPane(showStep) BorderLayout.CENTER);

this.setDefaultCloseOperation(Jframe.DO_NOTHING_ON_CLOSE);
this.setModal(true);
this.settitle(“Auto move disc“);
towerName = new char[3];

this.addWindowListener(new WindowAdapter() {

@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
timer.stop();
setVisible(false);
}

});
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == timer) {
number++;
char cStart cEnd;
if (i <= moveStep.length() - 2) {
cStart = moveStep.charAt(i);
cEnd = moveStep.charAt(i + 1);
showStep.append(“(“ + number + “)from “ + cStart + “ to “
+ cEnd + “\n“);
this.autoMoveDisc(cStart cEnd);
}
i += 2;
if (i >= moveStep.length() - 1) {
timer.stop();

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        301  2010-03-01 12:00  Hannoi\.classpath

     文件        382  2010-03-01 12:00  Hannoi\.project

     文件         88  2010-03-01 12:01  Hannoi\.settings\org.eclipse.core.resources.prefs

     文件         83  2010-03-01 12:01  Hannoi\.settings\org.eclipse.core.runtime.prefs

     文件        629  2010-03-01 12:00  Hannoi\.settings\org.eclipse.jdt.core.prefs

     文件        765  2010-04-01 09:24  Hannoi\bin\AutoMoveDisc$1.class

     文件       5450  2010-04-01 09:24  Hannoi\bin\AutoMoveDisc.class

     文件        799  2010-04-01 09:24  Hannoi\bin\Disc.class

     文件       4051  2010-04-01 09:24  Hannoi\bin\HandleMouse.class

     文件       3496  2010-04-01 09:24  Hannoi\bin\HannoiWindow.class

     文件        388  2010-04-01 09:24  Hannoi\bin\Main.class

     文件        425  2010-04-01 09:24  Hannoi\bin\Tower$TwoPoints.class

     文件       4588  2010-04-01 09:24  Hannoi\bin\Tower.class

     文件       2091  2010-04-01 09:24  Hannoi\bin\TowerPoint.class

     文件       5334  2010-03-06 12:07  Hannoi\src\AutoMoveDisc.java

     文件        474  2010-03-01 12:24  Hannoi\src\Disc.java

     文件       4216  2010-03-04 22:55  Hannoi\src\HandleMouse.java

     文件       2885  2010-03-03 22:42  Hannoi\src\HannoiWindow.java

     文件        129  2010-03-01 12:03  Hannoi\src\Main.java

     文件       4904  2010-03-05 12:34  Hannoi\src\Tower.java

     文件       1150  2010-03-01 22:41  Hannoi\src\TowerPoint.java

     目录          0  2010-03-01 12:01  Hannoi\.settings

     目录          0  2010-04-01 09:24  Hannoi\bin

     目录          0  2010-03-03 22:07  Hannoi\src

     目录          0  2010-03-01 12:00  Hannoi

----------- ---------  ---------- -----  ----

                42628                    25


评论

共有 条评论