Кнопки J не появятся, пока я не наведу на них курсор мыши - PullRequest
0 голосов
/ 28 марта 2020

У меня проблема с сеткой, которую я создал. кнопки этого smallerGrid не появятся, пока я не наведу на них курсор мыши. каждый smallerGrid состоит из сетки 3x3, которая вписывается в сетку biggerGrid также 3x3, которая содержит меньшие сетки. это краткое объяснение того, как я строю сетку судоку. Вы найдете код ниже. Заранее спасибо.

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


public class MyGridLayout {
    private int filledFields = 0;
    private JFrame mainFrame;
    private JPanel panelForSolvingButton;
    private JPanel smallerGridPanel;
    private boolean solvingButtonAppeared = false;
    SudokuCell[][] biggerGrid = new SudokuCell[10][10];


    MyGridLayout() {
        mainFrame = new JFrame("sudoku-solver");
        mainFrame.setLayout(new BorderLayout());

        smallerGridPanel = new JPanel(new GridLayout(3, 3));
        mainFrame.add(smallerGridPanel, BorderLayout.CENTER);

        panelForSolvingButton = new JPanel();
        panelForSolvingButton.setLayout(new FlowLayout(FlowLayout.RIGHT));
        mainFrame.add(panelForSolvingButton, BorderLayout.SOUTH);

        mainFrame.pack();
        mainFrame.setSize(600,600);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        mainFrame.setResizable(false);

        addRegularButtons();
    }

    void addRegularButtons() {
        int currentCol = 0;
        int currentRow = 0;
        int smallerGridFirstCol = 0;
        int firstRowOfLayer = 0;

        for (int layerOfGrids = 0; layerOfGrids < 3; ++layerOfGrids) {
            for (int gridOfLayer = 0; gridOfLayer < 3; ++gridOfLayer) {
                JComponent smallerGrid = new JPanel(new GridLayout(3, 3));
                smallerGrid.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                smallerGridPanel.add(smallerGrid);

                for (int j = 0; j < 3; ++j) {
                    for (int k = 0; k < 3; ++k) {
                        var cell = new SudokuCell(currentRow, currentCol);
                        smallerGrid.add(cell);
                        biggerGrid[currentRow][currentCol++] = cell;
                        cell.addActionListener(new CellActionListener(this));
                    }
                    currentRow++;
                    currentCol = smallerGridFirstCol;
                }
                smallerGridPanel.revalidate();

                currentRow = firstRowOfLayer;
                smallerGridFirstCol += 3;
                currentCol = smallerGridFirstCol;
            }

            firstRowOfLayer += 3;
            currentRow = firstRowOfLayer;
            smallerGridFirstCol = currentCol = 0;
        }
        mainFrame.revalidate();
        mainFrame.setVisible(true);
    }


    // checking if the solving process can begin
    // (filled fields must at least reach 17)
    void makeSolveButtonAppear() {
        JButton solveButton = new JButton();
        solveButton.setBackground(Color.white);
        solveButton.setFont(new Font("Arial", Font.PLAIN, 20));
        solveButton.setOpaque(false);
        solveButton.setText("Solve ?");
        panelForSolvingButton.add(solveButton);
    }

    public boolean isSolvingButtonAppeared() {
        return solvingButtonAppeared;
    }

    public void setSolvingButtonAppeared(boolean solvingButtonAppeared) {
        this.solvingButtonAppeared = solvingButtonAppeared;
    }

    public int getFilledFields() {
        return filledFields;
    }

    public void setFilledFields(int filledFields) {
        this.filledFields = filledFields;
    }

    public JPanel getPanelForSolvingButton() {
        return panelForSolvingButton;
    }

    public static void main(String[] args) {
        var gridLayout = new MyGridLayout();
    }
}

package mainFrame;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;

public class SudokuCell extends JButton {
    private int x;
    private int y;

    public SudokuCell(int x, int y) {
        this.x = x;
        this.y = y;
        setBackground(Color.white);
        setOpaque(true);
        setFont(new Font("Arial", Font.PLAIN, 20));
    }

    @Override
    public int getX() {
        return x;
    }

    @Override
    public int getY() {
        return y;
    }
}

package mainFrame;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CellActionListener implements ActionListener {
    private int clicked = 0;
    MyGridLayout currentGrid;

    CellActionListener(MyGridLayout currentGrid) {
        this.currentGrid = currentGrid;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(clicked ==0) currentGrid.setFilledFields(currentGrid.getFilledFields()+1);

        //clicked (number of clicks) must remain between 0 and 9
        clicked = (clicked % 9) + 1;
        ((JButton) e.getSource()).setText(Integer.toString(clicked));

        //first click on the button means an entry has been made
        //which means one less needed filledField out of 17
        if(!currentGrid.isSolvingButtonAppeared() && currentGrid.getFilledFields() >= 17) {
            currentGrid.makeSolveButtonAppear();
            currentGrid.setSolvingButtonAppeared(true);
        }
    }
}```
...