Updating JButton on a timer in a do-while loop

前端 未结 2 2102
独厮守ぢ
独厮守ぢ 2020-11-28 16:39

I\'m having some trouble getting a JButton to update repeatedly (used with a timer) in a do-while loop. I\'m working on a simple game, played on a 10 * 10 grid of tile objec

相关标签:
2条回答
  • 2020-11-28 17:05

    I really dont' know exactly what you wanted to know in your comments, though +1 to the answer above, seems to me that's the real cause. Have a look at this example program, simply add your call to the move(...) method inside the timerAction, seems like that can work for you. Here try this code :

    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();
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-11-28 17:08

    This is because you are doing your do { } while in the UI thread. To solve this, you should use a SwingWorker, or a javax.swing.Timer

    0 讨论(0)
提交回复
热议问题