2013-05-01 82 views
0

我有一個動作偵聽器,如果變量值爲null,我想取消當前迭代。有沒有一種方法可以讓ActionListener取消?

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語句,並如果mCal != null它做的一切,如果mCal == null什麼也不做,但有一個更好的方式來做到這一點?

+0

*「有沒有更好的方法來做到這一點?」*這種方式沒有錯。或者,當值爲空時禁用控制或「操作」 - 那麼事件不會首先被觸發! – 2013-05-01 15:22:34

+0

@Andrew上面的方法並沒有取消行動(這似乎是要求 - 雖然我可能是錯的)。它只是停止處理這個特定的事件監聽器。 – StuPointerException 2013-05-01 15:38:02

+0

@StuPointerException *「雖然我可能是錯的」*是的,你可能,我不是特別感興趣的人誰不是OP的投機。 – 2013-05-01 15:40:29

回答

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 */ 


    } 
} 
+0

這會停止動作偵聽器的邏輯,但不會取消事件(我認爲這是要求)。 – StuPointerException 2013-05-01 15:29:04

0

我不會說這是更好,但你也可以這樣做。

@Override 
public void actionPerformed(ActionEvent e) { 
    myCalendarKey mCal = Project5.verifyDate(month+"/"+day+"/"+year); 
    if(mCal != null) return; 
    ... 
} 
相關問題