各位大仙 帮我改改java代码呗 我想要 键盘按上下左右 棋子上下左右 但表格不移动

importjava.awt.*;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;impor... import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;

public class DrawRectTest extends JFrame {
public DrawRectTest() {
GPanel g = new GPanel();
this.getContentPane().add(g);
this.setSize(500, 500);
this.setVisible(true);
addKeyListener(new A(g));
}

public static void main(String[] args) {
new DrawRectTest();

}
}

class A implements KeyListener {
GPanel j;
public A(GPanel jp){
this.j=jp;
}

@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_DOWN:
j.setX(j.getX());
j.setY(j.getY()+40);
j.removeAll();
j.repaint();
break;
case KeyEvent.VK_UP:
break;
case KeyEvent.VK_LEFT:
break;
case KeyEvent.VK_RIGHT:
break;

}
}

@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub

}

}

class GPanel extends JPanel {
int x=40;
int y=30;

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public GPanel() {

}

public GPanel(int x, int y) {
this.x = x;
this.y = y;
}

public void paint(Graphics g) {

int w = 0;
int h = 30;
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
g.drawRect(w = w + 40, h, 40, 40);
}
w = 0;
h += 40;
}
g.fillOval(x, y, 40, 40);

}

}
展开
 我来答
freejsdou
2011-05-12 · TA获得超过421个赞
知道小有建树答主
回答量:364
采纳率:100%
帮助的人:375万
展开全部
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class DrawRectTest extends JFrame {
int oldx =40,x=40;
int y=30, oldy =40;
public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public void moveOval(){

Graphics g = this.getGraphics();
Color c = g.getColor();
g.setColor(this.getBackground());
g.fillOval(oldx, oldy, 40, 40);
g.setColor(c);
g.fillOval(x, y, 40, 40);
this.oldx = x;
this.oldy = y;
}
public DrawRectTest() {

this.setSize(500, 500);
this.setVisible(true);
addKeyListener(new A(this));
}
public void paint(Graphics g) {
super.paint(g);
int w = 0;
int h = 30;
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
g.drawRect(w = w + 40, h, 40, 40);
}
w = 0;
h += 40;
}
g.fillOval(x, y, 40, 40);

}
public static void main(String[] args) {
new DrawRectTest();

}
}

class A implements KeyListener {
DrawRectTest j;
public A(DrawRectTest jp){
this.j=jp;
}

@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_DOWN:
j.setX(j.getX());
j.setY(j.getY()+40);
//j.repaint();
j.moveOval();
break;
case KeyEvent.VK_UP:
break;
case KeyEvent.VK_LEFT:
break;
case KeyEvent.VK_RIGHT:
break;

}
}

@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub

}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式