java Timer関数とイベント処理

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
サイダー

java Timer関数とイベント処理

#1

投稿記事 by サイダー » 10年前

コード:

public void actionPerformed(ActionEvent e) {
		if(enflg==0){
			cmd = e.getActionCommand();
			enflg=1;
		}
		if(cmd.equals("ensei1")){
			timer.start();
				if(flg==0){
					zi01=zikan1.getText();//時間を文字列で取得
					hun01=enseihun1.getText();//分を文字列で取得
					zi1= Integer.parseInt(zi01);//時間を数値
					hun1= Integer.parseInt(hun01);//分を数値
					flg=1;
					if(hun1!=0){//2分と入力したら1分59秒で表示される
						hun1--;
					}
					zikann1.setText(zi1+"時間");
					hunn.setText(hun1+"分");
					testsec.setText(sec+"秒");
					sec--;
					return;
				}else if(flg==1){
					if(zi1>=0){
						if(hun1>=0){
							if(sec>=0){
								zikann1.setText(zi1+"時間");
								hunn.setText(hun1+"分");
								testsec.setText(sec+"秒");
								sec--;
								return;
							}else{
								hun1--;
								sec=59;
								return;
							}
						}else{
							zi1--;
							hun1=59;
							return;
						}
					}else{
						//タイマーの音声
						Clip clip = null;
					       int count = 5;      // (count+1)回 再生する
					       AudioInputStream audioInputStream;
					       try{  
					        File soundFile = new File("vc/Voice.wav");
					           audioInputStream = AudioSystem.getAudioInputStream(soundFile);
					           AudioFormat audioFormat = audioInputStream.getFormat();
					           DataLine.Info info = new DataLine.Info(Clip.class, audioFormat);
					           clip = (Clip)AudioSystem.getLine(info);
					           clip.open(audioInputStream);
					           clip.loop(count);
					       }
					       catch (UnsupportedAudioFileException e1){
					        	e1.printStackTrace(); 
					       }
					       catch (IOException e1){  
					        	e1.printStackTrace();  
					       }
					       catch (LineUnavailableException e1){
					        	e1.printStackTrace();
					       }
					        // 6秒経過したら終了する
					       try{  
					    	   Thread.sleep(6000);  
					       }
					       catch (InterruptedException e1){
					       }
					       clip.stop();
					       timer.stop();
					       flg=0;
					       enflg=0;
					}
				}
		}else if(cmd.equals("ensei2")){
			timer.start();
			if(flg==0){
				zi02=zikan2.getText();//時間を文字列で取得
				hun02=enseihun2.getText();//分を文字列で取得
				zi2= Integer.parseInt(zi02);//時間を数値
				hun2= Integer.parseInt(hun02);//分を数値
				flg=1;
				if(hun1!=0){//2分と入力したら1分59秒で表示される
					hun1--;
				}
				zikann2.setText(zi2+"時間");
				hunn2.setText(hun2+"分");
				testsec2.setText(sec2+"秒");
				sec2--;
				return;
			}else if(flg==1){
				if(zi2>=0){
					if(hun2>=0){
						if(sec2>=0){
							zikann2.setText(zi2+"時間");
							hunn2.setText(hun2+"分");
							testsec2.setText(sec2+"秒");
							sec2--;
							return;
						}else{
							hun2--;
							sec=59;
							return;
						}
					}else{
						zi2--;
						hun2=59;
						return;
					}
				}else{
					//タイマーの音声
					Clip clip = null;
				       int count = 5;      // (count+1)回 再生する
				       AudioInputStream audioInputStream;
				       try{  
				        File soundFile = new File("vc/Voice.wav");
				           audioInputStream = AudioSystem.getAudioInputStream(soundFile);
				           AudioFormat audioFormat = audioInputStream.getFormat();
				           DataLine.Info info = new DataLine.Info(Clip.class, audioFormat);
				           clip = (Clip)AudioSystem.getLine(info);
				           clip.open(audioInputStream);
				           clip.loop(count);
				       }
				       catch (UnsupportedAudioFileException e1){
				        	e1.printStackTrace(); 
				       }
				       catch (IOException e1){  
				        	e1.printStackTrace();  
				       }
				       catch (LineUnavailableException e1){
				        	e1.printStackTrace();
				       }
				        // 6秒経過したら終了する
				       try{  
				    	   Thread.sleep(6000);  
				       }
				       catch (InterruptedException e1){
				       }
				       clip.stop();
				       timer.stop();
				       flg=0;
				       enflg=0;
				}
			}
		}
	}
Timer関数を使用してボタンが押されたらテキストフィールドに入力されている値を読み込み
カウントダウン方式でタイマーを動かすプログラムを作っています。
「cmd = e.getActionCommand();」をボタンが押されていないときに読み込んでしまいエラーが出るため、一番上のif(enflg==0){}で囲っているのですが
別々のボタンで複数のタイマーを起動させるためにif(enflg==0 || enflg2==0){}などで囲むと当然上記のようなエラーが出ます。


複数のボタンがあり、ボタンが押されたときだけ「cmd = e.getActionCommand();」を読みこませる方法はないでしょうか?
※Timer関数を使用しているのでイベント処理内に普通に書くだけでは、ボタンが押されていないときでも読み込んでしまいます。

アバター
h2so5
副管理人
記事: 2212
登録日時: 13年前
住所: 東京
連絡を取る:

Re: java Timer関数とイベント処理

#2

投稿記事 by h2so5 » 10年前

ボタンが押されていないときはgetActionCommandはnullを返すのでそれをチェックすればいいと思います。

閉鎖

“C言語何でも質問掲示板” へ戻る