2013-04-24 46 views
0

我希望這是有道理的,並且有這個節目的整潔方式。我想要弄清楚如何迭代ArrayList,並將按鈕上的每個標籤設置爲每個Territory包含的int值,然後更改這個區域的顏色。按鈕背景以對應其擁有者。ArrayList和收集問題

很長的路被設置爲每個按鈕的標籤,然後使用的if-else檢查所有者和設置正確的背景色,但是,這會導致數以百計的重複的代碼行。

btnEgy.setLabel(Territory.EGYPT.units()); 
    if(Territory.EGYPT.getOwner().toString().equals("Player 1")) 
    { 
     btnEgy.setBackground(Color.BLUE); 
    } 
    else if(Territory.EGYPT.getOwner().toString().equals("Player 2")) 
    { 
     btnEgy.setBackground(Color.RED); 
    } 
    else if (Territory.EGYPT.getOwner().toString().equals("Player 3")) 
    { 
     btnEgy.setBackground(Color.GREEN); 
    } 
    else if (Territory.EGYPT.getOwner().toString().equals("Player 4")) 
    { 
     btnEgy.setBackground(Color.YELLOW); 
    } 
btnEus.setLabel(Territory.E_UNITEDSTATES.units()); 
    if(Territory.E_UNITEDSTATES.getOwner().toString().equals("Player 1")) 
    { 
     btnEus.setBackground(Color.BLUE); 
    } 
    else if(Territory.E_UNITEDSTATES.getOwner().toString().equals("Player 2")) 
    { 
     btnEus.setBackground(Color.RED); 
    } 
    else if (Territory.E_UNITEDSTATES.getOwner().toString().equals("Player 3")) 
    { 
     btnEus.setBackground(Color.GREEN); 
    } 
    else if (Territory.E_UNITEDSTATES.getOwner().toString().equals("Player 4")) 
    { 
     btnEus.setBackground(Color.YELLOW); 
    } 
+1

,你的問題是.... – WeMakeSoftware 2013-04-24 19:16:33

+0

@Funtik問題是,如何避免重複的代碼。 – Smit 2013-04-24 19:17:52

+0

什麼是'Territory.E_UNITEDSTATES','Territory.EGYPT'是枚舉? – Ilya 2013-04-24 19:19:07

回答

0

假設你有相同數量的按鈕和地區,

Iterator<Button> itr1 = buttons.iterator(); 
Iterator<Territory> itr2 = territories.iterator(); 
while(itr1.hasNext() && itr2.hasNext()) { 
    Button button = itr1.next(); 
    Territory territory = itr2.next(); 
    // set button data to territory data 
} 

If the collection sizes don't match then you'll need to figure out if you want to terminate when you reach the end of the shorter collection, or if you want to keep looping through the shorter collection until you reach the end of the longer collection. 
2
HashMap<String, Color> playerMap = new HashMap<String, Color>(); 
playerMap.add("Player 1", Color.BLUE); 
playerMap.add("Player 2", Color.RED); 

然後

btnEgy.setBackground(playerMap.get(Territory.EGYPT.getOwner().toString())); 
+0

這是一個有點可行的「HAX」的解決方案,如果你問我。如果採取更多的面向對象的方法,將會更清楚地看到正在發生的事情以及整個結構。您可以輕鬆地讓店主直接返回一種顏色。 – ddmps 2013-04-24 19:29:53

+0

這取決於,看來該領土和它的主人是某種域模型,這意味着像背景顏色的UI特性不屬於它的。顏色映射是一個簡單而合理的解決方案。 – Robin 2013-04-24 19:38:17

+0

@Pescis你可以。好點子。但是,經常有其他方法可以做事,我仍然認爲我的解決方案如羅賓所說的那樣簡單合理。 – KyleM 2013-04-24 19:45:06

0

如何在Java中使用的功能?

主代碼

setValues(btnEgy,Territory.EGYPT); 
setValues(btnEus,Territory.E_UNITEDSTATES); 

功能碼

public void setValues(Button btn,Territory t){ 
    btn.setLabel(t.units()); 

    if(t.getOwner().toString().equals("Player 1")) 
    { 
     btn.setBackground(Color.BLUE); 
    } 
    else if(t.getOwner().toString().equals("Player 2")) 
    { 
     btn.setBackground(Color.RED); 
    } 
    else if (t.getOwner().toString().equals("Player 3")) 
    { 
     btn.setBackground(Color.GREEN); 
    } 
    else if (t.getOwner().toString().equals("Player 4")) 
    { 
     btn.setBackground(Color.YELLOW); 
    } 

} 
0

如果有按鈕的列表和相等長度的地區的列表,其中鍵[0]是用於土[0]等上...

final Iterator<Button> buttonI = buttons.iterator(); 
final Iterator<Territory> territoryI = territories.iterator(); 
while (territoryI.hasNext() && buttonI.hasNext()) { 
    final Button button = buttonI.next(); 
    final Territory territory = territoryI.next(); 
    button.setBackground(territory.getOwner().getColor()); 
    button.setLabel(territory.units()); 
} 

在這裏,我假設你可以添加一個getColor()方法將CL屁股由territory.getOwner()返回。