import java.awt.Dimension; import java.awt.Font; import java.awt.Frame; import java.awt.Color; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyListener; import java.awt.event.WindowEvent; import java.awt.event.WindowAdapter; import java.beans.PropertyChangeListener; import javax.swing.BorderFactory; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; public class GUI extends Frame { private static final long serialVersionUID = 1L; private JButton newGame; private JButton solveGame; private JButton checkAnswers; private JPanel centerPanel; private JPanel eastPanel; private JPanel gridBlocks; private int answer; /**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(int[][] map, int[][] key) { setLayout(new BorderLayout()); setBackground(Color.lightGray); UIManager.getLookAndFeel(); setTitle("Sudoku!"); setPreferredSize(new Dimension(400, 400)); add("East", getEastPanel()); add("Center", getCenterPanel(map, key)); pack(); //Handles the exit call. addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { setVisible(false); dispose(); System.exit(0); } }); /**Listeners*/ getNGButton().addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //TODO Create method to start new game. } }); getCAButton().addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); getSGButton().addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //TODO Add Solve Game button. } }); /**EndListeners*/ } private JPanel getCenterPanel(int[][] map, int[][] key) { if(centerPanel==null) { centerPanel = new JPanel(new BorderLayout()); centerPanel.setBorder(BorderFactory.createLineBorder(Color.black)); centerPanel.add("Center",getGridBlocks(map, key)); } return centerPanel; } private JPanel 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 JPanel(new GridLayout(0,1)); eastPanel.add(getNGButton()); eastPanel.add(getCAButton()); eastPanel.add(getSGButton()); } return eastPanel; } /**----Buttons----*/ private JButton getNGButton() { if (newGame == null) newGame = new JButton("New Game"); return newGame; } private JButton getCAButton() { if (checkAnswers == null) checkAnswers = new JButton("Check Answers"); return checkAnswers; } private JButton getSGButton() { if (solveGame == null) solveGame = new JButton("Solve Game"); return solveGame; } /**----EndButtons----*/ // private void checkAnswers(int[][] map) // { // for(int i=0;i1 length. if(cell.getText().length()>1) { String str = cell.getText(); str = str.replaceAll("(.)*", "$1"); System.out.println(cell.getText()); cell.setText(str); System.out.println("Changed to: " + cell.getText()); } } }); //Sets the cell's property name for callback later. cell.getDocument().getProperty("val"); cell.setFont(font); //Assigns the cell to the current 3x3 block. smallBlocks.add(cell); } //Assigns all of the the 3x3 blocks to the 9x9 grid. gridBlocks.add(smallBlocks); } } return gridBlocks; } /** A simple main to let us test the GUI */ public static void main(java.lang.String[] args) { int[][] map=new int[9][9]; int[][] key=new int[9][9]; (new GUI(map,key)).setVisible(true); } }