需要帮助在 JFrame 中排列 JTextField

发布于 2024-12-29 13:46:19 字数 400 浏览 5 评论 0原文

我正在用java制作一个数独程序来学习一些算法,所以我希望用户能够输入一个未解决的数独谜题。到目前为止,我创建了 81 (9x9) 个框:

JTextField input[] = new JTextField[80];
for(int i = 0; i <= 79; i++)
{   
    input[i] = new JTextField();
    input[i].setPreferredSize(new Dimension(30,30));
    f.getContentPane().add(input[i]);
}

当我运行这个程序时,我得到的只是一个输入字段。我知道所有文本字段都已初始化、创建并添加到 jframe 中。我知道你必须搞乱布局,但我不知道该怎么做。任何帮助都是适当的。

I'm making a Sudoku program in java to learn some algorithms so I want the user to be able to input an unsolved Sudoku puzzle. Here is what I have so far that creates 81 (9x9) boxes:

JTextField input[] = new JTextField[80];
for(int i = 0; i <= 79; i++)
{   
    input[i] = new JTextField();
    input[i].setPreferredSize(new Dimension(30,30));
    f.getContentPane().add(input[i]);
}

When I run this program though all I get is just one input field. I know that all the text fields and initialized, created and added to the jframe. I know you have to mess this the layout but I'm not sure how to do that. Any help is appropriated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心是晴朗的。 2025-01-05 13:46:19

JPanel网格布局

另外:

JTextField input[] = new JTextField[80];

这是 80 个(不是 81 个)文本字段。

更新:(示例代码)

public class SodokuBoardDemo {

    public static void main(String... args) {
        SudokuBoard board = new SudokuBoard();    
        JFrame frame = new JFrame("Sodoku");
        frame.add(board);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);            
        frame.setVisible(true);
    }

    public static class SudokuBoard extends JPanel {

        public SudokuBoard() {
            setBorder(BorderFactory.createLineBorder(Color.GRAY));
            setLayout(new GridLayout(3, 3));
            BoardPart input[] = new BoardPart[9];
            for (int i = 0; i < 9; i++) {
                input[i] = new BoardPart();
                add(input[i]);
            }
        }

        public static class BoardPart extends JPanel {

            public BoardPart() {
                setBorder(BorderFactory.createLineBorder(Color.GRAY));
                setLayout(new GridLayout(3, 3));
                JTextField input[] = new JTextField[9];
                for (int i = 0; i < 9; i++) {
                    input[i] = new JTextField();
                    input[i].setPreferredSize(new Dimension(30, 30));
                    add(input[i]);
                }
            }
        }
    }
}

Use a JPanel with GridLayout.

Also:

JTextField input[] = new JTextField[80];

That's 80 (not 81) text fields.

Update: (sample code)

public class SodokuBoardDemo {

    public static void main(String... args) {
        SudokuBoard board = new SudokuBoard();    
        JFrame frame = new JFrame("Sodoku");
        frame.add(board);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);            
        frame.setVisible(true);
    }

    public static class SudokuBoard extends JPanel {

        public SudokuBoard() {
            setBorder(BorderFactory.createLineBorder(Color.GRAY));
            setLayout(new GridLayout(3, 3));
            BoardPart input[] = new BoardPart[9];
            for (int i = 0; i < 9; i++) {
                input[i] = new BoardPart();
                add(input[i]);
            }
        }

        public static class BoardPart extends JPanel {

            public BoardPart() {
                setBorder(BorderFactory.createLineBorder(Color.GRAY));
                setLayout(new GridLayout(3, 3));
                JTextField input[] = new JTextField[9];
                for (int i = 0; i < 9; i++) {
                    input[i] = new JTextField();
                    input[i].setPreferredSize(new Dimension(30, 30));
                    add(input[i]);
                }
            }
        }
    }
}
貪欢 2025-01-05 13:46:19

如果您不确定如何使用不同的 Layout,可以使用 有关 Oracle 文档的精彩教程。如果您想温习组件本身,您还可以查看教程 关于他们。 :)

ps: 可能是我太困了,但看起来你已经创建了 80 个文本字段而不是 81 个。

If you are unsure about how to use different Layouts there is a great tutorial on Oracle documents. If you want to brush up on the components themselves you could also check out the tutorial on them. :)

ps: It might be me being too sleepy but it would appear that you have created 80 text fields not 81.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文