Java 将JTextField分配给GridLayout时出现问题

Java 将JTextField分配给GridLayout时出现问题,java,swing,user-interface,actionlistener,Java,Swing,User Interface,Actionlistener,我有三个不同的类来处理我的GUI组件。我的主GUI类启动一个类,该类创建侧栏,另一个类使用GridLayout保存一个textarea 这是我的主要GUI类 public class Gui extends JFrame { private TextPanel textPanel; private Sidebar sidebar; public Gui() { super("Oblig 5"); setLayout(new Borde

我有三个不同的类来处理我的GUI组件。我的主GUI类启动一个类,该类创建侧栏,另一个类使用GridLayout保存一个textarea

这是我的主要GUI类

public class Gui extends JFrame {

    private TextPanel textPanel;
    private Sidebar sidebar;

    public Gui() {
        super("Oblig 5");

        setLayout(new BorderLayout());

        textPanel = new TextPanel();
        sidebar   = new Sidebar(textPanel);

        add(sidebar, BorderLayout.WEST);
        add(textPanel, BorderLayout.CENTER);
        ...
    }
}
我的边栏类有一个JButton,它将addActionListener()设置为另一个类中的textarea。 侧边栏类:

public class Sidebar extends JPanel {

    private JButton next;
    private TextPanel panel;
    ...

    FormPanel(TextPanel panel) {
            this.panel = panel;
            ...
            next = new JButton("Next");
            next.addActionListener(panel);
            ...
            add(next, BorderLayout.SOUTH);
            ...
    }
}
我相信我的TextPanel类就是问题所在,因为我已经在单击jbuttonnext时设法写入终端,但没有写入GUI屏幕

public class TextPanel extends JPanel implements ActionListener {
    ...

    public TextPanel() {
        setLayout(new GridLayout(emptyBoard.length,emptyBoard.length));
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        nextBoard(container.getBoard());
    }


    public void nextBoard(Square[][] sudokuboard) {
        // I've tried writing out sudokuboard
        // to the terminal, which works fine

        for(int i=0; i<sudokuboard.length; i++) {
            north = 1; west = 1; south = 1; east = 1;
            if(i%rows == 0 && i > 0) {
                    north = 6;
            }

            for(int j=0; j<sudokuboard[i].length; j++) {
                west = 1;
                if(j%columns == 0 && j > 0) {
                    west = 6;
                }

                JTextField square = new JTextField();
                square.setFont(new Font("SansSerif", Font.BOLD, 32));
                square.setText(""+sudokuboard[i][j].getValue());
                square.setEditable(false);
                square.setBorder(BorderFactory.createMatteBorder(north,west,south,east,Color.black));

                if((i/rows + j/columns) % 2 == 0) {
                    square.setBackground(Color.LIGHT_GRAY);
                }

                add(square);
            }
        }
    }
}
公共类TextPanel扩展JPanel实现ActionListener{
...
公共文本面板(){
setLayout(新网格布局(emptyBoard.length,emptyBoard.length));
}
@凌驾
已执行的公共无效操作(操作事件e){
nextBoard(container.getBoard());
}
公共真空下一个板(方形[][]sudokuboard){
//我试过写数独板
//到终点站,效果很好
对于(int i=0;i 0){
北=6;
}
对于(int j=0;j 0){
西=6;
}
JTextField square=新的JTextField();
setFont(新字体(“SansSerif”,Font.BOLD,32));
square.setText(“+sudokuboard[i][j].getValue());
square.setEditable(false);
square.setBorder(BorderFactory.createMatteBorder(北、西、南、东、彩色.黑色));
如果((i/行+j/列)%2==0){
方形。立根背景(颜色。浅灰色);
}
加(正方形);
}
}
}
}
我是GUI新手,所以这似乎是一个很容易解决的问题,但我已经做了一段时间了。。。
正如我所说,我设法将nextBoard(Square[][]sudokuboard)写入终端,但到目前为止还没有写入GUI屏幕。有人能解释一下我在这里做错了什么吗?

为了更快地获得更好的帮助,请发布一个(最简单的完整和可验证的示例)。将组件添加到可见组件中可能有问题(您可以尝试在
nextBoard
方法的末尾添加
revalidate();repaint();
)。另外,请按照AndrewThompson的建议发布MCVE。您从未在
nextBoard(…)
中删除先前添加的组件。似乎这样做会更好,而不是使用
JTextField
s.
“……但是我有15个类需要协同工作,GUI才能工作。我不想吓跑评论!”
--请重新阅读链接。如果你正确地创建了这个东西,你就不会吓跑任何人,因为你发布的代码最多只有20到50行。是的,创造一个这样的人需要付出很多努力,但这是值得的。由你决定。