0

変数値が の場合に現在の反復をキャンセルしたいアクション リスナーがありますnull

public class ValidateListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        myCalendarKey mCal = Project5.verifyDate(month+"/"+day+"/"+year); //month day and year are defined, just not in this code display.

        if (mCal == null) e.cancel(); //I need something to cancel the current ActionEvent if this returns true.

        /* A lot more code down here that only works if mCal is defined */
   }
}

if-else ステートメントを使用して、 if のすべてを実行し、 if の場合 mCal != nullは何もしないようにすることもできると思いますmCal == nullが、これを行うより良い方法はありますか?

4

3 に答える 3

1

あなたが探しているのは次のとおりだと思います:

e.consume()

これにより、イベントが消費されます: http://docs.oracle.com/javase/6/docs/api/java/awt/AWTEvent.html#consume()

于 2013-05-01T15:13:18.787 に答える
0

これを試して:

 @Override
 public void actionPerformed(ActionEvent e) {
    myCalendarKey mCal = Project5.verifyDate(month+"/"+day+"/"+year);
    if(mCal != null) {
        /* A lot more code down here that only works if mCal is defined */


    }
 }
于 2013-05-01T15:10:51.543 に答える
0

私はそれが良いとは言いませんが、これを行うこともできます。

@Override
public void actionPerformed(ActionEvent e) {
   myCalendarKey mCal = Project5.verifyDate(month+"/"+day+"/"+year);
   if(mCal != null) return;
   ...
}
于 2013-05-01T15:41:02.340 に答える