我正在为Sudoku开发GUI。我的Droid(SudokuFree)有一个不错的应用程序,我想复制其功能并添加一个小功能(保存开发板,继续,然后根据需要还原)。一些代码已被注释掉以使其更像SSCCE,如果您有疑问,请参阅第二篇文章。有两篇文章对我有很大帮助,我从他们那里借了很多下面的代码。

Action listener for JButton array

Building a GUI for a Sudoku Solver (Complete with ASCII Example)

我的问题是JButtons只有在它们重叠时才可见。我搜索了以前的帖子,只找到了对setVisible(true)的引用。

谢谢

public class RunSudokuNinja implements Runnable{
    @Override
    public void run() {
        //SudokuEngineInterface sudokuEngine = new SudokuEngine();
        SudokuView sudokuView = new SudokuView();
        //sudokuView.setSudokuImplementation(sudokuEngine);
        sudokuView.setVisible(true);
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(new RunSudokuNinja());
    }
}

public class SudokuView extends JFrame{
    //SudokuController controller;
    //SudokuEngine sudokuEngine;

    private static final int width = 500;
    private static final int height = 650;
    private static final int yMar = 100;
    private static final int xMar = 25;
    private static final int cellSize = 40;
    private static final int gridSpace = 3;
    private static final int secSpace = 9;

    public JButton[][] btnz = new JButton[9][9];

    public SudokuView() {
        //controller = new SudokuController();
        //sudokuEngine = new SudokuEngine();

        setTitle("Sudoku Ninja 0.1");
        setSize(width, height);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);
        setBackground(Color.GRAY);

        int y=yMar;
        for (int r=0;r<9;r++){
            int x=xMar;
            for (int c=0;c<9;c++){
                btnz[r][c]=new JButton("1");
                btnz[r][c].setBounds(x,y,cellSize,cellSize);
                btnz[r][c].setVisible(true);
                add(btnz[r][c]);
                if (c==2||c==5){
                    x = x+cellSize+secSpace;
                }else{
                    x = x+cellSize+gridSpace;
                }
            }
            if (r==2||r==5){
                y = y+cellSize+secSpace;
            }else{
                y = y+cellSize+gridSpace;
            }
        }
        setVisible(true);
    }

    @Override
    public void paint(Graphics g){

    }


    public void setSudokuImplementation(SudokuEngineInterface listener) {
        //controller.setListener(listener);
    }
}

最佳答案

您正在覆盖paint方法,告诉它什么也不做,将您的paint()更正为:

public void paint(Graphics g){
    super.paint(g);
    //do whatever you need to do here
}

07-26 02:24