Java:如何等待updateUI?
我正在做一个棋盘游戏的演示。我想你一定知道。我怎么遇到问题。结果很快就显示出来。我想慢慢看看它是如何运作的。
主要代码如下:
public void ChessBoard(int tr, int tc, int dr, int dc, int size) throws InterruptedException {
if (size == 1) {
return;
}
Random rd = new Random();
float red = rd.nextFloat();
float green = rd.nextFloat();
float blue = rd.nextFloat();
Color col = new Color(red, green, blue);
int s = size / 2;
if (dr < tr + s && dc < tc + s) {
ChessBoard(tr, tc, dr, dc, s);
} else {
button[tr + s - 1][tc + s - 1].setBackground(col);
ChessBoard(tr, tc, tr + s - 1, tc + s - 1, s);
}
if (dr < tr + s && dc >= tc + s) {
ChessBoard(tr, tc + s, dr, dc, s);
} else {
button[tr + s - 1][tc + s].setBackground(col);
ChessBoard(tr, tc + s, tr + s - 1, tc + s, s);
}
if (dr >= tr + s && dc < tc + s) {
ChessBoard(tr + s, tc, dr, dc, s);
} else {
button[tr + s][tc + s - 1].setBackground(col);
ChessBoard(tr + s, tc, tr + s, tc + s - 1, s);
}
if (dr >= tr + s && dc >= tc + s) {
ChessBoard(tr + s, tc + s, dr, dc, s);
} else {
button[tr + s][tc + s].setBackground(col);
ChessBoard(tr + s, tc + s, tr + s, tc + s, s);
}
mainPanel.updateUI();
for (long i=1;i<10000;i++){
for (long j=0;j<10000;j++){
i+=1;
i-=1;
}
}
}
但结果转眼间就出来了!看来 mainPanel 还没有完成它的工作。 mainJanel 是一个 JPanel 对象。
I was doing a demo on the ChessBoard Game.I guess you must know it.How ever I meet with a problem.The result just show to quickly.I want to see how it works out slowly.
Here is the main code:
public void ChessBoard(int tr, int tc, int dr, int dc, int size) throws InterruptedException {
if (size == 1) {
return;
}
Random rd = new Random();
float red = rd.nextFloat();
float green = rd.nextFloat();
float blue = rd.nextFloat();
Color col = new Color(red, green, blue);
int s = size / 2;
if (dr < tr + s && dc < tc + s) {
ChessBoard(tr, tc, dr, dc, s);
} else {
button[tr + s - 1][tc + s - 1].setBackground(col);
ChessBoard(tr, tc, tr + s - 1, tc + s - 1, s);
}
if (dr < tr + s && dc >= tc + s) {
ChessBoard(tr, tc + s, dr, dc, s);
} else {
button[tr + s - 1][tc + s].setBackground(col);
ChessBoard(tr, tc + s, tr + s - 1, tc + s, s);
}
if (dr >= tr + s && dc < tc + s) {
ChessBoard(tr + s, tc, dr, dc, s);
} else {
button[tr + s][tc + s - 1].setBackground(col);
ChessBoard(tr + s, tc, tr + s, tc + s - 1, s);
}
if (dr >= tr + s && dc >= tc + s) {
ChessBoard(tr + s, tc + s, dr, dc, s);
} else {
button[tr + s][tc + s].setBackground(col);
ChessBoard(tr + s, tc + s, tr + s, tc + s, s);
}
mainPanel.updateUI();
for (long i=1;i<10000;i++){
for (long j=0;j<10000;j++){
i+=1;
i-=1;
}
}
}
But the result just turned out in a flash!It seems the mainPanel didn't finished its job.
The mainJanel is a JPanel object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来您的代码正在事件调度线程上执行。你永远不应该告诉 EDT 去 sleep()。这可以防止 GUI 重新绘制自身。有关详细信息,请阅读 Swing 教程中有关并发的部分。
相反,您应该使用 Swing Timer 来安排每个动作的动画。本教程还有关于计时器的部分。
It appears your code is executing on the Event Dispatch Thread. You should NEVER tell the EDT to sleep(). This prevents the GUI from repainting itself. Read the section from the Swing tutorial on Concurrency for more information.
Instead you should be using a Swing Timer to schedule the animation of each move. The tutorial also has a section on Timers.
updateUI 用于更改组件的外观和感觉。要重绘面板的内容,您应该使用
updateUI is used to change the look&feel of the component. To redraw the contents of the panel you should use
只需使用 Thread.sleep( /* time in milliseconds */ ) 来减慢执行速度。
Just use
Thread.sleep( /* time in milliseconds */ )
to slow the execution.使用
sleep
停止执行。示例
这样它会减慢你的程序。
Halting execution with
sleep
.Example
This way it would slow down your program.