刷新 Swing GUI [英] Refresh Swing GUI

查看:55
本文介绍了刷新 Swing GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含图块的板,而图块包含障碍物.障碍物可以是多种类型之一(墙壁、激光等).

I have a Board that contains tiles and the Tiles contain Obstacles. An obstacle can be one of the several types (wall, laser, ...).

游戏的每一轮我都执行棋盘,执行所有障碍.一旦障碍物被执行并且某些事情发生了变化,GUI 就会收到通知并刷新受影响的图块.

Every round of the game I execute the board, which exectutes all the Obstacles. Once an obstacle has been executed and something has changed the GUI is notified and the affected tiles are refreshed.

在 GUI 中,图块是包含重叠图像 (JLabels) 的 OverLayLayout,因此所有重叠图像都是可见的.

In the GUI a tile is an OverLayLayout that contains overlapping images (JLabels), so all the overlapping images are visible.

现在的问题是当我执行它们时,它变得很快,你什么也不会发生.例如.你没有看到激光束出现,玩家会受到伤害但你没有看到它发生......

Now the problem is when I execute them, it goes to fast, you can't anything happen. E.g. you don't see the laser beam appear, the player will be damaged but you didn't see it happen...

所以我试着让游戏等待,例如500 毫秒,以便用户可以看到 GUI 更新.我试过(但可能做错了):计时器、摇摆计时器、倒计时锁……但我似乎无法让它工作.有想法该怎么解决这个吗?或者也许是等待的替代方案?

So I tried to let the game wait for e.g. 500ms so the user can see the GUI update. I've tried (but might have done it wrong): timers, swingtimers, countdownlatch, ... but I can't seem to get it to work. Any ideas on how to fix this? Or maybe an alternative for the waiting?

提前致谢!

更新:板内执行如下所示:

Iterator<Obstacle> it = obstacles.iterator();
while (it.hasNext())
    it.next().execute(this);

CountDownLatch countDown = new CountDownLatch(1);
setChanged();
notifyObservers(countDown);
countDown.await();

GUI 板的更新功能(通知)如下所示:

The update function of the GUI board (which is notified) looks like this:

final CountDownLatch countDown = (CountDownLatch) arg;
SwingUtilities.invokeLater(new Runnable(){
   public void run() {
      try {
        updateUI();
        wait(500);
      } catch (InterruptedException ex) { }
            countDown.countDown();
   }
});

推荐答案

不看你的代码,很难找出问题所在;我怀疑你阻塞了事件调度线程.

Without seeing your code, it's hard to identify the problem; I suspect you're blocking the event dispatch thread.

这个基于磁贴的游戏使用了一个javax.swing.Timer的实例动画播放.按 A 启用动画,然后单击一个自由方块查看效果.您可以根据自己的游戏调整方法.

This tile-based game uses an instance of javax.swing.Timer to animate play. Press A to enable animation, and click on a free square to see the effect. You may be able adapt the approach to your game.

这篇关于刷新 Swing GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆