简体   繁体   English

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

[英]Cannot find symbol error when calling methods in an extended JButton class

I need some help resolving this issue. 我需要一些帮助来解决此问题。 I can't seem to figure out why it wouldn't be working. 我似乎无法弄清楚为什么它不起作用。 I want to add more methods to my ButtonDefault class which is a subclass of JButton but I keep getting "symbol not found" error. 我想向我的ButtonDefault类添加更多方法,该类是JButton的子类,但我不断收到“找不到符号”错误。

The code below, instantiation of the ButtonDefault worked, the default color and action of the button returned and work just fine. 下面的代码可以正常执行ButtonDefault的实例化,按钮的默认颜色和操作,并且可以正常工作。 However all of that code is in the constructor of ButtonDefault. 但是,所有这些代码都在ButtonDefault的构造函数中。 I want to add additional methods to the ButtonDefault class so later on I can change the color of the button and such. 我想向ButtonDefault类添加其他方法,以便稍后可以更改按钮等的颜色。

the error that I keep getting: 我不断得到的错误:
MineSweeper.java:50: cannot find symbol symbol : method setUpButton() location: class javax.swing.JButton button[i][j].setUpButton(); MineSweeper.java:50:找不到符号符号:方法setUpButton()位置:类javax.swing.JButton button [i] [j] .setUpButton();

I have 2 classes. 我有2节课。

//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);
    }

}

setUpButton is a custom method of ButtonDefault not JButton . setUpButtonsetUpButton的自定义方法, ButtonDefault不是JButton Therefore the array declaration should be 因此,数组声明应为

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

Notes: 笔记:

  • Generally using static declarations indicates poor design so use an instance field instead 通常使用static声明表示设计不佳,因此请使用实例字段代替
  • Magic numbers are also perceived as poor design, consider at least using constants 幻数也被认为是不良设计,至少应考虑使用常数

在调用setUpButton之前尝试投射

((ButtonDefault)button[i][j]).setUpButton();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM