在do-while循环在定时器更新的JButton在do-while循环在定时器更新的JButton

2019-05-16 19:15发布

我有一些麻烦JButton的反复更新一个do-while循环(带有定时器使用)。 我工作的一个简单的游戏,玩上对应于一个JButton的ArrayList与100层的按钮对象的10×10格。

该计划的这部分处理简单的寻路(即,如果我点击的字符,然后一个空的瓷砖,该角色会通过的道路上每瓦到目的地移动)。 有每个步骤之间的延迟,使用户可以看到角色的进步。

在当前事物的状态,运动是正确的,但是当人物到达目的地,而不是中间步骤将JButton才会更新。

public void move(int terrainTile)
{
    int currentPosition = actorList.get(selectedActor).getPosition();
    int movementValue = 0;
    int destination = terrainTile;
    int destinationX = destination / 10;
    int destinationY = destination % 10;

    do
    {
        currentPosition = actorList.get(selectedActor).getPosition();  // Gets PC's current position (before move)
        System.out.println("Old position is " + currentPosition);
        int currentX = currentPosition / 10;
        int currentY = currentPosition % 10;


        if(actorList.get(selectedActor).getCurrentAP() > 0)
        {
            movementValue = 0;

            if(destinationX > currentX)
            {
                movementValue += 10;
            }

            if(destinationX < currentX)
            {
                movementValue -= 10;
            }

            if(destinationY > currentY)
            {
                movementValue += 1;
            }

            if(destinationY < currentY)
            {
                movementValue -= 1;
            }

            int nextStep = currentPosition + movementValue;

            myGame.setActorIdInTile(currentPosition, -1);  //Changes ActorId in PC current tile back to -1
            scrubTiles(currentPosition);

            actorList.get(selectedActor).setPosition(nextStep);  //  Sets new position in actor object
            System.out.println("Actor " + selectedActor + " " + actorList.get(selectedActor).getName() + " position has been updated to " + nextStep);

            myGame.setActorIdInTile(nextStep, selectedActor); // Sets ActorId in moved to Tile
            System.out.println("Tile " + nextStep + " actorId has been updated to " + selectedActor);

            buttons.get(nextStep).setIcon(new ImageIcon(actorList.get(selectedActor).getImageName()));

            // If orthagonal move AP-4
            if(movementValue == 10 || movementValue == -10 || movementValue == 1 || movementValue == -1)
            {
                actorList.get(selectedActor).reduceAP(4);
            }
            // If diagonal move AP-6
            else
            {
                actorList.get(selectedActor).reduceAP(6);
            }

            System.out.println(actorList.get(selectedActor).getName() + " has " + actorList.get(selectedActor).getCurrentAP() + " AP remaining");

            try
            {
                Thread.sleep(500);    // one second
            }
            catch (Exception e){} 

            buttons.get(nextStep).repaint();

        }

        else
        {
            System.out.println(actorList.get(selectedActor).getName() + " has insufficient AP to move");
            break;
        }

    }while(destination != (currentPosition + movementValue));

我已经试过:

  1. buttons.get(NEXTSTEP).repaint(); (试图把一个命令设置的ImageIcon后重新绘制按钮,没有变化。

  2. buttons.get(NEXTSTEP).revalidate(); (没有100%确定这是什么一样 - 它想出了一个潜在的解决方案,但不起作用。

  3. 步骤1和2加起来

  4. 看着摆定时器类 - 不发生每次运动一个ActionEvent被激发,(仅当字符被选择目标文件是空的),所以不知道我怎么能得到这个工作

任何帮助将不胜感激!

Answer 1:

我真的不知道”你想知道你的意见,但+1以上的答案,在我看来,这才是真正的原因到底是什么。 看看下面这个例子程序,只需您的通话添加到move(...)方法里面timerAction ,似乎可以为你工作。 这里试试这个代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GridExample
{
    private static final int SIZE = 36;
    private JButton[] buttons;
    private int presentPos;
    private int desiredPos;
    private Timer timer;
    private Icon infoIcon = 
                UIManager.getIcon("OptionPane.informationIcon");

    private ActionListener timerAction = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            buttons[presentPos].setIcon(null);
            if (desiredPos < presentPos)
            {
                presentPos--;
                buttons[presentPos].setIcon(infoIcon);
            }
            else if (desiredPos > presentPos)
            {
                presentPos++;
                buttons[presentPos].setIcon(infoIcon);
            }
            else if (desiredPos == presentPos)
            {
                timer.stop();
                buttons[presentPos].setIcon(infoIcon);
            }
        }
    };

    public GridExample()
    {
        buttons = new JButton[SIZE];
        presentPos = 0;
        desiredPos = 0;
    }

    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("Grid Game");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(6, 6, 5, 5));
        for (int i = 0; i < SIZE; i++)
        {
            final int counter = i;
            buttons[i] = new JButton();
            buttons[i].setActionCommand("" + i);
            buttons[i].addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    desiredPos = Integer.parseInt(
                                    (String) buttons[counter].getActionCommand());
                    timer.start();              
                }
            });
            contentPane.add(buttons[i]);
        }
        buttons[presentPos].setIcon(infoIcon);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
        timer = new Timer(1000, timerAction);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new GridExample().createAndDisplayGUI();
            }
        });
    }
}


Answer 2:

这是因为你正在做你做{}而在UI线程。 为了解决这个问题,你应该使用的SwingWorker,或javax.swing.Timer中



文章来源: Updating JButton on a timer in a do-while loop