gpt4 book ai didi

java - 当我使用 : int cannot be converted to String, this 时出现错误 : timer = new Timer(1000,);

转载 作者:行者123 更新时间:2023-12-01 22:38:23 25 4
gpt4 key购买 nike

我制作了一个计时器,通过timer = new Timer(1000, this);声明(完整代码见下文)。但我收到错误:错误:不兼容的类型:int 无法转换为 java.lang.String。这是否意味着编译器认为“this”是一个字符串或者..?我应该怎么做才能修复这个错误?

import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.JFrame;

public class Snake extends JFrame implements KeyListener, ActionListener{
/*Painter painter;
LinkedList<Point> snakeList;*/
Timer timer;
/*int direction;
int snakeSize;
int movementX, movementY;*/

public static void main(String[] arg){
new Snake();
}

public Snake(){
/*painter = new Painter(this);
this.add(painter, BorderLayout.CENTER);
this.setSize(500, 500);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.addKeyListener(this);
this.requestFocusInWindow();*/
timer = new Timer(1000, this);
/*startGame();*/
}

/*public void startGame(){
snakeList = new LinkedList<Point>();
snakeList.addFirst(new Point(30,30));
snakeSegments(3);
movementX = 0;
movementY = 0;
}

public void gameUpdate(){
snakeMove(movementX, movementY);
repaint();
}

public void snakeSegments(int i){
snakeSize = getSnakeSize();
while(snakeSize > 0){
snakeList.addLast(new Point(getLast()));
snakeSize--;
}
}

public void snakeInstructor(){
int currentDirection = getDirection();
if (currentDirection == 1){
snakeMove(-1, 0);
} else if (currentDirection == 2){
snakeMove(1, 0);
} else if (currentDirection == 3){
snakeMove(0, 1);
} else if (currentDirection == 4){
snakeMove(0, -1);
}
}

public void snakeMove(int directionX, int directionY){
snakeList.getFirst().x = snakeList.getFirst().x + directionX;
snakeList.getFirst().y = snakeList.getFirst().y + directionY;
for(int i = getSnakeSize()-1; i >=1; i--) {
snakeList.get(i).setLocation(snakeList.get(i-1));
}
}

public void setDirection(int newDirection){
direction = newDirection;
}

public int getDirection (){
return direction;
}

public boolean isEmpty(){
return true;
}

Point getFirst(){
return snakeList.getFirst();
}

Point getLast(){
return snakeList.getLast();
}

Point get(int i){
return snakeList.get(i);
}

public void addFirst(Point p){
snakeList.addFirst(p);
}

public void addLast(Point p){
snakeList.addLast(p);
}

public Point removeFirst(){
snakeList.removeFirst();
return null;
}

public Point removeLast(){
snakeList.removeLast();
return null;
}

public int getSnakeSize(){
return snakeList.size();
}

public void setSnakeSize(){
snakeSize = snakeList.size() + 1;
}*/

@Override
public void actionPerformed(ActionEvent event) {
gameUpdate();
}

/*@Override public void keyReleased(KeyEvent e){ }
@Override public void keyTyped(KeyEvent e){ }
@Override public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if((key == KeyEvent.VK_LEFT) && direction != 2){
setDirection(1);
} else if ((key == KeyEvent.VK_RIGHT) && direction != 1){
setDirection(2);
} else if ((key == KeyEvent.VK_UP) && direction != 4){
setDirection(3);
} else if ((key == KeyEvent.VK_DOWN) && direction != 3){
setDirection(4);
} else if (key == KeyEvent.VK_SPACE){
startGame();
}
}*/

}

最佳答案

您已经导入了java.util.Timer,但您似乎想要使用javax.swing.Timer

关于java - 当我使用 : int cannot be converted to String, this 时出现错误 : timer = new Timer(1000,);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26556399/

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