Monday, 20 May 2013

Swing gui not displaying, but no errors?

Swing gui not displaying, but no errors?

I am working on a java program that solves sudoku puzzles. So far all I have coded is a class that draws the board with swing, and another class that calls the gui class.
When I try and run the program nothing happens. No error messages are shown, but the gui doesn't show either. It immediately terminates.
Here is my code so far:
Gui class:
package sudoku;

import java.awt.*;
import javax.swing.*;

public class Gui {

    Gui gui;
    JPanel board;
    JPanel subBoard[][];
    GridLayout layout;
    JLabel square[][];

    public void load() {

        gui = new Gui();
        gui.setUp();
        gui.buildBoard();

    }

    private void setUp() {

        layout = new GridLayout(3, 3);
        board = new JPanel(layout);
        subBoard = new JPanel[3][3];
        square = new JLabel[9][9];

    }

    private void buildBoard() {

        // set up board
        board.setSize(800, 600);
        board.setVisible(true);

        int mod = 0;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {

                // add subBoards to board
                subBoard[i][j] = new JPanel();
                board.add(subBoard[i][j]);
                subBoard[i][j].setLayout(layout);
                subBoard[i][j].setVisible(true);

                // add textfields to each subBoard
                square[i + mod][j + mod] = new JLabel();
                subBoard[i][j].add(square[i + mod][j + mod]);
                square[i + mod][j + mod].setVisible(true);

            }
            mod += 3;

        }
    }

}
main class:
package sudoku;

public class SudokuSolver {

    public static void main(String[] args) {

        Gui gui = new Gui();
        gui.load();

    }

}
I tried running it in both eclipse and netbeans but got the same result both times. Why does this not work?

No comments:

Post a Comment