gpt4 book ai didi

java - JButton 数组返回 void

转载 作者:行者123 更新时间:2023-12-01 06:44:44 26 4
gpt4 key购买 nike

我正在尝试创建扫雷,但我很早就陷入了困境,因为我的 JButton 数组返回 void 而不是 JButton,因此我无法对其执行任何操作。

代码如下:(当我想删除按钮时,错误发生在最后一行)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Game extends JFrame implements ActionListener
{
JButton[][] buttons;
int rows;
int cols;
int x;
int y;

public Game(int rows, int cols)
{
setTitle("Minesweeper");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.rows = rows;
this.cols = cols;
setLayout(new GridLayout(rows, cols));
buttons(rows, cols);
}
public void buttons(int tableX, int tableY)
{

buttons = new JButton[tableX][tableY];

for (x = 0; x < tableX; x++)
{
for (y = 0; y < tableY; y++)
{
buttons[x][y] = new JButton();
buttons[x][y].setActionCommand("Pressed");
buttons[x][y].addActionListener(this);
this.add(buttons[x][y]);
}
}
}
public void actionPerformed(ActionEvent e)
{
for (x = 0; x < rows; x++)
{
for (y = 0; y < cols; y++)
{
if (e.getActionCommand().equals("Pressed"))
{
buttons[x][y] = setVisible(false);
}
}
}
}
}

最佳答案

使用 . 而不是 = 来调用方法

buttons[x][y].setVisible(false);

关于java - JButton 数组返回 void,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18592947/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com