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更新)。
それでも、すでにカウントされているはずですが(タイマーを表示する画面の前でも)、まだ遅延があります。タイマーを表示するパネルが表示され、約2秒後にJlabel表示されてカウントダウンが開始されます。私がやっている時間に
すぐに更新されないイベントディスパッチスレッドが原因だと思いますJlabelJlabel.setText()

表示を遅らせることなくゲームを開始するにはどうすればよいJlabelですか?
ありがとう

4

1 に答える 1

2

スレッドから SwingUtilities.invokeAndWait を呼び出して、ラベル テキストを設定します。

于 2011-10-12T16:48:40.833 に答える