请教,awt/swing高手请进!

Yoinn 2003-12-02 05:30:13
import java.awt.*;
import java.awt.event.*;

public class MainClass extends Frame{

public MainClass() {
super();
this.addWindowListener(new MyWindowListener());
}

public void showFrame(){
this.add( new MyComponent() );
this.setTitle("Main Frame");
this.setSize(300,200);
this.setVisible(true);
}

private class MyWindowListener extends WindowAdapter{
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
}

public static void main(String[] args) {
MainClass mainClass = new MainClass();
mainClass.showFrame();
}
}

class MyComponent extends Component implements Runnable {

Thread t = null;

public MyComponent() {
super();
t = new Thread(this);
t.start();
System.out.println("MyComponent()");
}


public void run() {
while(true){
System.out.println("run()");
try{
Thread.sleep(500);
}catch(InterruptedException e){
}
repaint();
}
}

public void paint(Graphics g){
System.out.println("paint(Graphics g)");
drawString(g);
}

public void update(Graphics g){
System.out.println("update(Graphics g)");
drawString(g);
}

public void drawString(Graphics g){
g.drawString("Hello",20,20);
}
}
//=============================================================
//为什么显示结果是:
/*
MyComponent()
run()
paint(Graphics g)
paint(Graphics g)
run()
paint(Graphics g)
run()
paint(Graphics g)
run()
paint(Graphics g)
run()
....
*/
// update方法没有执行,这是怎么回事啊?
...全文
74 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
cql0007 2003-12-06
  • 打赏
  • 举报
回复
标记一下
killme2008 2003-12-06
  • 打赏
  • 举报
回复
楼上正解
wobelisk 2003-12-06
  • 打赏
  • 举报
回复
还没明白?
你的MyComponent是Component的派生类。J2sdk1.1后,Component的派生类都是轻量级(Lightweighted)的。Lightweighted 对象的repaint()会尽快调用paint()方法,而不是update()方法。这就是你的程序中update()不执行的原因。
Lightweighted不执行update()的目的是为了减少闪烁。
Component类中已经定义的大多数类是重量级的,包括Button,List,Label等。Heavyweighted对象的repaint()方法会产生一个PaintEvent事件并建议程序执行update()。实际什么时间执行update()是由程序决定的,并不由你控制。
如果你的MyComponent从重量级类派生的,你将会看到update()被执行。
kiki0712 2003-12-06
  • 打赏
  • 举报
回复
wobelisk()把我从书上看的知识解释的这么透彻,学习ING!~~~
Yoinn 2003-12-06
  • 打赏
  • 举报
回复
to wobelisk(),
如果frame add two Component,,
一个repaint(),另一个为什么也跟着repaint();
tsd3698 2003-12-06
  • 打赏
  • 举报
回复
感谢wobelisk(),学到好东西了。
Yoinn 2003-12-05
  • 打赏
  • 举报
回复
没解决啊!!
wobelisk 2003-12-04
  • 打赏
  • 举报
回复
try this:

class MyComponent extends Label implements Runnable {
Thread t = null;
public MyComponent() {
super("fff");
....

result will be
update()
run()
....

As you overrided update() ( which usually will call paint()), paint() is not called by update(). So when paint() will be excuted is determined by JVM.
wobelisk 2003-12-04
  • 打赏
  • 举报
回复
subclass extends java.awt.Component is Lightweighted component, while existing defined awt components (Button, List..) are Heavyweighted.

If it's a lightweighted component, repaint() will try to call paint() as soon as possible, otherwise, repaint() will generate a PaintEvent which will suggest update() to be executed.

Another thing is when to execute update() is determined by the Thread. It will be on the schedule, but you don't know when.
Yoinn 2003-12-03
  • 打赏
  • 举报
回复
to onefox(一品狐);
具体说明一下;
我用g.drawString("Hello " + System.currentTimeMillis(),20,20);
也不执行update
nil2000 2003-12-03
  • 打赏
  • 举报
回复
程序改错:
public void paint(Graphics g){
//add this line
super.paint(g);
System.out.println("paint(Graphics g)");
drawString(g);
}
wbel 2003-12-03
  • 打赏
  • 举报
回复
我把窗体放大,最小化,移动……都不执行update,为什么?
cfan246 2003-12-03
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2523/2523130.xml?temp=.629162 麻烦看看
tensiont 2003-12-03
  • 打赏
  • 举报
回复
同意 binbin2000

移动一下也可以看见!
qm0445 2003-12-03
  • 打赏
  • 举报
回复
看看:)HOHO!
binbin2000 2003-12-03
  • 打赏
  • 举报
回复
呵呵,把窗体放大一下,就执行了。
loveyousomuch 2003-12-03
  • 打赏
  • 举报
回复
路过!
Hodex 2003-12-03
  • 打赏
  • 举报
回复
update是调用的Component的
不过paint就不是很清楚了
iamwls 2003-12-03
  • 打赏
  • 举报
回复
Updates this component.
The AWT calls the update method in response to a call to repaint. The appearance of the component on the screen has not changed since the last call to update or paint. You can assume that the background is not cleared





改为
public static void main(String[] args) {
MainClass mainClass = new MainClass();
mainClass.showFrame();
mainClass.repaint();
}

即可
onefox 2003-12-02
  • 打赏
  • 举报
回复
update() 方法是当数据改变时才调用.
加载更多回复(4)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧