一元运算符感叹号的错误操作数类型 actionlistener
bad operand type actionlistener for unary operator exclamation mark
我在第 2 行收到 "bad operand type actionlistener for unary operator !" 错误,在第 2 行和第 8 行收到错误“不兼容的类型:布尔值无法转换为 ActionListener。我在 . 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;
您混淆了变量。变量 walk
是一个 ActionListener
。您不能将其视为 boolean
(使用 [=14= 测试其真值],分配 true
或 false
)或作为 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;
}
}
我在第 2 行收到 "bad operand type actionlistener for unary operator !" 错误,在第 2 行和第 8 行收到错误“不兼容的类型:布尔值无法转换为 ActionListener。我在 . 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;
您混淆了变量。变量 walk
是一个 ActionListener
。您不能将其视为 boolean
(使用 [=14= 测试其真值],分配 true
或 false
)或作为 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;
}
}