0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Javaでタイマー(Timer)を活用してみよう

0
Posted at

決済処理のような待機時間を視覚的に表現するため、javax.swing.Timerを使用しました。
300ms(0.3秒)間隔で進捗率JProgressBarを更新することで、ユーザーにアプリが正常に動作していることを伝えます。

(カード決済の例)

    public JProgressBar progress;
	public  Timer timer;


  public UseCardDesign(OrderDesign od) {
		this.od=od;
		
		UseCardEvent uce=new UseCardEvent(this);
		 progress=new JProgressBar(0,100);//0~100%
		 progress.setString("決済中...");
		 progress.setStringPainted(true);
		 
		 timer=new Timer(300,null);//0.3秒ごとに
		 timer.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int percent=progress.getValue()+10;
				 progress.setValue(percent);//10%ずつ伸びる
                
				 if(percent>=100) {
					 timer.stop();
					 uce.successCard();//100以上になった決済成功
				 }//end if
			}//actionPerformed
		});

         timer.start();
		 

javax.swing.Timerのメソッド

start() : タイマーを起動し、リスナー(ActionListener)への通知を開始します。

stop() : タイマーを停止し、イベントの発生を中断します。

setRepeats(boolean flag) : タイマーを継続的に繰り返すか、一度だけ実行するかを設定します。デフォルトはtrueですが、一度限りの遅延処理を行いたい場合はfalseに設定します。

restart() : タイマーをリセットして再度開始します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?