Example 1-

Tic Tac Toe Game -

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TT extends Frame implements ActionListener
{
    Button b[] = new Button[9];
    String pl = "X";
    public TT()
    {
        super("Tic Tac Toe");
        setSize(400,400);
        setResizable(false);
        addWindowListener(new WindowAdapter() 
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(1);
            }
        
        });
        setFont(new Font("Times New Roman",Font.BOLD,50));
        setLayout(new GridLayout(3,3));
        for(int i=0;i<9;i++)
        {
            b[i] = new Button("");
            add(b[i]);
            b[i].addActionListener(this);
        }
        
        
    }
    public void actionPerformed(ActionEvent e)
    {
        
        Button bt = (Button)e.getSource();
        if(bt.getLabel().equals(""))
        {
        bt.setLabel(pl);
        if(win())
        {
            JOptionPane.showMessageDialog(null, pl+"Wins");
            reset();
        }
        else
                {
                 if(draw())
                 {
                     JOptionPane.showMessageDialog(null, "Match Draw");
                      reset();
                 }
                 else
                 {
                     pl= pl.equals("X")?"O":"X"; 
                 }
                }
        }
        
      
    }
    
    public static void main(String args[])
    {
        new TT().setVisible(true);
    }
        
    public boolean win()
    {
        if((!b[0].getLabel().equals(""))&&
            b[0].getLabel().equals(b[1].getLabel())&&
            b[0].getLabel().equals(b[2].getLabel()))
        {
            return true;
        }
        if((!b[3].getLabel().equals(""))&&
            b[3].getLabel().equals(b[4].getLabel())&&
            b[3].getLabel().equals(b[5].getLabel()))
        {
            return true;
        }
        if((!b[6].getLabel().equals(""))&&
            b[6].getLabel().equals(b[7].getLabel())&&
            b[6].getLabel().equals(b[8].getLabel()))
        {
            return true;
        }
        if((!b[0].getLabel().equals(""))&&
            b[0].getLabel().equals(b[3].getLabel())&&
            b[0].getLabel().equals(b[6].getLabel()))
        {
            return true;
        }
        if((!b[1].getLabel().equals(""))&&
            b[1].getLabel().equals(b[4].getLabel())&&
            b[1].getLabel().equals(b[7].getLabel()))
        {
            return true;
        }
        if((!b[2].getLabel().equals(""))&&
            b[2].getLabel().equals(b[5].getLabel())&&
            b[2].getLabel().equals(b[8].getLabel()))
        {
            return true;
        }
        if((!b[0].getLabel().equals(""))&&
            b[0].getLabel().equals(b[4].getLabel())&&
            b[0].getLabel().equals(b[8].getLabel()))
        {
            return true;
        }
        if((!b[2].getLabel().equals(""))&&
            b[2].getLabel().equals(b[4].getLabel())&&
            b[2].getLabel().equals(b[6].getLabel()))
        {
            return true;
        }
        return false;
    }
    public boolean draw()
    {
        for(Button bt:b)
        {
           if(bt.getLabel().equals(""))
            return false;
        }
        return true;
    }
    public void reset()
    {
        for(Button bt:b)
        {
            bt.setLabel("");
        }
    }
        
    
}
        

Output -

Example 2-

Puzzle Game - You have to arrange numbers in order.

import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;

public class Puzzle extends Frame implements ActionListener
{
    Button b[][] = new Button[4][4];
    int row,col;    
    public Puzzle()
    {
        //setResizable(false);
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(1);
            }
        
        });
        setLayout(new GridLayout(4,4));
        int count =1;
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                b[i][j]= new Button(""+count);
                b[i][j].setActionCommand(""+i+j);
                b[i][j].addActionListener(this);
                Font f = new Font("Times New Roman",Font.BOLD,30);
                b[i][j].setFont(f);
                add(b[i][j]);
                count++;
            }
        }
        b[3][3].setLabel("");
        row=col=3;
        shuffle();
    }
    
    public void shuffle()
    {
        int count =0;
        while(count<=100)
        {
            int val = (int)(Math.random()*4);
            
            switch(val)
            {
                case 0: if(col!=0)
            {
                b[row][col].setLabel(b[row][col-1].getLabel());
                b[row][col-1].setLabel("");
                    col--;
                    count++;
            }
                    break;
                case 1: if(col!=3)
                {
                    b[row][col].setLabel(b[row][col+1].getLabel());
                    b[row][col+1].setLabel("");
                    col++;
                    count++;
                }
                    break;
                case 2: if(row!=0)
                {
                    b[row][col].setLabel(b[row-1][col].getLabel());
                    b[row-1][col].setLabel("");
                    row--;
                    count++;
                }
                    break;
                case 3: if(row!=3)
                {
                    b[row][col].setLabel(b[row+1][col].getLabel());
                    b[row+1][col].setLabel("");
                    row++;
                    count++; 
                }
                    break;
            }
        }
    }
        public static void main(String args[])
        {
            new Puzzle().setVisible(true);
        }

        
        public void actionPerformed(ActionEvent e)
            {
                Button bt = (Button)e.getSource();
                int val = Integer.parseInt(e.getActionCommand());
                int r = val/10;
                int c = val%10;
                if(!(r==row&&c==col))
                {
                    if(r==row)
                    {
                        if(c=c;i--)
                            {
                                b[row][i+1].setLabel(b[row][i].getLabel());
                            }
                               b[row][c].setLabel("");
                               col = c;
                        }
                        else
                        {
                           for(int i =col+1;i<=c;i++)
                            {
                                b[row][i-1].setLabel(b[row][i].getLabel());
                            }
                               b[row][c].setLabel("");
                               col = c; 
                        }
                    }
                    if(c==col)
                    {
                        if(r=r;i--)
                            {
                                b[i+1][col].setLabel(b[i][col].getLabel());
                                        
                            }
                            b[r][col].setLabel("");
                            row=r;
                        }
                        else
                        {
                             for(int i=row+1;i<=r;i++)
                            {
                                b[i-1][col].setLabel(b[i][col].getLabel());
                                        
                            }
                            b[r][col].setLabel("");
                            row=r;
                        }
                    }
                }
                if(win())
                {
                    JOptionPane.showMessageDialog(null, "You Win");
                    shuffle();
                }
            }
            public boolean win()
            {
             int count=1;
             for(int i=0;i<4;i++)
             {
                 for(int j=0;j<4;j++)
                 {
                     if(!(i==3&&j==3))
                     {
                         if(!b[i][j].getLabel().equals(""+count))
                         {
                             return false;
                         }
                         count++;
                     }
                 }
                 
             }
             return true;
            }
        }        

Output -