准备自己做个坦克大战的小游戏试试,有个疑问是:我主窗口是一个类,坦克是一个类,我在主窗口里面建了个键盘监听器,主要是监听按下哪个键,控制坦克的移动。坦克类里面我建好了移动的方法。,但是现在我不知道监听器里面要怎么做,才可以把坦克的移动方法,加入监听器里面去。
代码如下:
主窗口:
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:
}
}
}请问一下高手,我要怎么解决???
代码如下:
主窗口:
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:
}
}
}请问一下高手,我要怎么解决???
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();
}
}
}
}
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); 这里会报错。
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();
}
}
}}
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();
}
}
} }
那个KeyListener 是abstract的类,在里面可以引用tank的方法吗?
我看报错的意思,好像是引用了一个非最终类的方法。
我按照你的方法修改后,在tank.Draw的地方会报错:Cannot make a static reference to the non-static field tank