简体   繁体   English

从 mousehandler 中调用非静态方法会导致错误:“不能从静态上下文中引用非静态方法

[英]Calling non-static method from within mousehandler results in error:" non static method cannot be referenced from static context

As the title states im having trouble with a method inside a mousehandler.正如标题所述,我在使用鼠标处理程序中的方法时遇到了问题。 My mousehandler is:我的鼠标处理程序是:

public class MouseHandler implements MouseListener{

    @Override
    public void mouseClicked(MouseEvent e){


    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {
            try {
                Pieces.getPos(e, coordinateXYZV, tile,chessBoard);
            }catch (Exception a){

            }
        }
    }

now i know questions about static stuff are really not welcome, but i just cant seem to find the solution although similar problems were easily fixed.现在我知道关于静态内容的问题真的不受欢迎,但我似乎无法找到解决方案,尽管类似的问题很容易解决。 Neither the class with the mousehandler nor the method is static, so why am i getting this error in the first place?带有鼠标处理程序的类和方法都不是静态的,那么为什么我首先会收到此错误? Im creating a new mousehandler like this:我创建了一个像这样的新鼠标处理程序:

MouseHandler mhandler = new MouseHandler();

and then pass it around to whereever its needed.然后将其传递到任何需要的地方。 if anything further is needed please point it out because this is my first time asking a question here and im not entirely sure whats needed to help me here如果还需要什么,请指出,因为这是我第一次在这里提问,我不完全确定需要什么来帮助我

For future people stumbling upon this: call your methods correclty:对于未来遇到此问题的人:请正确调用您的方法:

 @Override
    public void mousePressed(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {
            try {
                Pieces p = new Pieces();                       <--
                p.getPos(e, coordinateXYZV, tile,chessBoard);  <--
            }catch (Exception a){

            }
        }
    }

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

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