-1
這不是第一次我的代碼的部分自己執行,我不明白爲什麼。我運行了調試器,然後它似乎回到隨機的代碼行。我的匿名內部類鼠標適配器自己做事
在這種情況下,它完成所有的代碼在MouseAdapter
,但隨後它會馬上回if(boundaryIndex==1)...
—甚至沒有if(me.getSource()==backBtn)
—並執行以下重複代碼,直到boundaryIndex
等於1
,並然後的代碼結束。
MouseAdapter ma =
new MouseAdapter()
{
...
public void mouseClicked(MouseEvent me)
{
...
if(me.getSource()==backBtn) // this statement is called repeatedly.
{
if(boundaryIndex==1)
{
backBtn.setIcon(backDisabled);
for(MouseListener l : backBtn.getMouseListeners())
backBtn.removeMouseListener(l);
}
boundaryIndex--;
restoreBackground();
try{
for (int b : map.territories.get(boundaryIndex).getBorders())
img.getGraphics().drawImage(
blueIco,
map.territories.get(b).getPoint().x - blueIco.getWidth()/2,
map.territories.get(b).getPoint().y - blueIco.getHeight()/2,
null);
} catch(Exception e) {}
img.getGraphics().drawImage(
redIco,
map.territories.get(boundaryIndex).getPoint().x - redIco.getWidth()/2,
map.territories.get(boundaryIndex).getPoint().y - redIco.getHeight()/2,
null);
background.repaint();
}
...
}
};
我希望我能解釋這更好,但我只是不」不瞭解它。它無視我對Java的所有知識。沒有循環,實際上,它確實嚴重地跳到一行代碼中。它跳槽。它從if語句的INSIDE開始,並且if語句在第一次運行之後甚至不被檢查。它從字面上開始。
對不起,但我不認爲這是可回答的,因爲我們無法看到代碼周圍有什麼。什麼是部分?我認爲我們沒有足夠的背景。 (首先,你指的是一個匿名的內部類,但是我的代碼中沒有看到其中的一個)。 – ajb
Atleast添加完整的'MouseAdapter'源代碼。我們不知道給定的代碼是否在循環中執行。另外你可能想提一下如何將鼠標監聽器添加到'backBtn'中並解釋你正在嘗試做什麼。 – KDM
@ user3376587向backBtn添加多少個鼠標監聽器?他們都是同一類型的嗎?你什麼時候添加監聽器? – KDM