0

いくつかのJPanelを含むJFrameがあります。1つは、JButtonのGridLayout(4 x 4)を備えたboardPanelです。もう1つは、JTextFieldと2つのJButtonを含むFlowLayoutを持つcontrolPanelです。

enableBoard()とdisableBoard()の2つのメソッドがあります。これらのメソッドでは、forループを介してboardPanelとcontrolPanelの各コンポーネントをtrueまたはfalseに設定しました。これはすべて、スイングタイマーの実行中に発生します。タイマーを開始してenableBoard()を開始すると(disableBoardで無効になりました)、多少は機能しますが、遅延が発生することが多いため、すべてのボタンが同時に無効になるわけではありません。タイマーと関係があるのではないかと思います。遅延は数秒で発生しているように見えるため...重要な方法は次のとおりです。

public void disableBord() {
    Component[] boardcomps = boardPanel.getComponents();
    for(int i = 0; i < bordcomps.length; i++) {
        boardcomps[i].setEnabled(false);
    }

    Component[] checkComps = controlPanel.getComponents();
    for(int i = 0; i < checkComps.length; i++) {
        checkComps[i].setEnabled(false);
    }
}

public void enableBord() {
    Component[] boardcomps = boardPanel.getComponents();
    for(int i = 0; i < bordcomps.length; i++) {
        boardcomps[i].setEnabled(true);
    }

    Component[] checkComps = controlPanel.getComponents();
    for(int i = 0; i < checkComps.length; i++) {
        checkComps[i].setEnabled(true);
    }
}

そして実行されるタイマー:

timer = new Timer(1000, new ActionListener() {
        int time = game.getSeconds()+4;
        @Override
        public void actionPerformed(ActionEvent e) {
            if (time == game.getSeconds()+4) {
                lblFeedback.setText("3");
                lblTime.setText(game.secToMinSec(game.getSeconds()));
                time--;
            } else if (time == game.getSeconds()+3) {
                lblFeedback.setText("2");
                lbltime.setText(game.secToMinSec(game.getSeconds()));
                time--;
            } else if (time == game.getSeconds()+2) {
                lblFeedback.setText("1");
                lblTime.setText(game.secToMinSec(game.getSeconds()));
                time--;
            } else if (time == game.getSeconds()+1) {
                lblFeedback.setText("Start!");
                lblTime.setText(game.secToMinSec(time));
                time--;
                enableBord();
            } else if(time == 0) {
                lblTime.setText("0:00");
                lblFeedback.setText("Game finished!");
                disableBord();
                game.endeGame();
                timer.stop();
            } else if (time ==  game.getSeconds()) {
                lblTime.setText(game.secToMinSec(time));
                time--;
            } else {
                lblTime.setText(game.secToMinSec(time));
                time--;
            }
        }
    });

遅延は次のようになります。

JComponentsを無効にするときの遅延

4

1 に答える 1

1

適切なrepaint(); ループ内のすべてのsetEnable()が私の問題を解決したように見えた後!

于 2013-03-06T17:21:47.333 に答える