gpt4 book ai didi

java - 使用计时器的多线程图形?

转载 作者:行者123 更新时间:2023-12-02 07:23:49 25 4
gpt4 key购买 nike

我的讲师给我布置了一项作业,要求编写一个多线程图形程序,其中球在 jframe 周围弹跳。他希望每个球都有自己的线。标记后,他告诉我计时器是多线程的,这是执行此操作的最佳方法。我为每个球使用了一个新的线程,我知道这很昂贵,但他告诉我们为每个球使用一个线程。定时器类是多线程的吗?

使用定时器

Ball b = new Ball(x, y);
BallMover bm = new BallMover(b)//adds the ball to a new instance of an actionListner
theTimer.addActionListener(bm);

使用线程

Ball b = new Ball(x,y);
BallMover bm = new BallMover(b);//adds the ball to a new instance of a Thread
bm.start();

最佳答案

来自 Swing 的文档 Timer .

Although all Timers perform their waiting using a single, shared thread (created by the first Timer object that executes), the action event handlers for Timers execute on another thread -- the event-dispatching thread. This means that the action handlers for Timers can safely perform operations on Swing components. However, it also means that the handlers must execute quickly to keep the GUI responsive.

关于java - 使用计时器的多线程图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13818743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com