5 Replies - 5797 Views - Last Post: 28 February 2010 - 10:42 PM Rate Topic: -----

#1 buzooty  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 12-October 08

Sudoku Board

Posted 28 February 2010 - 12:25 PM

Hi, I'm attempting to make a GUI for a sudoku board. It doesn't need to be functional, but i have to use textfield, and I have to have those four buttons on the side. I need to have a 9x9 grid, but with the 3x3 squares having a thicker border. Here's the code I have so far. I think the problem comes from me trying to add a panel to another panel. Any help would be awesome, Thanks!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class Board extends JFrame {
	private JFrame window = new JFrame("Sudoku");
	public Board(){
		window.setSize(600,300);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setLayout(new GridLayout(1,2));
		Border lineBorder = new LineBorder(Color.BLACK,2);
		String[] difficulties = { "Easy", "Medium", "Hard" };
		JComboBox difficultyBox = new JComboBox(difficulties);
		difficultyBox.setSelectedIndex(1);

		
		JPanel p2 = new JPanel(new GridLayout(3,3));
		p2.setBorder(lineBorder);
		for(int i =0; i <=8; i++){
			p2.add(new JTextField(1));
		}
		
		JPanel p1 = new JPanel(new GridLayout(3,3));
		for(int i = 0; i <=8; i++){
			p1.add(p2);
		}
		JPanel Buttons = new JPanel(new GridLayout(5,1));
		Buttons.add(new JButton("Reset"));
		Buttons.add(new JButton("Solve"));
		Buttons.add(new JButton("Hint"));
		Buttons.add(new JButton("New Puzzle"));
		Buttons.add(difficultyBox);
		
		window.add(p1);
		window.add(Buttons);
		window.setVisible(true);
	}
	public static void main(String[] args){
		Board Sud = new Board();
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Sudoku Board

#2 javabie  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 117
  • Joined: 12-February 10

Re: Sudoku Board

Posted 28 February 2010 - 12:47 PM

		JPanel p2 = null;
		JPanel p1 =  new JPanel(new GridLayout(3,3));
		for (int k =0; k<9; k++)
		{
			p2 = new JPanel(new GridLayout(3,3));

			p2.setBorder(lineBorder);
			for(int i =0; i <=8; i++){
				p2.add(new JTextField(1));
			}
			for(int i = 0; i <=8; i++){
				p1.add(p2);
			}
		}


you just needed to loop 9 times and add panel every time..




edited:
your design looks good.....
since your are extending JFrame.. then you don't need to create a JFrame object..
simply change your code to ...

public class Board extends JFrame {

	//private JFrame window = new JFrame("Sudoku");
	public Board(){
		setSize(600,300);



see how i changed the windows.setSize(600, 300) to setSize(600,300).. just do the same for every other windows.*

This post has been edited by javabie: 28 February 2010 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 buzooty  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 12-October 08

Re: Sudoku Board

Posted 28 February 2010 - 02:48 PM

Thanks!!! haha I had been looking at that code forever I couldn't see what was wrong, thanks for catching that. And that was a fast reply
P.S. your winning in hockey :(
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Sudoku Board

Posted 28 February 2010 - 05:31 PM

Nothing wrong (as GUI presentation is conncerned) with Javabie code

      for(int i =0; i <=8; i++){ 
        p2.add(new JTextField(1)); 
      } 



but your JTextField will have to be declared as instance variables if you want to be able to access them later
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5875
  • View blog
  • Posts: 12,757
  • Joined: 16-October 07

Re: Sudoku Board

Posted 28 February 2010 - 06:40 PM

This is a little funky:
class Board extends JFrame {
	private JFrame window = new JFrame("Sudoku");
	public Board(){
		window.setSize(600,300);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setLayout(new GridLayout(1,2));
		// ...
		window.add(p1);
		window.add(Buttons);
		window.setVisible(true);
	}
}



Get rid of window, make it this.

Break up the display elements into discrete chunks. It will make the logic of the thing easier to follow.

e.g.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

class Buttons extends JPanel {
	JComboBox difficultyBox;
	public Buttons(){
		this.setLayout(new GridLayout(5,1));
		this.add(new JButton("Reset"));
		this.add(new JButton("Solve"));
		this.add(new JButton("Hint"));
		this.add(new JButton("New Puzzle"));
		
		difficultyBox = new JComboBox(new String[]{ "Easy", "Medium", "Hard" });
		difficultyBox.setSelectedIndex(1);
		this.add(difficultyBox);
	}
}


class Box extends JPanel {
	public final int CELL_COUNT = 9;
	public class Cell extends JTextField {
		private int number;
		public Cell() {  }
		public void setNumber(int number) { 
			this.number = number;
			this.setText("1");
		}
		public int getNumber() { return number; }
	}
	
	public Cell [] cells = new Cell[CELL_COUNT];
	
	public Box(){
		this.setLayout(new GridLayout(3,3));
		this.setBorder(new LineBorder(Color.BLACK,2));
		for(int i=0; i<CELL_COUNT; i++){
			cells[i] = new Cell();
			this.add(cells[i]);
		}
	}
}



class Board extends JFrame {
	public final int BOX_COUNT = 9;
	public Box [] boxes = new Box[BOX_COUNT];
	
	public Board(){
		this.setTitle("Sudoku");
		this.setSize(600,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new GridLayout(1,2));
		
		JPanel p1 = new JPanel(new GridLayout(3,3));
		for(int i=0; i<BOX_COUNT; i++){
			boxes[i] = new Box();
			p1.add(boxes[i]);
		}
		
		this.add(p1);
		this.add(new Buttons());
	}
}



public class SudokuGUI {
	public static void main(String[] args){
		Board board = new Board();
		board.setVisible(true);
	}
}


Was This Post Helpful? 1
  • +
  • -

#6 buzooty  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 12-October 08

Re: Sudoku Board

Posted 28 February 2010 - 10:42 PM

Thanks for the help, I'm starting to understand this stuff more and more :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1