我有三个不同的类来处理我的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。 Sidebarclass:
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类就是问题所在,因为我在单击JButton时设法写入终端,但没有写入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);
}
}
}
}
我对GUI很新,所以这似乎是一个容易解决的问题,但我现在已经拉了一段时间...... 正如我所说,我可以将nextBoard(Square [] [] sudokuboard)写入终端,但到目前为止还没有到GUI屏幕。谁能解释一下我在这里做错了什么?