1
にタイマーを停止するにはどうすれば色offlashingランダムシーケンスを刺激するためにいくつかのボタンの色を変更するために少しのコードを書きました。私は私のためにこれを行うためにタイマーを設定しているが、私はちょうどそれがは、Swingアプリケーション
HERESに私のコードあなたがそれを行うことができますいくつかの方法があります
Timer timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int buttons[] = new int[9];
for (int j = 0; j < buttons.length; j++) {
// System.out.println("times");
Component button = frame.getContentPane().getComponent(j);
int Red = new Random().nextInt(255 - 1) + 1;
int Green = new Random().nextInt(255 - 1) + 1;
int Blue = new Random().nextInt(255 - 1) + 1;
button.setBackground(new Color(Red, Green, Blue));
SwingUtilities.updateComponentTreeUI(button);
}
}// }
}); timer.start();
Timerオブジェクトでstop()メソッドを呼び出す必要があります。いつ/あなたはそれを止めたいですか? –
iは整数変数カウンタを作成し、forループが、動作しませんで、それをインクリメントすることを試みた@fastsnil変数値が8 –
に到達tick.stopタイマータイマーは、あなたはおそらく私に何を表示することができたときに、それをインクリメントvariable.thenを作成しますあなたはコードを意味しますか? – Claudiga