实训任务书-java 下载本文

漳州职业技术学院计算机工程系实训任务书

专业 课程 任务名称 学习形式 学习 目标 软件技术 班级 10软件2 任课教师 谢世煊 面向对象初级编程(java) 任务一、演示汉诺塔(windows) 课内 ( ) 课后 (√ ) 1、 运用递归算法 2、 熟悉汉诺塔算法 3、 java桌面应用程序基本控件的使用 1、 在NETBEANS中拖拉控件 2、 实现窗体的显示 3、 实现窗体控件的动态生成 过程:源代码 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class hanoiDemo extends JFrame implements ActionListener, TextListener, ChangeListener{ static Thread thread = new Thread(); static int n = 1; //初始化盘子个数 static int timect = 50; //演示的时间设置为中速 static int xh = 530; //原柱的的高度 static int yh = 530; //中间柱的的高度 static int zh = 530; //目的柱的的高度 static hanoiDemo frm = new hanoiDemo(); //创建窗口对象 static Label lab4 = new Label(); static JSlider slider = new JSlider(); //创建滑杆 static Label lab1 = new Label(\请输入盘子个数(1~20)\ static TextField txt1 = new TextField(2); static Label lab2 = new Label(\演示速度\ static Button btn1 = new Button(\演示\创建按钮 static Button btn2 = new Button(\退出\ static Button btn3 = new Button(\刷新\static Label lab[] = new Label[20]; //使用标签对象做盘子,最多20 个盘 public static void main(String[] args) { slider.setPaintLabels(true); 学习方式 小组( ) 个人独立(√ ) 实训过程 实施建议

slider.addChangeListener(frm); btn1.addActionListener(frm); //设置按钮的事件监听者为FRM btn2.addActionListener(frm); btn3.addActionListener(frm); txt1.addTextListener(frm); //设置文本的事件监听者为FRM frm.setResizable(false); //设置窗口不可改变大小 frm.setLayout(null); frm.setTitle(\汉诺塔问题的直观演示\ frm.setSize(1024, 738); lab1.setBounds(20, 560, 150, 15); txt1.setBounds(170, 560, 20, 15);//输入个数的窗口 lab2.setBounds(20, 580, 60, 15);//速度控制位置 slider.setBounds(90, 560, 300, 100); lab4.setBounds(90, 650, 180, 15); lab4.setText(\每移到一个盘的时间\毫秒\ btn1.setBounds(340, 560, 120, 30);//演示按钮 btn2.setBounds(600, 560, 120, 30); btn3.setBounds(340, 560, 120, 30); frm.add(txt1); //加载标签,按钮,文本框,单选框到窗口 frm.add(lab1); frm.add(lab2); frm.add(btn1); frm.add(btn2); frm.add(btn3); frm.add(lab4); frm.add(slider); frm.setVisible(true); //设置各组件对象是否可见 btn1.setVisible(false); btn3.setVisible(false); frm.addWindowListener(new WindowAdapter() //设置窗口关闭按钮是可用 { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void paint(Graphics g) //绘制塔柱的位置 { g.setColor(Color.magenta); //增加的颜色控制 g.drawLine(40, 550, 340, 550); g.setColor(Color.red); g.drawLine(160, 50, 160, 550); g.setColor(Color.green);

g.drawLine(360, 550, 660, 550); g.setColor(Color.cyan); g.drawLine(510, 50, 510, 550); g.setColor(Color.orange); g.drawLine(680, 550, 980, 550); g.setColor(Color.blue); g.drawLine(830, 50, 830, 550); } public void actionPerformed(ActionEvent e) //设置按钮事件 { Button btn = (Button) e.getSource(); if (btn == btn1) //btn1 为演示开始按钮 { for (int i = 0; i <= n - 1; i++) //建立盘子对象并加载到窗口 { lab[i] = new Label(); lab[i].setSize(15 * (i + 1), 25); lab[i].setLocation(160 - lab[i].getWidth() / 2, 530 - lab[i].getHeight() * (n - i)); lab[i].setBackground(Color.red); frm.add(lab[i]); } btn1.setVisible(false); for (double i = 1; i <= Math.pow(10, 6); i++); frm.hanoi(lab, n, 160, 510, 830); //汉诺塔演示的核心程序的调用 btn3.setVisible(true); //演示结束显示刷新按钮 txt1.setVisible(false); } if (btn == btn3) //刷新,回到初始状态 { btn3.setVisible(false); btn1.setVisible(true); for (int i = 0; i <= n - 1; i++) { frm.remove(lab[i]); } txt1.setVisible(true); xh = 530 - 25 * n; yh = 530; zh = 530; } if (btn == btn2) //退出 { System.exit(0); } }

public void textValueChanged(TextEvent e) //的到用户指定的盘子的个数 { TextField txt = (TextField) e.getSource(); if (txt == txt1) { try { n = Integer.parseInt(txt1.getText()); } //对输入的异常处理 catch (NumberFormatException f) { btn1.setVisible(false); return; } if (n > 0 && n <= 20) //对输入的异常处理 { xh = 530 - 25 * n; btn1.setVisible(true); } else { btn1.setVisible(false); } } } public void stateChanged(ChangeEvent e) { timect = slider.getValue(); lab4.setText(\每移到一个盘的时间\毫秒\} public void settimectr(int timectc) //移动的间隔时间 { for (double h = 1; h <= Math.pow(2, (double) timectc); h++); System.out.println(timectc); } public void setactcolor(Label labl[], int ncl) //盘子移动时的颜色,绿色 { labl[ncl - 1].setBackground(Color.blue); } public void setcolorbak(Label labl[], int ncl) //盘子静止时的颜色,红色 { labl[ncl - 1].setBackground(Color.green); }//汉诺塔演示的核心程序,递归实现 public void hanoi(Label labc[], int nc, int xc, int yc, int zc) { try { if (nc == 1) { setactcolor(labc, 1);