java图形界面问题,为什么出现累加现象?

importjava.awt.*;importjava.awt.event.*;importjava.awt.geom.Rectangle2D;importjavax.s... import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;

import javax.swing.*;
public class GameTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
GameFrame frame=new GameFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class GameFrame extends JFrame
{
public GameFrame()
{
setTitle("ActionTest");
setSize(600,600);
buttonPanel=new GamePanel();
add(buttonPanel);
}
private GamePanel buttonPanel;
}

class GamePanel extends JComponent implements KeyListener
{
private double x;
private double y;

public GamePanel()
{
x=0;
double y=0;
}
public void paintComponent(Graphics g)
{
setFocusable(true);
this.addKeyListener(this);
g2=(Graphics2D)g;
//初始化界面
rect1=new Rectangle2D.Double(y,x,100,100);

g2.setPaint(color);
g2.fill(rect1);

}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode(); //获取按键码
switch(key)
{
case KeyEvent.VK_UP: //向上
x-=100; break;
case KeyEvent.VK_DOWN: //向下
x=x+100; break;
case KeyEvent.VK_LEFT: //向左
y-=100; break;
case KeyEvent.VK_RIGHT: //向右
y+=100; break;
}
repaint();
}
public void keyTyped(KeyEvent event)
{
}
@Override
public void keyReleased(KeyEvent arg0) {

}

private Rectangle2D rect1;

}
让小方块每次只走一格,
展开
 我来答
榕朵灏6670
2011-10-27 · TA获得超过7.9万个赞
知道大有可为答主
回答量:3.7万
采纳率:0%
帮助的人:4765万
展开全部
主要原因是你的监听加错了地方,稍微改了一下,可以用了:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;

import javax.swing.*;
public class GameTest
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
GameFrame frame=new GameFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class GameFrame extends JFrame
{
/**
* NOPMD
*/
private static final long serialVersionUID = 1L;
private GamePanel buttonPanel;
public GameFrame()
{
setTitle("ActionTest");
setSize(600,600);
buttonPanel=new GamePanel();
add(buttonPanel);
}

}

class GamePanel extends JComponent implements KeyListener
{
/**
* NOPMD
*/
private static final long serialVersionUID = 1L;
private double x;
private double y;
private Rectangle2D rect1;
public GamePanel() {
setFocusable(true);
this.addKeyListener(this);
}

public void paintComponent(Graphics g) {
Graphics2D g2;
g2=(Graphics2D)g;
rect1=new Rectangle2D.Double(x,y,100,100);
g2.setPaint(Color.white);
g2.fill(rect1);
}

public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key)
{
case KeyEvent.VK_UP:
y-=100; break;
case KeyEvent.VK_DOWN:
y=y+100; break;
case KeyEvent.VK_LEFT:
x-=100; break;
case KeyEvent.VK_RIGHT:
x+=100; break;
}
repaint();

}

public void keyReleased(KeyEvent arg0) {
//do nothing
}

public void keyTyped(KeyEvent arg0) {
//do nothing
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式