这是我的简单代码的一部分。此代码包含带有坐标的可移动椭圆和静态椭圆。我newX=100,newY=100
试图在点击鼠标左键后自动移动该可移动椭圆。移动代码行在新线程中thread
。Thrad 真正从单击鼠标按钮开始,但没有任何反应。使用箭头键移动后,椭圆开始移动。我尝试repaint()
在不同的地方调用方法,但似乎没有帮助。有什么建议吗?谢谢!
public class Buffer extends JPanel implements Runnable,KeyListener,MouseListener{
public static int x;
public static int y;
public static int newX;
public static int newY;
public static Thread thread;
public static boolean check;
public static JFrame frame;
public static int pointX;
public static int pointY;
public static boolean repaint;
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawOval(x, y, 20, 20);
newX=100;
newY=100;
g.fillOval(newX, newY, 20, 20);
if(repaint)
repaint();
}
public static void main(String args[]){
Buffer z=new Buffer();
z.setBackground(Color.white);
frame=new JFrame();
frame.setSize(500,500);
frame.addKeyListener(z);
frame.addMouseListener(z);
frame.add(z);
frame.setVisible(true);
frame.requestFocusInWindow();
thread=new Thread(){
public void run(){
try{
for(int i=0;i<=5;i++){
x=x+i;
repaint=true;
thread.sleep(1000);
}
}catch(InterruptedException v){System.out.println(v);}
}
};
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_LEFT){
x=x-10;
repaint();
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
x=x+10;
repaint();
}
if(e.getKeyCode()==KeyEvent.VK_UP){
y=y-10;
repaint();
}
if(e.getKeyCode()==KeyEvent.VK_DOWN){
y=y+10;
repaint();
}
}
public void mouseClicked(MouseEvent e) {
thread.start();
}
}