【问题标题】:Error: int cannot be converted to String, when I use: timer = new Timer(1000, this);错误:int 无法转换为 String,当我使用:timer = new Timer(1000, this);
【发布时间】:2014-10-24 21:17:15
【问题描述】:

我做了一个计时器,我通过 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.utiljavax.swing 等)存在于 Java 等语言中的原因。因为,随着越来越多的库和 API 发布,两个(不同且可能不相关的)类必须共享相同名称的可能性增加。命名空间是区分Timer 类和我的Timer 类、Window 类和我的Window 类等的一种方式。

标签: java


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-12-09
    • 2011-04-11
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多