Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Button;
- import java.awt.Checkbox;
- import java.awt.CheckboxGroup;
- import java.awt.Panel;
- import java.awt.TextArea;
- import java.awt.Frame;
- import java.awt.Color;
- import java.awt.Insets;
- import java.awt.FlowLayout;
- import java.awt.BorderLayout;
- import java.awt.GridLayout;
- import java.awt.GridBagLayout;
- import java.awt.GridBagConstraints;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.awt.event.WindowListener;
- import java.awt.event.WindowEvent;
- import java.awt.event.WindowAdapter;
- import javax.swing.JTextField;
- public class GUI extends Frame
- {
- private Button newGame;
- private Button solveGame;
- private Button exitButton;
- private Panel centerPanel;
- private Panel eastPanel;
- private Panel gridBlocks;
- private Panel sudokuField;
- /**Constructor:
- * This GUI is made of two parts (East/Center) in a border layout.
- * The East portion contains two buttons, centered vertically, of
- * the same width. The Center portion contains the Sudoku game and
- * will scale to the window's size after the preferred width of the
- * East portion is satisfied.
- */
- public GUI()
- {
- setLayout(new BorderLayout());
- setBackground(Color.lightGray);
- setTitle("Sudoku!");
- add("East", getEastPanel());
- add("Center", getCenterPanel());
- pack();
- //Handles the exit call.
- addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- setVisible(false);
- dispose();
- System.exit(0);
- }
- });
- }
- private Panel getCenterPanel()
- {
- if(centerPanel==null)
- {
- Panel centerPanel = new Panel(new BorderLayout());
- centerPanel.add("Center",getGridBlocks());
- }
- return centerPanel;
- }
- private Panel getEastPanel()
- {
- if(eastPanel==null)
- {
- /**
- * 0,1 allows for only 1 column, but any amount
- * of rows. Grid items are always the same size.
- **/
- eastPanel = new Panel(new GridLayout(0,1));
- eastPanel.add(getNGButton());
- eastPanel.add(getSGButton());
- }
- return eastPanel;
- }
- private Button getNGButton()
- {
- if (newGame == null)
- newGame = new Button("New Game");
- return newGame;
- }
- private Button getSGButton()
- {
- if (solveGame == null)
- solveGame = new Button("Solve Game");
- return solveGame;
- }
- private Panel getGridBlocks()
- {
- if(gridBlocks==null)
- {
- Panel gridBlocks = new Panel(new GridLayout(0,3));
- for(int bBlocks=1;bBlocks<=9;bBlocks++)
- {
- /**
- * Generate the inner blocks that will
- * be filled inside the larger grid.
- **/
- Panel smallBlocks = new Panel(new GridLayout(0,3));
- for(int sBlocks=1;sBlocks<=9;sBlocks++)
- {
- JTextField cell = new JTextField(Integer.toString(sBlocks));
- smallBlocks.add(cell);
- }
- gridBlocks.add(smallBlocks);
- }
- }
- return gridBlocks;
- }
- /** A simple main to let us test the GUI */
- public static void main(java.lang.String[] args)
- {
- (new GUI()).setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement