Java汉诺塔课程设计程序.docx
- 文档编号:10339514
- 上传时间:2023-02-10
- 格式:DOCX
- 页数:32
- 大小:20.76KB
Java汉诺塔课程设计程序.docx
《Java汉诺塔课程设计程序.docx》由会员分享,可在线阅读,更多相关《Java汉诺塔课程设计程序.docx(32页珍藏版)》请在冰豆网上搜索。
Java汉诺塔课程设计程序
---------------------------------------------A.java--------------------------------------------------
importjavax.swing.*;
importjava.awt.event.*;
publicclassAimplementsItemListener
{
Stringlevel;
JTextFieldfield;
JComboBoxchoice;
BworkTogether;
publicvoidsetJComboBox(JComboBoxbox)
{
choice=box;
}
publicvoidsetWorkTogether(Bb)
{
workTogether=b;
}
publicvoiditemStateChanged(ItemEvente)
{
Stringdengji=choice.getSelectedItem().toString();
workTogether.setLevel(dengji);
}
}
---------------------------------------------------B.java----------------------------------------------
importjavax.swing.*;
importjava.awt.event.*;
importjava.awt.*;
importjavax.swing.border.*;
importjava.awt.geom.*;
publicclassBextendsJLayeredPaneimplementsActionListener,MouseListener,MouseMotionListener
{
intx,y,x0,y0,m,n,m0,n0;//K是判断要移到的那根柱子现有几个棋子的
JTextAreaone,two;
JButtonb3,b2,b1;
Stringlevel;
Stringstr1="1";
Stringstr2="2";
Stringstr3="3";
intA[]={1,2,3};//用来作为移动按钮的哨兵,
intB[]={0,0,0};
intC[]={0,0,0};
publicvoidpaint(Graphicsg)
{
Graphics2Dg_2d=(Graphics2D)g;
Line2Dline_1=newLine2D.Double(100,0,100,480);
g_2d.setColor(Color.black);
g_2d.draw(line_1);
Line2Dline_2=newLine2D.Double(400,0,400,480);
g_2d.setColor(Color.black);
g_2d.draw(line_2);
Line2Dline_3=newLine2D.Double(700,0,700,480);
g_2d.setColor(Color.black);
g_2d.draw(line_3);
}
B()
{
b3=newJButton(str3);//
b3.setBounds(25,440,150,40);//
add(b3,JLayeredPane.DEFAULT_LAYER);//
b2=newJButton(str2);//
b2.setBounds(40,400,120,40);//
add(b2,JLayeredPane.DEFAULT_LAYER);//
b1=newJButton(str1);//
b1.setBounds(55,360,90,40);//
add(b1,JLayeredPane.DEFAULT_LAYER);//
b3.addMouseListener(this);
b3.addMouseMotionListener(this);
b2.addMouseListener(this);
b2.addMouseMotionListener(this);
b1.addMouseListener(this);
b1.addMouseMotionListener(this);//向三个按钮分别加入两个监视器
}
publicvoidsetJTextAreaOne(JTextAreaArea)
{
one=Area;
}
publicvoidsetJTextAreaTwo(JTextAreaArea)//
{
two=Area;
}
/*publicvoidsetJTextArea(JTextAreaa)
{
show=a;
}*/
publicvoidsetLevel(Strings)
{
level=s;
}
publicvoidactionPerformed(ActionEvente)
{
try
{
if(level.equals("叁"))
{
two.setText(null);
two.append("7");
}
if(level.equals("肆"))
{
two.setText(null);
two.append("15");
}
if(level.equals("伍"))
{
two.setText(null);
two.append("31");
}
if(level.equals("陆"))
{
two.setText(null);
two.append("63");
}
}
catch(ExceptionE)
{
one.append("enternumbers,pls\n");
}
}
publicvoidmousePressed(MouseEvente)
{
JComponentcomp=null;
comp=(JComponent)e.getSource();
setLayer(comp,JLayeredPane.DRAG_LAYER);
m=comp.getBounds().x;
n=comp.getBounds().y;
m0=m;n0=n;
x0=e.getX();
y0=e.getY();//获取鼠标的位置
}
publicvoidmouseClicked(MouseEvente)
{
}
publicvoidmouseEntered(MouseEvente)
{
}
publicvoidmouseExited(MouseEvente)
{
}
publicvoidmouseReleased(MouseEvente)
{System.out.println("mouseReleased");
intxx;
JComponentcomp=null;
comp=(JComponent)e.getSource();
xx=comp.getBounds().x;
//yy=comp.getBounds().y;
if(e.getSource()==b1)
{System.out.println("movebutton(1,xx)");
movebutton(1,xx);
}
elseif(e.getSource()==b2)
{
movebutton(2,xx);
}
elseif(e.getSource()==b3)
{
movebutton(3,xx);
}
}
publicvoidmouseMoved(MouseEvente)
{
}
publicvoidmouseDragged(MouseEvente)
{System.out.println("mouseDragged");
Componentcomp=null;
if(e.getSource()instanceofComponent)
{
comp=(Component)e.getSource();
m=comp.getBounds().x;
n=comp.getBounds().y;//获得当前组件左上点坐标?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
x=e.getX();
y=e.getY();
m+=x;
n+=y;
comp.setLocation(m-x0,n-y0);
}
}
voidmovebutton(inta,intxx)
{System.out.println(""+xx);
if(xx<175)//如果落在A塔
{System.out.println(""+A[2]+A[1]+A[0]);
if(A[2]==0)//如果A塔没有盘子
{//放在A塔
A[2]=a;
switch(a)
{
case1:
b1.setLocation(55,440);break;
case2:
b2.setLocation(40,440);break;
case3:
b3.setLocation(25,440);break;
}
if(m0>175&&m0<475)
{
B[a-1]=0;
}
elseif(m0>475)
{
C[a-1]=0;
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 汉诺塔 课程设计 程序
