0

我已经实现了一个倒数计时器(在函数中),它每秒更新一次摆动面板中的标签这是代码:

public void DefineTimer()
    {
    Action updateClockAction = new AbstractAction() {
                    public void actionPerformed(ActionEvent e){

                         JPanelMainGame.this.jLabelSeconds.setText(Integer.toString(JPanelMainGame.this.m_TimerTotalSeconds));
                         JPanelMainGame.this.jLabelSeconds.setFont(new java.awt.Font("Lucida Handwriting", 1, 36));
                         JPanelMainGame.this.jLabelSeconds.setForeground(Color.red);
                         JPanelMainGame.this.jLabelSeconds.setVisible(true);



                        if( JPanelMainGame.this.m_TimerTotalSeconds >0)
                        {
                             JPanelMainGame.this.m_TimerTotalSeconds--;
                        }
                        else if ( JPanelMainGame.this.m_TimerTotalSeconds == 0)
                        {

                            JPanelMainGame.this.m_Timer.stop();
                            JPanelMainGame.this.jLabelSeconds.setText("0");
                            System.out.println("!m_WasGameDecisived: "+!m_WasGameDecisived);

                            JPanelGameApplet gameApplet = (JPanelGameApplet) getTopLevelAncestor();
                            //Checking whether time ended for both players and no solution was recieved

                            if(gameApplet.GetJPanelChooseGame().GetGameType() == eGameType.Net)
                            {


                                gameApplet.GetClinetThread().UpdateServerOfTimeEnded();

                                if (!m_WasGameDecisived)
                                {
                                    // 
                                    System.out.println("Tie - No one had a solution in the given time");

                                    System.out.println("Before send request to solve - Is Socket Closed:"+((JPanelGameApplet) 
                                    gameApplet.GetClinetThread().SendRequestToClosePlayerThreadAndRemoveItFromPlayersOnServer();

                                    ((JPanelGameApplet)getTopLevelAncestor()).GetDJ().stop();
                                    Menu.BrowseTo(PanelMenuNumber.k_ChooseGame, JPanelMainGame.this.getParent());
                                    ((JPanelGameApplet)getTopLevelAncestor()).GetDJ().play(0);

                                }
                            }
                            else if(gameApplet.GetJPanelChooseGame().GetGameType() == eGameType.Single)
                            {
                                JPanelMainGame.this.showPopUpSelectionBar();

                            }

                        }

                       ((JPanelGameApplet)getTopLevelAncestor()).GetNetMainGame().Initialize();

                    }
                };
                m_Timer = new Timer(1000, updateClockAction);
        }  

现在我的问题出在我的代码的另一部分,当我想要做以下事情时:

case ProtocolMessages.k_StartGame:
                       m_GameApplet.GetJpanelStartNetGame().DefineTimer();
                       m_GameApplet.GetJpanelStartNetGame().GetTimer().start();
                       m_GameApplet.ShowBoardToSolve();
                       Menu.BrowseTo(PanelMenuNumber.k_NetPlayersGameStart,m_GameApplet.GetJPanelNetGameSetting().getParent());
                       m_GameApplet.GetDJ().Next();
                       break;  

所以问题是当我想开始游戏时,我正在定义我的计时器并分配它,给它启动命令并转到我应该在那里看到计时器(JLabel更新)的屏幕。
尽管它应该已经在计数(甚至在显示计时器的屏幕之前),但我仍然有延迟:我得到了显示计时器的面板,大约两秒钟后Jlabel出现并开始倒计时。
我认为这是因为在Jlabel我做的时候事件调度线程没有立即更新Jlabel.setText()

关于如何立即开始游戏而不延迟显示的任何建议Jlabel
谢谢

4

1 回答 1

2

从线程调用 SwingUtilities.invokeAndWait 以设置标签文本。

于 2011-10-12T16:48:40.833 回答