简体   繁体   中英

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

Finally, I have an entry-point with a main method that contains only the following:

    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.

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". 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. 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.

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?

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 . At each tick, the timer's listener should render only running robots.

图片

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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