准备自己做个坦克大战的小游戏试试,有个疑问是:我主窗口是一个类,坦克是一个类,我在主窗口里面建了个键盘监听器,主要是监听按下哪个键,控制坦克的移动。坦克类里面我建好了移动的方法。,但是现在我不知道监听器里面要怎么做,才可以把坦克的移动方法,加入监听器里面去。
代码如下:
主窗口:
package main;import com.Tank;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.events.KeyEvent;public class MainFrame {
public static void main(String[] args) {
Display display = Display.getDefault();
 Shell shell = new Shell(display);
shell.setText("Tank");
shell.setSize(800, 600);
shell.setBounds(200, 150, 800, 600);
shell.open(); Color c = display.getSystemColor(SWT.COLOR_RED);
Tank tank = new Tank(50, 50, c);
tank.Draw(new GC(shell)); shell.addKeyListener(new KeyAdapter(KeyEvent e){//疑问在这里,这里会报错
tank.KeyPressed(e);//
});
//shell.addKeyListener(new KeyAdapter(tank.KeyPressed(e)));//e从哪里来??? while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}}
我坦克的类,代码如下:
package com;import org.eclipse.swt.graphics.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.internal.win32.*;public class Tank {
int x, y;
Color c; public Tank(int x, int y, Color c) {
this.c = c;
this.x = x;
this.y = y;
} public void Draw(GC g) {
g.setBackground(c);
g.fillOval(x, y, 30, 30);
} public void KeyPressed(KeyEvent e) {
int code = e.keyCode; switch (code) {
case OS.VK_UP:
y -= 5;
break;
case OS.VK_DOWN:
y += 5;
break;
case OS.VK_RIGHT:
x += 5;
break;
case OS.VK_LEFT:
y -= 5;
break;
default:
}
}
}请问一下高手,我要怎么解决???

解决方案 »

  1.   

    你去看看Java的事件处理机制就知道了。你试试导入这个包看看:import java.awt.event.KeyEvent;
      

  2.   

    你发帖的时候,点字体颜色图标右边那个图标(有一个#符号的那个图标),然后选择Java,再把代码贴在中间就好了。
      

  3.   

    我用的是SWT,有导入org.eclipse.swt.events.KeyEvent。刚在论坛里面有看到怎么贴代码,不过我用Chrome,没有#可以选择,呵呵,下次发代码的时候,用IE 好了。
      

  4.   

    我按照我的理解改了下,你看看吧。
    public class MainFrame{ 
    public static void main(String[] args) { 
    Display display = Display.getDefault(); 

    Shell shell = new Shell(display); 
    shell.setText("Tank"); 
    shell.setSize(800, 600); 
    shell.setBounds(200, 150, 800, 600); 
    shell.open();  Color c = display.getSystemColor(SWT.COLOR_RED); 
    Tank tank = new Tank(50, 50, c); 
    tank.Draw(new GC(shell));  shell.addKeyListener(
    new KeyListener(){
    public void keyPressed(KeyEvent e) {
    //你想要实现的功能
    } public void keyReleased(KeyEvent e) {
    //你想要实现的功能
    }
    }
    );   while (!shell.isDisposed()) { 
    if (!display.readAndDispatch()) { 
    display.sleep(); 


    }
      

  5.   

    我有试过,会报Cannot refer to a non-final variable tank inside an inner class defined in a different method 的错误。
      

  6.   


    package main;import com.Tank;import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor;
    import org.eclipse.osgi.framework.internal.core.Framework;
    import org.eclipse.swt.*;
    import org.eclipse.swt.widgets.*;
    import org.eclipse.swt.graphics.*;
    import org.eclipse.swt.events.*;public class MainFrame { public static void main(String[] args) {
    Display display = Display.getDefault();
     Shell shell = new Shell(display);
    shell.setText("Tank");
    shell.setSize(800, 600);
    shell.setBounds(200, 150, 800, 600);
    shell.open(); Color c = display.getSystemColor(SWT.COLOR_RED);
    Tank tank = new Tank(50, 50, c);
    tank.Draw(new GC(shell)); shell.addKeyListener(new KeyAdapter(){
    public void KeyPressed(KeyEvent e){
    tank.KeyPressed(e);//Cannot refer to a non-final variable tank inside an inner class defined in a different method
    }
    });

    while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) {
    display.sleep();
    }
    }
    }}tank.KeyPressed(e); 这里会报错。
      

  7.   

    把你的Tank声明到类里,象这样:package main;import com.Tank;import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor;
    import org.eclipse.osgi.framework.internal.core.Framework;
    import org.eclipse.swt.*;
    import org.eclipse.swt.widgets.*;
    import org.eclipse.swt.graphics.*;
    import org.eclipse.swt.events.*;public class MainFrame {
        Tank tank = new Tank(50, 50, c);
        public static void main(String[] args) {
            Display display = Display.getDefault();
             Shell shell = new Shell(display);
            shell.setText("Tank");
            shell.setSize(800, 600);
            shell.setBounds(200, 150, 800, 600);
            shell.open();        Color c = display.getSystemColor(SWT.COLOR_RED);
            
            tank.Draw(new GC(shell));        shell.addKeyListener(new KeyAdapter(){
                public void KeyPressed(KeyEvent e){
                    tank.KeyPressed(e);//Cannot refer to a non-final variable tank inside an inner class defined in a different method
                }
            });
                    
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        }}
      

  8.   

    package main; import com.Tank; import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor; 
    import org.eclipse.osgi.framework.internal.core.Framework; 
    import org.eclipse.swt.*; 
    import org.eclipse.swt.widgets.*; 
    import org.eclipse.swt.graphics.*; 
    import org.eclipse.swt.events.*; public class MainFrame { 
        Tank tank = new Tank(50, 50, c); 
        public static void main(String[] args) { 
            Display display = Display.getDefault(); 
            Shell shell = new Shell(display); 
            shell.setText("Tank"); 
            shell.setSize(800, 600); 
            shell.setBounds(200, 150, 800, 600); 
            shell.open();         Color c = display.getSystemColor(SWT.COLOR_RED); 
            
            tank.Draw(new GC(shell));         shell.addKeyListener(new KeyAdapter(){ 
                public void KeyPressed(KeyEvent e){ 
                    tank.KeyPressed(e);//Cannot refer to a non-final variable tank inside an inner class defined in a different method 
                } 
            }); 
                    
            while (!shell.isDisposed()) { 
                if (!display.readAndDispatch()) { 
                    display.sleep(); 
                } 
            } 
        } }
      

  9.   


    那个KeyListener 是abstract的类,在里面可以引用tank的方法吗?
    我看报错的意思,好像是引用了一个非最终类的方法。
      

  10.   


    我按照你的方法修改后,在tank.Draw的地方会报错:Cannot make a static reference to the non-static field tank