简体   繁体   English

GUI无法正确更新

[英]GUI not updating properly

I have just finished a project that involves creates a maze and programming three "robots" to move through it using different strategies. 我刚刚完成了一个项目,其中涉及创建一个迷宫并对三个“机器人”进行编程,以使用不同的策略在其中进行移动。 The classes involved in my implementation are: 实现中涉及的类是:

  • an abstract class Robot with three subclasses 具有三个子类的抽象类机器人
  • a Maze class which handles the logical operations of the maze 处理迷宫逻辑操作的迷宫类
  • a JFrame class that displays the maze graphically and shows the robot moving through it 一个JFrame类,以图形方式显示迷宫并显示机器人在迷宫中移动

Finally, I have an entry-point with a main method that contains only the following: 最后,我有一个带有main方法的入口点,其中仅包含以下内容:

    Maze m = new Maze();
    MazeFrame mf = new MazeFrame(m);
    RightHandRobot mr = new RightHandRobot(m, mf);
    mr.go(m);

This works perfectly - the maze is displayed on the GUI and I can watch the robot moving through it until he reaches the end - the desired effect. 这非常完美-迷宫显示在GUI上,我可以看到机器人穿过迷宫直至到达终点-达到了预期的效果。

My problem is this: On the top of the JFrame are three buttons, each with an ActionListener that creates an instances of one of the robot types and calls its go method, which causes it to move until it is at the "finish cell". 我的问题是:在JFrame的顶部是三个按钮,每个按钮都有一个ActionListener,它创建一种机械手类型的实例并调用其go方法,这使其移动直到到达“完成单元”。 When I click one of these buttons, the robot is created and begins moving (which I can verify by println statements executed on each move), but I cannot see the robot moving on the GUI. 当我单击这些按钮之一时,便创建了机械手并开始移动(可以通过每次移动执行的println语句进行验证),但是我无法在GUI上看到机械手在移动。 The GUI is only update when the robot reaches the finish (ie when the go() method completes), at which point the entire path taken by the robot is shown. 仅在机器人到达终点(即go()方法完成)时才更新GUI,此时将显示机器人所走的整个路径。

Why does the GUI update properly when the robot is created by the main method, but not when it is created by the JFrame class? 为什么在通过main方法创建机械手而不是通过JFrame类创建机械手时,GUI可以正确更新?

I'm not sure what code you'll need to see to help me with this, so let me know what you need to see. 我不确定您需要看什么代码来帮助我解决这个问题,所以让我知道您需要看什么。

Use a javax.swing.Timer to pace the animation, as shown in this FleetPanel . 使用javax.swing.Timer来对动画进行节奏调整,如本FleetPanel所示。 At each tick, the timer's listener should render only running robots. 在每个滴答声中,计时器的侦听器应仅渲染正在运行的机器人。

图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM