I'm trying to populate a combo box using enums based on a player's enum.
我正在尝试使用基于播放器枚举的枚举来填充组合框。
For example, upon initializing the game, the player chooses a faction (FactionType). The FactionType enum is used in the enum values of each unit (ShipType). The ShipType(FactionType) enum so that each ship can be switched for the appropriate object and instantiated. The problem I'm having is how to populate the combo box with ONLY the units of that faction that the player has chosen. Example code below:
例如,在初始化游戏时,玩家选择一个阵营(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
}
so is it possible to create a ComboBoxModel after filtering out any unit type that isn't of the player's faction? Normally I would just make a conditional statement to check player.faction = faction but not entirely certain how to create the ComboBoxModel so any help is appreciated.
那么在筛选出不属于玩家阵营的任何单位类型后,是否可以创建一个ComboBoxModel?通常我会做一个条件语句来检查player.faction = faction但不完全确定如何创建ComboBoxModel所以任何帮助都是值得赞赏的。
2 个解决方案
#1
1
Instead of separate enum
classes, consider an EnumSet
for each faction, as outlined here.
如此处所述,考虑每个派系的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);
}
#2
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:
而不是拥有FactionType和ShipType,将派系分成单独的枚举会更简单。这样每个派系数据都可以更好地封装,并且更容易为组合框模型设置。所以现在我的枚举将如下所示:
public enum factionOne {
unit1, unit2, unit3, unit4, unit5;
}
public enum factionTwo {
unit1, unit2, unit3, unit4, unit5, unit6;
}
etc......
#1
1
Instead of separate enum
classes, consider an EnumSet
for each faction, as outlined here.
如此处所述,考虑每个派系的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);
}
#2
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:
而不是拥有FactionType和ShipType,将派系分成单独的枚举会更简单。这样每个派系数据都可以更好地封装,并且更容易为组合框模型设置。所以现在我的枚举将如下所示:
public enum factionOne {
unit1, unit2, unit3, unit4, unit5;
}
public enum factionTwo {
unit1, unit2, unit3, unit4, unit5, unit6;
}
etc......