Reputation: 35
I am trying to build a sudoku board. For now i am just trying to get the board to draw, i have tried just drawing lines but was told this was better....i have not gotten this to work as of yet. any hints on what i am doing wrong
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.*;
public class SudokuView4 extends JPanel {
int rows = 3;
int col = 3;
public JPanel container = new JPanel(new GridLayout(rows*col,rows*col));
public SudokuView4(SudokuBase sb) {
// TODO Auto-generated constructor stub
for(int r = 0; r < rows; r++){
for(int c = 0; c < col; c++){
//container.add(Region(rows,col));
//add(build);
//build.setSize(50, 50)
Region();
container.setVisible(true);
}
}
}
//class Region extends JPanel {
public void Region( ) {
//setLayout(new GridLayout(3,3));
//JPanel grid = new JPanel(new GridLayout(3,3));
//grid.setSize(50, 50);
for(int r1 = 0; r1 < rows; r1++){
for(int c1 = 0; c1 < col; c1++){
//JPanel grid = new JPanel();
JButton build = new JButton();
container.add(build);
//container.setVisible(true);
}
}
}
}
Upvotes: 0
Views: 1102
Reputation: 124
The JPanel container needs to be placed inside of a JFrame and updated once the buttons are added. This is if your goal is to run a Java application.
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.*;
public class SudokuView4 extends JPanel {
int rows = 3;
int col = 3;
public JPanel container = new JPanel(new GridLayout(rows*col,rows*col));
// added main for testing
public static void main(String [] args){
SudokuView4 sudoku = new SudokuView4();
}
public SudokuView4(/*SudokuBase sb*/) {
// TODO Auto-generated constructor stub
JFrame frame = new JFrame();
frame.add(container);
for(int r = 0; r < rows; r++){
for(int c = 0; c < col; c++){
//container.add(Region(rows,col));
//add(build);
//build.setSize(50, 50)
Region();
container.setVisible(true);
}
}
frame.setSize(300,300);
frame.setVisible(true);
}
//class Region extends JPanel {
public void Region( ) {
//setLayout(new GridLayout(3,3));
//JPanel grid = new JPanel(new GridLayout(3,3));
//grid.setSize(50, 50);
for(int r1 = 0; r1 < rows; r1++){
for(int c1 = 0; c1 < col; c1++){
//JPanel grid = new JPanel();
JButton build = new JButton();
container.add(build);
//container.setVisible(true);
}
}
}
}
Upvotes: 1