weixin_43436017 2018-10-16 14:59 采纳率: 33.3%
浏览 605
已结题

Java题目,求大神帮忙看看怎么修改,一直有两处错误?

package tf1703;

import java.awt.Color;
import java.awt.Graphics;

//import Tank.Direction;

public class Tank {
public int x,y,w,h;

Enum Direction{L,LU,U,RU,R,RD,D,LD,STOP};
public Direction dir=Direction.U;

public Tank(int x,int y,int w,int h,Direction dir) {
    this.x=x;
    this.y=y;
    this.w=w;
    this.h=h;
    this.dir=dir;
}

public void draw(Graphics g) {
    g.setColor(Color.green);
    g.fillOval(x,y,w,h);
    switch(this.dir) {
    case U:
        g.setColor(Color.black);
        g.drawLine(x+w/2, y+h/2, x+w/2, y-10);
        break;
    case LU:
        g.setColor(Color.black);
        g.drawLine(x+w/2, y+h/2, x, y);
        break;
    case L:
        g.setColor(Color.black);
        g.drawLine(x+w/2, y+h/2, x-10, y+h/2);
        break;
    case LD:
        g.setColor(Color.black);
        g.drawLine(x+w/2, y+h/2, x, y+h);
        break;
    case D:
        g.setColor(Color.black);
        g.drawLine(x+w/2, y+h/2, x+w/2, y+h+10);
        break;
    case RD:
        g.setColor(Color.black);
        g.drawLine(x+w/2, y+h/2, x+w, y+h);
        break;
    case R:
        g.setColor(Color.black);
        g.drawLine(x+w/2, y+h/2, x+w+10, y+h/2);
        break;
    case RU:
        g.setColor(Color.black);
        g.drawLine(x+w/2, y+h/2, x+w, y);
        break;
    }
}

}

package tf1703;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import tf1703.Tank.Direction;

public class TFrame extends Frame{
public int frameX=100;
public int frameY=100;
public int frameH=800;
public int frameW=1200;

public Tank myTank=null;

public TFrame() {
    this.setBounds(frameX,frameY,frameW,frameH);
    this.setBackground(Color.pink);

    this.myTank=new Tank(200,200,100,100,Tank.Direction.RU);

    this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(-1);
        }
    });

    this.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {

            if(e.getKeyCode()==KeyEvent.VK_A) {
                System.out.println("你按了A");
                myTank.dir=Direction.L;
                myTank.x=myTank.x-10;

            }else if(e.getKeyCode()==KeyEvent.VK_S) {
                System.out.println("你按了S");
                myTank.dir=Direction.D;
                myTank.y=myTank.y+10;
            }
            else if(e.getKeyCode()==KeyEvent.VK_D) {
                System.out.println("你按了D");
                myTank.dir=Direction.R;
                myTank.x=myTank.x+10;
            }

            else if(e.getKeyCode()==KeyEvent.VK_W) {
                System.out.println("你按了W");
                myTank.dir=Direction.U;
                myTank.y=myTank.y-10;
            }
            repaint();
        }

    });



    this.setVisible(true);

}

public void paint(Graphics g) {
    this.myTank.draw(g);


}

public static void main(String[] args) {

    new TFrame();

}

}

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The import tf1703.Tank.Direction cannot be resolved
Cannot make a static reference to the non-static field Tank.Direction
Direction cannot be resolved to a type
Direction cannot be resolved to a variable
Direction cannot be resolved to a type
Direction cannot be resolved to a variable
Direction cannot be resolved to a type
Direction cannot be resolved to a variable
Direction cannot be resolved to a type
Direction cannot be resolved to a variable

at tf1703.TFrame.<init>(TFrame.java:9)
at tf1703.TFrame.main(TFrame.java:86)
  • 写回答

3条回答

  • michaelbournelisihao 2018-10-16 15:17
    关注

    在Tank.Direction的定义前添加static,如:
    static Enum Direction{L,LU,U,RU,R,RD,D,LD,STOP};

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?