Что если вы создали свой собственный JPanel для хранения цифры и рисования черной границы, а затем пользовательский JPanel для хранения сетки из них?
Пример пользовательских JPanel:
class SudokuPanel extends JPanel {
int digit; //the number it would display
int x, y; //the x,y position on the grid
SudokuPanel(int x, int y) {
super();
this.x = x;
this.y = y;
/** create a black border */
setBorder(BorderFactory.createLineBorder(Color.black));
/** set size to 50x50 pixel for one square */
setPreferredSize(50,50);
}
public int getDigit() { return digit; }
//getters for x and y
public void setDigit(int num) { digit = num }
}
Пример пользовательской сетки JPanel:
class SudokuGrid extends JPanel {
SudokuGrid(int w, int h) {
super(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
/** construct the grid */
for (int i=0; i<w; i++) {
for (int j=0; j<h; j++) {
c.weightx = 1.0;
c.weighty = 1.0;
c.fill = GridBagConstraints.BOTH;
c.gridx = i;
c.gridy = j;
add(new SudokuPanel(i, j), c);
}
}
/** create a black border */
setBorder(BorderFactory.createLineBorder(Color.black));
}
}
Пример кода:
...
SudokuGrid sg = new SudokuGrid(3,3);
myFrame.add(sg);
...