Java JButtons和2darry出现问题
我想做一个类似扫雷的游戏。我的钮扣有问题。 因此,我尝试将jbutton添加到一个二维数组中,将它们对齐到一个与行和列相等的网格中,然后将它们添加到一个面板中,并将面板添加到jframe中,尝试在另一个类中调用它,除了它什么都不做之外Java JButtons和2darry出现问题,java,swing,Java,Swing,我想做一个类似扫雷的游戏。我的钮扣有问题。 因此,我尝试将jbutton添加到一个二维数组中,将它们对齐到一个与行和列相等的网格中,然后将它们添加到一个面板中,并将面板添加到jframe中,尝试在另一个类中调用它,除了它什么都不做之外 public class Field{ private int row_value; private int column_value; private JButton board[][]; private JPanel panel; private Grid
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);
}
大型机从何而来。所有这些都必须在事件调度线程EDT上运行,因此请确保这一点。2.Cell必须实现ActionListener才能编译它。3.您需要在某个时候调用frame.pack并将其设置为visibleNo无需调用panel.setVisibletrue,只需调用mainframe.setVisibletrue即可。@PM 77-1 mainframe是扩展JFrame的另一个类,我在这里创建了我的框架,我只是在测试是否以这种方式添加它..为了尽快获得更好的帮助,发布一个最小的完整的可验证示例。