2013-04-24 55 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()返回。