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 mCal != null,并且什么都不做 if 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) return;
   ...
}
于 2013-05-01T15:41:02.340 回答
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 回答