【问题标题】:Sudoku GUI using java数独 GUI 使用 java
【发布时间】:2012-03-16 23:37:58
【问题描述】:

我目前正在为我正在制作的这个数独求解器开发我的 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]);

        }
    }

【问题讨论】:

标签: java swing user-interface sudoku solver


【解决方案1】:

到目前为止,最简单的方法是将九个 3x3 JPanels 的 JLabels 嵌套到一个大的 3x3 JPanelJPanels 中。然后你可以对小 3x3 应用特殊边框。

【讨论】:

  • 还是不明白我是怎么做到的。我在网上查过,我能找到的只有命令提示符板。他们似乎使用模 3 语句来使它们更加突出。 JLabels 没有类似的方法可以更轻松、更短地执行吗?
【解决方案2】:

如果您创建自己的自定义 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);
...

【讨论】:

    【解决方案3】:

    这是来自 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);
    }
    }
    

    如果一切都搞砸了,就无法在评论中发布。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      相关资源
      最近更新 更多