如何将JComboBox的值作为枚举值获取

huangapple go评论70阅读模式
英文:

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();

huangapple
  • 本文由 发表于 2023年5月28日 02:42:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定