Advertisement
valdeEdius

SudokuWindow

Mar 14th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.20 KB | None | 0 0
  1. import java.awt.Button;
  2. import java.awt.Checkbox;
  3. import java.awt.CheckboxGroup;
  4. import java.awt.Panel;
  5. import java.awt.TextArea;
  6. import java.awt.Frame;
  7. import java.awt.Color;
  8. import java.awt.Insets;
  9. import java.awt.FlowLayout;
  10. import java.awt.BorderLayout;
  11. import java.awt.GridLayout;
  12. import java.awt.GridBagLayout;
  13. import java.awt.GridBagConstraints;
  14. import java.awt.event.ActionListener;
  15. import java.awt.event.ActionEvent;
  16. import java.awt.event.WindowListener;
  17. import java.awt.event.WindowEvent;
  18. import java.awt.event.WindowAdapter;
  19.  
  20. import javax.swing.JTextField;
  21.  
  22. public class GUI extends Frame
  23. {
  24.   private Button newGame;
  25.   private Button solveGame;
  26.   private Button exitButton;
  27.  
  28.   private Panel centerPanel;
  29.   private Panel eastPanel;
  30.   private Panel gridBlocks;
  31.   private Panel sudokuField;
  32.  
  33.  
  34.  
  35.   /**Constructor:
  36.    * This GUI is made of two parts (East/Center) in a border layout.
  37.    * The East portion contains two buttons, centered vertically, of
  38.    * the same width. The Center portion contains the Sudoku game and
  39.    * will scale to the window's size after the preferred width of the
  40.    * East portion is satisfied.
  41.    */
  42.   public GUI()
  43.   {
  44.     setLayout(new BorderLayout());
  45.     setBackground(Color.lightGray);
  46.     setTitle("Sudoku!");
  47.     add("East",  getEastPanel());
  48.     add("Center", getCenterPanel());
  49.     pack();
  50.    
  51.    
  52.     //Handles the exit call.
  53.     addWindowListener(new WindowAdapter()
  54.     {
  55.       public void windowClosing(WindowEvent e)
  56.       {
  57.         setVisible(false);
  58.         dispose();
  59.         System.exit(0);
  60.       }
  61.     });
  62.    
  63.    
  64.   }
  65.   private Panel getCenterPanel()
  66.   {
  67.     if(centerPanel==null)
  68.     {
  69.       Panel centerPanel = new Panel(new BorderLayout());
  70.       centerPanel.add("Center",getGridBlocks());
  71.     }
  72.     return centerPanel;
  73.   }
  74.   private Panel getEastPanel()
  75.   {
  76.     if(eastPanel==null)
  77.     {
  78.       /**
  79.        * 0,1 allows for only 1 column, but any amount
  80.        * of rows. Grid items are always the same size.
  81.       **/
  82.       eastPanel = new Panel(new GridLayout(0,1));
  83.       eastPanel.add(getNGButton());
  84.       eastPanel.add(getSGButton());
  85.     }
  86.     return eastPanel;
  87.   }
  88.   private Button getNGButton()
  89.   {
  90.     if (newGame == null)
  91.       newGame = new Button("New Game");
  92.     return newGame;
  93.   }
  94.   private Button getSGButton()
  95.   {
  96.     if (solveGame == null)
  97.       solveGame = new Button("Solve Game");
  98.     return solveGame;
  99.   }
  100.   private Panel getGridBlocks()
  101.   {
  102.     if(gridBlocks==null)
  103.     {
  104.       Panel gridBlocks = new Panel(new GridLayout(0,3));
  105.       for(int bBlocks=1;bBlocks<=9;bBlocks++)
  106.       {
  107.         /**
  108.          * Generate the inner blocks that will
  109.          * be filled inside the larger grid.
  110.         **/
  111.         Panel smallBlocks = new Panel(new GridLayout(0,3));
  112.         for(int sBlocks=1;sBlocks<=9;sBlocks++)
  113.         {
  114.          JTextField cell = new JTextField(Integer.toString(sBlocks));
  115.          smallBlocks.add(cell);
  116.         }
  117.         gridBlocks.add(smallBlocks);
  118.       }
  119.     }
  120.     return gridBlocks;
  121.   }
  122.  
  123.  
  124.  
  125.   /** A simple main to let us test the GUI */
  126.   public static void main(java.lang.String[] args)
  127.   {
  128.     (new GUI()).setVisible(true);
  129.   }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement