5

私は現在、作成中のこの数独ソルバーの GUI に取り組んでいます。問題なくボードを印刷できました。ただし、3x3 領域を太い線や色付きの線で区別する方法を知りたいです。

基本的に下の写真に似たもの。

数独

以下は、私がすでに実装したコードです。ありがとう!

    Board = new JPanel(new GridLayout(9, 9));
    for(int i= 0; i < 9; i++) {

        for(int j = 0; j < 9; j++) {

            board[i][j] = new JLabel();

            board[i][j].setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));


            Font font = new Font("Arial", Font.PLAIN, 20);

            board[i][j].setFont(font);

            board[i][j].setForeground(Color.WHITE);

            board[i][j].setBackground(Color.WHITE);


            board[i][j].setOpaque(true);

            board[i][j].setHorizontalAlignment(JTextField.CENTER);

            Board.add(board[i][j]);

        }
    }
4

3 に答える 3

6

最も簡単な方法は、1 つの大きな 3x3 の にネストされた 9 つの 3x3 の を使用することJPanelです。次に、小さな 3x3 に特別な境界線を適用するだけです。JLabelJPanelJPanel

于 2012-03-16T23:46:27.810 に答える
4

数字を保持して黒い境界線を描画する独自のカスタム 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);
...
于 2012-03-17T00:46:08.203 に答える
1

Yahoo Answers のサンプル コードを次に示します。JPanel に JPanel を追加する方法について

    import javax.swing.*;

public class RecursiveJPanelTest
{
public static void main(String[] arg)
{
JFrame window = new JFrame();
JPanel top = new JPanel();
JPanel within = new JPanel();

window. setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);

window.setSize(400, 200);
top.setSize(400, 200);
within.setSize(400, 200);

window.add(top);
top.add(within);
within.add(new JButton("Button"));

window.validate();
window.setVisible(true);
top.setVisible(true);
within.setVisible(true);
}
}

すべてを台無しにすることなく、これをコメントに投稿できませんでした。

于 2012-03-17T00:25:35.060 に答える