0

我正在尝试使用基于玩家枚举的枚举来填充组合框。

例如,在初始化游戏时,玩家选择一个派系(FactionType)。FactionType 枚举用于每个单元(ShipType)的枚举值。ShipType(FactionType) 枚举,以便每艘船都可以切换为适当的对象并实例化。我遇到的问题是如何仅使用玩家选择的那个派系的单位来填充组合框。下面的示例代码:

public enum FactionType {
faction1, faction2, faction3
}

public enum ShipType {
Ship1(faction1), Ship2(faction2), Ship3(faction3)

private FactionType faction;

private ShipType(FactionType faction)
this.faction = faction;
}

public FactionType getFaction() {
return this.faction
}

那么是否可以在过滤掉任何不属于玩家派系的单位类型后创建一个 ComboBoxModel ?通常我只会做一个条件语句来检查 player.faction = faction 但不完全确定如何创建 ComboBoxModel 所以任何帮助表示赞赏。

4

2 回答 2

1

如此处所述,请考虑为每个派系设置一个而不是单独enum类。EnumSet

public enum Ship {

    Unit1, Unit2, Unit3, Unit4, Unit5, Unit6;
    public static Set<Ship> factionOne = EnumSet.range(Unit1, Unit5);
    public static Set<Ship> factionTwo = EnumSet.allOf(Ship.class);
}
...
DefaultComboBoxModel modelOne = new DefaultComboBoxModel();
for (Ship ship : Ship.factionOne) {
    modelOne.addElement(ship);
}
于 2013-04-21T03:26:40.697 回答
0

I believe I've over complicated my code.

Instead of having a FactionType and ShipType, it would be simpler to split the factions into separate enums. so that each factions data is better encapsulated and simpler to set for the combo box model. so now my enums will look like so:

public enum factionOne {

unit1, unit2, unit3, unit4, unit5;

}

public enum factionTwo {

unit1, unit2, unit3, unit4, unit5, unit6;

}

etc......
于 2013-04-21T01:50:55.817 回答