如图:
排列成这样以后,怎么判断已经完成游戏呢?
1楼是程序代码。

解决方案 »

  1.   

    这是PuzzleFrame.java类import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;public class PuzzleFrame extends JFrame implements ActionListener
    {
      public int a=1;
      PuzzlePad puzzlePad;
      JLabel label;
      JButton 开始,暂停,停止;
      JMenuBar bar;
      JMenu fileMenu;
      JMenuItem 排行榜,退出;
      Container con=null;
      public Time time; 
      public Timer t;
      public PuzzleFrame(String title)
      {
       super(title);
       time = new Time();
       ActionListener listener = new ActionListener() 
         { 
               public void actionPerformed(ActionEvent e) 
                    { 
                      time.timePass(); 
                      label.setText(time.getTime()); 
                    } 
         };
        t = new Timer(1000, listener);
        label = new JLabel(time.getTime(), JLabel.CENTER); 
        label.setFont(new Font("仿宋", Font.BOLD, 25));
        bar=new JMenuBar();
        fileMenu=new JMenu("游戏");
        排行榜=new JMenuItem("排行榜");
        退出=new JMenuItem("退出");
        fileMenu.add(排行榜);
        fileMenu.add(退出);
        bar.add(fileMenu);
        setJMenuBar(bar);
        排行榜.addActionListener(this);
        退出.addActionListener(this);
        开始=new JButton("开始@继续");
        暂停=new JButton("暂停");
        停止=new JButton("停止");
        开始.addActionListener(this);
        暂停.addActionListener(this);
        暂停.setEnabled(false);
        停止.addActionListener(this);
        停止.setEnabled(false);
        puzzlePad=new PuzzlePad(3,3,50,50);                
        con=getContentPane();//容器
        con.add(puzzlePad,BorderLayout.CENTER);
        JPanel pNorth=new JPanel();
        pNorth.add(开始);
        pNorth.add(暂停);
        pNorth.add(停止);
        pNorth.add(label);
        con.add(pNorth,BorderLayout.NORTH);
        con.validate();//验证此容器及其所有子组件。
        addWindowListener(new WindowAdapter()
                        { public void windowClosing(WindowEvent e)
                           {
                             System.exit(0);
                           }
                        });
        validate();
      }
      
      
      
      class Time 
       { 
         private int hour, min, sec;      public Time() 
           { 
             hour = 0; 
             min = 0; 
             sec = 0;
           }   public void timePass() 
       { 
         sec++; 
         if(sec == 60) 
           { 
             sec = 0; 
             min++; 
         if(min == 60) 
           { 
             min = 0; 
             hour++; 
         if(hour == 24) 
           { 
             hour = 0; 
           } 
           } 
           } 
       } 
        public String getTime() 
         { 
            return String.format("%1$02d : %2$02d : %3$02d", hour, min, sec); 
         }    public void reset() 
         { 
           hour = 0; 
           min = 0; 
           sec = 0;
         } 
       }
     
     public void actionPerformed(ActionEvent e)
      { if(e.getSource()==开始)
           {
            if (a==1)
            {
            puzzlePad.随机排列数字();
            t.start();
            开始.setEnabled(false);
            暂停.setEnabled(true);
            停止.setEnabled(true);
           }
            if (a==2)
            {
              t.start();
              开始.setEnabled(false);
              暂停.setEnabled(true);
              停止.setEnabled(true);
            }
           }
        else if(e.getSource()==排行榜)
         {
            JOptionPane.showMessageDialog(null,"数据库不存在!");
         }
        else if(e.getSource()==退出)
         {
            dispose();
         }
        else if(e.getSource()==暂停)
         {
            t.stop();
            a=2;
            开始.setEnabled(true);
            暂停.setEnabled(false);
            //JOptionPane.showMessageDialog(null,"失去焦点!");
         }
        else if(e.getSource()==停止)
         {
           time.reset(); 
           label.setText(time.getTime());
           a=1;
           开始.setEnabled(true);
           暂停.setEnabled(false);
           停止.setEnabled(false); 
         }
         } 
        
     public  static void main(String args[])
       {
          PuzzleFrame MoBan = new PuzzleFrame("魔板游戏");
          MoBan.setBounds(300,100,440,360);
      MoBan.setVisible(true);
       }  
    }
      

  2.   

    这是PuzzlePad类import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.util.Vector;
    public class PuzzlePad extends JPanel implements ActionListener
    {
      int rows ,colums;           
      int width,height;           
      SquarePoint point[][];      
      JButton block[][];          
      Vector vector;              
      int leftX=50,leftY=50;    
           
      public PuzzlePad(int r,int c,int w,int h)
        {
          setLayout(null);
          vector=new Vector();
          rows=r;
          colums=c;
          width=w;
          height=h;
          block=new JButton[rows][colums];
          point=new SquarePoint[rows][colums];
          for(int i=0;i<rows;i++)
             { 
              for(int j=0;j<colums;j++)
                {
                   if((i==rows-1)&&(j==colums-1))
                      {
                      }
                   else    
                   {
                    block[i][j]=new JButton();
                    block[i][j].setSize(width,height);
                    block[i][j].addActionListener(this);
                    vector.add(block[i][j]);
                   }
                }
             }
           for(int i=0;i<rows;i++)
             { 
              for(int j=0;j<colums;j++)
                {
                  point[i][j]=new SquarePoint(50+j*width,50+i*height);
                   if((i==rows-1)&&(j==colums-1))
                      {
                      }
                   else
                      {
                       point[i][j].setBlock(block[i][j],this);
                     }
                }
             } 
        }   public void actionPerformed(ActionEvent e)
        {
          JButton button=(JButton)e.getSource();
          int x=button.getBounds().x;
          int y=button.getBounds().y;
          int m=0,n=0;
          
          for(int i=0;i<rows;i++)
             { 
              for(int j=0;j<colums;j++)
               {
                 if(point[i][j].getX()==x&&point[i][j].getY()==y)
                    {
                      m=i;
                      n=j;                               
                    }
               }
             }
           
           int 上=Math.max(0,m-1);
           int 下=Math.min(rows-1,m+1);
           int 左=Math.max(0,n-1);
           int 右=Math.min(colums-1,n+1);
           
           if(point[上][n].get有方块()==false)          
             {
               point[上][n].setBlock(button,this);
               point[m][n].set有方块(false);
             }
           else if(point[下][n].get有方块()==false)     
             {
               point[下][n].setBlock(button,this);
               point[m][n].set有方块(false);
             }
           else if(point[m][左].get有方块()==false)     
             {
               point[m][左].setBlock(button,this);
               point[m][n].set有方块(false);
             }
           else if(point[m][右].get有方块()==false)     
             {
               point[m][右].setBlock(button,this);
               point[m][n].set有方块(false);
             }
        }
      public void 随机排列数字()
        { 
           vector.removeAllElements();
           for(int i=0;i<rows;i++)
             { 
              for(int j=0;j<colums;j++)
                {
                   if((i==rows-1)&&(j==colums-1))
                      {
                      }
                   else
                      {
                         vector.add(block[i][j]);
                      }
                }
             }
          int i=1;
          while(vector.size()>0)
            {
              int n=(int)(Math.random()*vector.size());
              JButton b=(JButton)vector.elementAt(n);
              b.setText(""+i);
              b.setIcon(null);
              b.setBorder(BorderFactory.createLineBorder(Color.blue,1));
              vector.remove(n);
              i++;
            }
          repaint();  
        }
     
     public void paintComponent(Graphics g)//绘制背景
        {
           super.paintComponent(g); 
           g.setColor(Color.black);
           g.fillRect(leftX,leftY,colums*width,rows*height); 
        }
    }
    这是SquarePoint类import java.awt.*;
    import javax.swing.JButton;public class SquarePoint
    {
       int x,y;
       boolean 有方块;
       JButton  block=null;                   
       Container con=null;
       public SquarePoint(int x,int y)
       {
          this.x=x;
          this.y=y;
       }
      public boolean get有方块()
      {
        return 有方块;
      }
      public void set有方块(boolean boo)
      {
        有方块=boo;
      }
     
      public int getX()
      {
        return x;
      }
      public int getY()
      {
        return y;
      }
      public void setBlock(JButton block,Container con)
      {
         this.con=con;
         this.block=block;
         con.add(block);
         block.setLocation(x,y);
         有方块=true;
         con.validate(); 
      }
      public JButton getBlock()
      {
         return block;
      }
    }
      

  3.   

    代码不全. 少一个Time 的Class .                       你要判断一列..你就直接遍历数组不就行了. 
      

  4.   

    没看到你图,猜测哈:如果正确结束游戏只有一种结果的话可以弄个数组,9个大小,每个对应一个数字,当一个数字进入正确位置,相应位置为true,然后判断这个数组;
    如果正确结束游戏有多种结果的话按照每种可能去判断了
      

  5.   


    不少啊,time的class 在PuzzleFrame里定义了