//引入包
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.JOptionPane;
public class Tupianjiemian extends JPanel 
{
//声明变量
JButton[][] bu;
int flag;
public Tupianjiemian()
{
//布局设置,并设置i是竖坐标,j是横坐标
super(new GridLayout(3,3));
bu=new JButton[3][3];
for(int i=0,cnt=1;i<3;i++)
{ for(int j=0;j<3;j++,cnt++)
{
if((i==2) && (j==2))
bu[2][2]=new JButton("");
else
{
bu[i][j]=new JButton(cnt+"");
bu[i][j].setActionCommand("bu"+i+j);
bu[i][j].addKeyListener((new MoveListener()));
bu[i][j].setFont(new Font("Arial",Font.BOLD,15));
    add(bu[i][j]);
}
}
}

}
public class MoveListener extends KeyAdapter
{
public void keypressed(KeyEvent e)
    {
    for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
if(e.getSource().equals(bu[i][j]))
{
if(j+1<3&&bu[i][j+1].getLabel()=="")
{
bu[i][j+1].setLabel(bu[i][j].getLabel());
bu[i][j].setLabel("");
}
if(i+1<3&&bu[i+1][j].getLabel()=="")
{
bu[i+1][j].setLabel(bu[i][j].getLabel());
bu[i][j].setLabel("");
}
if(j-1>-1&&bu[i][j-1].getLabel()=="")
{
bu[i][j-1].setLabel(bu[i][j].getLabel());
bu[i][j].setLabel("");
}
if(i-1>-1&&bu[i-1][j].getLabel()=="")
{
bu[i-1][j].setLabel(bu[i][j].getLabel());
bu[i][j].setLabel("");
}
}
}
if(bu[0][0].getLabel()=="")
  {
flag=0;
for(int i=2,cnt=15;i>=0;i--)
{
for(int j=2;j>=0;j--,cnt--)
{
     if(cnt!=1)
if(e.getSource().equals(bu[Math.abs(i-2)][Math.abs(j-2)]))
flag++;     }
}
      }
    }
     }
}
这代码检验,运行都可以··都没有错误··为什么不能移动呢??麻烦个位高手指教··

解决方案 »

  1.   

    1,
                               bu[i][j].setActionCommand("bu" + i + j);
                            //bu[i][j].addKeyListener((new MoveListener()));
                            bu[i][j].addActionListener((new MoveListener()));2,        public class MoveListener implements ActionListener //extends KeyAdapter
            {            public void actionPerformed(ActionEvent e)
                //public void keypressed(KeyEvent e)
                {
      

  2.   

    另外,
    3,
    public Tupianjiemian()
            {
                //  布局设置,并设置i是竖坐标,j是横坐标 
                super(new GridLayout(3, 3));
                bu = new JButton[3][3];
                for (int i = 0, cnt = 1; i < 3; i++)
                {
                    
                    for (int j = 0; j < 3; j++, cnt++)
                    {
                        if ((i == 2) && (j == 2))
                            bu[2][2] = new JButton("");
                        else
                        {
                            bu[i][j] = new JButton(cnt + "");
                            
                        }
                        bu[i][j].setActionCommand("bu" + i + j);
                        //bu[i][j].addKeyListener((new MoveListener()));
                        bu[i][j].addActionListener((new MoveListener()));
                        
                        bu[i][j].setFont(new Font("Arial", Font.BOLD, 15));
                        add(bu[i][j]);
                    }
                }
                
            }
      

  3.   

    应该是MouseListener吧,逻辑应该是有数字的移到空白处,没看你的逻辑,肯定不合理的,呵呵
    package testA;import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.lang.*;
    import javax.swing.JOptionPane;public class Tupianjiemian extends JPanel {
    // 声明变量
    JButton[][] bu; int flag; public Tupianjiemian() {
    // 布局设置,并设置i是竖坐标,j是横坐标
    super(new GridLayout(3, 3));
    bu = new JButton[3][3];
    for (int i = 0, cnt = 1; i < 3; i++) { for (int j = 0; j < 3; j++, cnt++) {
    if ((i == 2) && (j == 2))
    bu[2][2] = new JButton("");
    else {
    bu[i][j] = new JButton(cnt + "");
    bu[i][j].setActionCommand("bu" + i + j);
    bu[i][j].addMouseListener((new MoveListener()));
    bu[i][j].setFont(new Font("Arial", Font.BOLD, 15));
    add(bu[i][j]);
    }
    }
    } } public class MoveListener extends MouseAdapter {
    public void mouseClicked(MouseEvent e) {
    for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) {
    if (e.getSource().equals(bu[i][j])) {
    if (j + 1 < 3 && bu[i][j + 1].getLabel() == "") {
    bu[i][j + 1].setLabel(bu[i][j].getLabel());
    bu[i][j].setLabel("");
    }
    if (i + 1 < 3 && bu[i + 1][j].getLabel() == "") {
    bu[i + 1][j].setLabel(bu[i][j].getLabel());
    bu[i][j].setLabel("");
    }
    if (j - 1 > -1 && bu[i][j - 1].getLabel() == "") {
    bu[i][j - 1].setLabel(bu[i][j].getLabel());
    bu[i][j].setLabel("");
    }
    if (i - 1 > -1 && bu[i - 1][j].getLabel() == "") {
    bu[i - 1][j].setLabel(bu[i][j].getLabel());
    bu[i][j].setLabel("");
    }
    }
    }
    if (bu[0][0].getLabel() == "") {
    flag = 0;
    for (int i = 2, cnt = 15; i >= 0; i--) {
    for (int j = 2; j >= 0; j--, cnt--) {
    if (cnt != 1)
    if (e.getSource().equals(bu[Math.abs(i - 2)][Math.abs(j - 2)]))
    flag++; }
    }
    }
    }
    } public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.add(new Tupianjiemian());
    frame.setVisible(true);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    }