各位大仙 帮我改改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);
}
} 展开
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);
}
} 展开
1个回答
展开全部
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
}
}
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
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询