用于一元运算符感叹号的错误操作数类型actionlistener

时间:2015-06-29 23:24:08

标签: java netbeans operators actionlistener

我为一元运算符得到了一个"糟糕的操作数类型actionlistener!"第2行的错误,以及错误"不兼容的类型:布尔值无法在第2行和第8行转换为ActionListener。我在.start()的第5行也有错误,指出符号start不能找到。有谁知道这里出了什么问题?

private void walkjButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    if(!walk){ //error here
        walk = true; //here
        timer = new Timer(40, walk);
        walk.start(); //here
    }
    else{
        timer.stop();
        walk = false; //and here      
    }
}                                           

private ActionListener walk = new ActionListener(){
    public void actionPerformed(ActionEvent e){
        if(leftLegWalk){
            while (leftLegLength <=50){
                leftLegLength -= 5;
                }
            while (leftLegLength >=20) {
                    leftLegLength +=5;
                }
            }
        else {
            while (rightLegLength <=50) {
                rightLegLength -=5;
                }
            while (rightLegLength >=20) {
                rightLegLength += 5;
                }
            }
        }
    };

boolean running = false;
int count = 0; 
boolean wave = true;
boolean leftLegWalk = true;
boolean rightLegWalk = false;
int leftLegLength = 50;
int rightLegLength = 50;
private Timer timer;

1 个答案:

答案 0 :(得分:3)

你正在混淆你的变量。变量walkActionListener。您不能将其视为boolean(使用if测试其真值,分配truefalse)或Thread(调用start() })。很难确定你想要完成什么,但我认为你想要的是:

private void walkjButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    if(!running){
        running = true;
        timer = new Timer(40, walk);
        timer.start();
    }
    else{
        timer.stop();
        running = false;
    }
}