java frame update_java编程:为什么repaint()没有调用update呢?

【功能】绘制一个横着动的小球【问题】如题!小球动不起来【代码】importjava.awt.Color;importjava.awt.Graphics;importjava.awt.Graphics2D;importjavax.swing.*;/**Paneltodisplay...

【功能】绘制一个横着动的小球

【问题】如题!小球动不起来

【代码】

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import javax.swing.*;

/*

* Panel to display the picture.

*/

class MyPanel extends JPanel{

public int x=50;

public int y=50;

Color ballColor;

Runnable mbr;

Thread mbt;

MyPanel(){

this.setVisible(true);

mbr=new MoveBall();

mbt=new Thread(mbr);

mbt.start();

}

public void update(Graphics g){

g.setColor(Color.ORANGE);

g.fillRect(0, 0, 600, 500);

x+=50;

System.out.println("------>the x is :"+x);

}

public void paint(Graphics g){

ballColor=Color.BLUE;

g.setColor(ballColor);

g.fillOval(x, y, 50, 50);

}

private class MoveBall implements Runnable{

public void run(){

try{

while(x<400){

System.out.println("@@@@@@@@@@--->"+x);

repaint();

mbt.sleep(1000);

}

}catch(Exception e){

System.out.println("Error:Thread----Sleep!");

}

}

}

}

/*

* Frame to display the application.

*/

class MyFrame extends JFrame{

JPanel myPanel;

MyFrame(){

this.setTitle("Graphics Test");

this.setBounds(20,10,600,500);

this.setVisible(true);

this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);

myPanel=new MyPanel();

this.add(myPanel);

}

}

public class MyTest {

public static void main(String[] args){

MyFrame myFrame=new MyFrame();

}

}

---------------------------------------------------------------------------------------

希望各位大鸟说的详细些,非常感谢!

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值