英文:
How to get JComboBox value as enum value
问题
我如下创建了一个JComboBox:
mode = new JComboBox(SpawnMode.values());
SpawnMode枚举如下:
public enum SpawnMode {
POINT,
RANDOM,
INWARD_CIRCLE,
RANDOM_CIRCLE
}
我如何从JComboBox中获取选定的值作为枚举值,然后分配给它,如下所示?
SpawnMode value = selected_value
英文:
I've created JComboBox like this:
mode = new JComboBox(SpawnMode.values());
SpawnMode enum looks like this:
public enum SpawnMode {
POINT,
RANDOM,
INWARD_CIRCLE,
RANDOM_CIRCLE
}
How do I get selected value from JComboBox as a enum value to assign it like below?
SpawnMode value = selected_value
答案1
得分: 2
将您的_selected_value_强制转换为枚举类型。
SpawnMode value = (SpawnMode) selected_value;
在这种情况下,使用_JComboBox#getSelectedItem_。
SpawnMode value = (SpawnMode) mode.getSelectedItem();
英文:
Cast you're selected_value to the enum type.
SpawnMode value = (SpawnMode) selected_value
In this case, use JComboBox#getSelectedItem.
SpawnMode value = (SpawnMode) mode.getSelectedItem();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论