我正在尝试制作类似于扫雷车的游戏。我遇到了JButtons的问题。 因此,我试图将JButtons添加到2d数组,将它们排列在等于行和列的网格中,然后将它们添加到面板并将面板添加到jframe,尝试在另一个类中调用它,除了它什么都不做。
public class Field{
private int row_value;
private int column_value;
private JButton board[][];
private JPanel panel;
private GridLayout grid_layout;
//creates the board with the buttons and adds cell to each of the buttons
public Field(){
row_values();
Column_values();
int x = row_value;
int y = column_value;
grid_layout = new GridLayout(x, y);
board = new JButton[x][y];
for(int row = 0; row < board[x].length ;row++){ //stops here
for(int col = 0; col< board[y].length;col++){
board[row][col] = new JButton();
board[row][col].addActionListener(new Cell());//adds action listener to cell?
panel.add(board[row][col]);
}
}
panel.setVisible(true);
mainframe.add(panel);
}