gpt4 book ai didi

java - 调用扩展 JButton 类中的方法时找不到符号错误

转载 作者:行者123 更新时间:2023-12-02 05:07:07 24 4
gpt4 key购买 nike

我需要一些帮助来解决这个问题。我似乎无法弄清楚为什么它不起作用。我想向 ButtonDefault 类添加更多方法,该类是 JButton 的子类,但我不断收到“符号未找到”错误。

下面的代码,ButtonDefault 的实例化有效,默认颜色和操作按钮返回并且工作正常。然而,所有这些代码都在 ButtonDefault 的构造函数中。我想向 ButtonDefault 类添加其他方法,以便稍后我可以更改按钮的颜色按钮之类的。

我不断收到的错误:
MineSweeper.java:50: 找不到符号 符号:方法 setUpButton() 位置:类 javax.swing.JButton 按钮[i][j].setUpButton();

我有两个类。

//MineSweeper.java
public class MineSweeper extends JFrame implements ActionListener,MouseListener,ItemListener {
static JButton[][] button = new ButtonDefault[17][31];

public void tileSetup()
{
button[i][j] = new ButtonDefault(i,j, this); //This work just fine
//These work too - I just don't want it here.
//button[i][j].setIcon(null);
//button[i][j].setBorder(UIManager.getBorder("Button.border"));
//button[i][j].setBackground(null);
//button[i][j].setBackground(Color.BLUE);
//button[i][j].setBorder(new LineBorder(Color.GRAY, 1));
//button[i][j].setEnabled(true);
button[i][j].setUpButton(); //This don't work.
}
}

//ButtonDefault.java
public class ButtonDefault extends JButton implements ActionListener,MouseListener,ItemListener
{
public ButtonDefault(){};
public ButtonDefault(int x, int y, final MineSweeper mineObject)
{
this.setPreferredSize(new Dimension(18,18));
this.setBackground(Color.BLUE);

addMouseListener(new MouseListener() {
//All the code in here work just fine.
}

public void setUpButton()
{
this.setIcon(null);
this.setBorder(UIManager.getBorder("Button.border"));
this.setBackground(null);
this.setBackground(Color.BLUE);
this.setBorder(new LineBorder(Color.GRAY, 1));
this.setEnabled(true);
}

}

最佳答案

setUpButtonButtonDefault 的自定义方法,而不是 JButton。因此数组声明应该是

private ButtonDefault[][] buttons = new ButtonDefault[ROWS][COLUMNS];

注释:

  • 通常使用 static 声明表示设计较差,因此请改用实例字段
  • 魔数(Magic Number)也被认为是糟糕的设计,至少考虑使用常量

关于java - 调用扩展 JButton 类中的方法时找不到符号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27693709/

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