Java汉诺塔课程设计程序
---------------------------------------------A.java--------------------------------------------------
importjavax.swing.*;
importjava.awt.event.*;
public class A implements ItemListener
{
String level;
JTextField field;
JComboBox choice;
B workTogether;
public void setJComboBox(JComboBox box)
{
choice=box;
}
public void setWorkTogether(B b)
{
workTogether=b;
}
public void itemStateChanged(ItemEvent e)
{
String dengji=choice.getSelectedItem().toString();
workTogether.setLevel(dengji);
}
}
---------------------------------------------------B.java---------------------------------------------- importjavax.swing.*;
importjava.awt.event.*;
importjava.awt.*;
importjavax.swing.border.*;
importjava.awt.geom.*;
public class B extends JLayeredPane implements ActionListener,MouseListener,MouseMotionListener
{
int x,y,x0,y0,m,n,m0,n0; //K是判断要移到的那根柱子现有几个棋子的
JTextAreaone,two;
JButton b3,b2,b1;
String level;
String str1="1";
String str2="2";
String str3="3";
int A[]={1,2,3}; // 用来作为移动按钮的哨兵,
int B[]={0,0,0};
int C[]={0,0,0};
public void paint(Graphics g)
{
Graphics2D g_2d=(Graphics2D)g;
Line2D line_1=new Line2D.Double(100,0,100,480);
g_2d.setColor(Color.black);
g_2d.draw(line_1);
Line2D line_2=new Line2D.Double(400,0,400,480);
g_2d.setColor(Color.black);
g_2d.draw(line_2);
Line2D line_3=new Line2D.Double(700,0,700,480);
g_2d.setColor(Color.black);
g_2d.draw(line_3);
}
B()
{
b3=new JButton(str3);//
b3.setBounds(25,440,150,40); //
add(b3,JLayeredPane.DEFAULT_LAYER);//
b2=new JButton(str2);//
b2.setBounds(40,400,120,40);//
add(b2,JLayeredPane.DEFAULT_LAYER);//
b1=new JButton(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); //向三个按钮分别加入两个监视器}
public void setJTextAreaOne(JTextArea Area)
{
one=Area;
}
public void setJTextAreaTwo(JTextArea Area) //
{
two=Area;
}
/*public void setJTextArea(JTextArea a)
{
show=a;
}*/
public void setLevel(String s)
{
level=s;
}
public void actionPerformed(ActionEvent e) {
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(Exception E)
{
one.append("enter numbers,pls\n");
}
}
public void mousePressed(MouseEvent e)
{
JComponent comp=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();//获取鼠标的位置
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e) {System.out.println("mouseReleased");
int xx;
JComponent comp=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);
}
else if(e.getSource()==b2)
{
movebutton(2,xx);
}
else if(e.getSource()==b3)
{
movebutton(3,xx);
}
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e)
{System.out.println("mouseDragged");
Component comp=null;
if(e.getSource() instanceof Component)
{
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,int xx)
{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)
{
case 1: b1.setLocation(55,440);break;
case 2: b2.setLocation(40,440);break;
case 3: b3.setLocation(25,440);break;
}
if(m0>175&&m0<475)
{
B[a-1]=0;
}
else if(m0>475)
{
C[a-1]=0;
}
}