java - 数独板的 JComponent 构建

标签 java swing jframe jcomponent sudoku

我正在尝试构建一个数独板。现在我只是想让板子画出来,我试过只画线,但被告知这样更好......我还没有让它工作。关于我做错了什么的任何提示

import java.awt.Component;
import java.awt.GridLayout;

import javax.swing.*;

public class SudokuView4 extends JPanel {
    int rows = 3;
    int col = 3;
    public JPanel container = new JPanel(new GridLayout(rows*col,rows*col));

    public SudokuView4(SudokuBase sb) {
        // TODO Auto-generated constructor stub

        for(int r = 0; r < rows; r++){
            for(int c = 0; c < col; c++){
                //container.add(Region(rows,col));
                //add(build);
                //build.setSize(50, 50)
                Region();
                container.setVisible(true);
            }
        }
    }

    //class Region extends JPanel {

    public void Region( ) {
        //setLayout(new GridLayout(3,3));
        //JPanel grid = new JPanel(new GridLayout(3,3));
        //grid.setSize(50, 50);

        for(int r1 = 0; r1 < rows; r1++){
            for(int c1 = 0; c1 < col; c1++){
                //JPanel grid = new JPanel();
                JButton build = new JButton();

                container.add(build);
                //container.setVisible(true);               
            }
        }
    }
}

最佳答案

JPanel 容器需要放置在 JFrame 内部,并在添加按钮后进行更新。如果您的目标是运行 Java 应用程序,则为这种情况。

import java.awt.Component;
import java.awt.GridLayout;

import javax.swing.*;

public class SudokuView4 extends JPanel {
int rows = 3;
int col = 3;
public JPanel container = new JPanel(new GridLayout(rows*col,rows*col));

// added main for testing
public static void main(String [] args){
    SudokuView4 sudoku = new SudokuView4();
}

public SudokuView4(/*SudokuBase sb*/) {
    // TODO Auto-generated constructor stub
    JFrame frame = new JFrame();
    frame.add(container);


    for(int r = 0; r < rows; r++){
        for(int c = 0; c < col; c++){
            //container.add(Region(rows,col));
            //add(build);
            //build.setSize(50, 50)
            Region();
            container.setVisible(true);
        }
    }

    frame.setSize(300,300);
    frame.setVisible(true);
}

//class Region extends JPanel {

public void Region( ) {
    //setLayout(new GridLayout(3,3));
    //JPanel grid = new JPanel(new GridLayout(3,3));
    //grid.setSize(50, 50);

    for(int r1 = 0; r1 < rows; r1++){
        for(int c1 = 0; c1 < col; c1++){
            //JPanel grid = new JPanel();
            JButton build = new JButton();

            container.add(build);
            //container.setVisible(true);               
        }
    }
}
}

关于java - 数独板的 JComponent 构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10289763/

相关文章:

java - 打印文件列表

Java,创建并修改作为参数传递给新对象的构造函数的对象

java - JLabel 内的两行文本

java - 将 JText 字段值绑定(bind)到 Info 类

java - 将文本插入 Quartz Schedule 上的 Swing 文本区域

java - 如何在 Java 中仅显示字符串几秒钟?

java - JMS中上下文的目的是什么?

java - Fragment 中的 findViewById 出现 NullPointerException

java - 如何将动画实现到 JFrame 中

java - 为什么在 Frame 中看不到我的 Jtable?