JCombobox接受字符数组

时间:2014-10-27 01:19:17

标签: java arrays swing jcombobox

我希望JCombobox接受一个字符数组。我似乎无法为此找到解决方法。基本上,我希望JCombobox保持价值观' 0' 0 - ' 9',但作为字符而不是int。

char[] levels = {'0', '1', '2', '3'};
skillLevelCombo = new JComboBox<Object>(levels); //Does not work

我如何解决这个问题?我制作一个Character数组吗?如果是这样,我如何在以后获取char值?

1 个答案:

答案 0 :(得分:3)

这很好用:

  Character[] levels = {'0', '1', '2', '3'};
  skillLevelCombo = new JComboBox<Character>(levels);

如,

import javax.swing.JComboBox;
import javax.swing.JOptionPane;

public class ComboFun {
   private static JComboBox<Character> skillLevelCombo;

   public static void main(String[] args) {
      Character[] levels = {'0', '1', '2', '3'};
      skillLevelCombo = new JComboBox<Character>(levels); 

      JOptionPane.showMessageDialog(null, skillLevelCombo);
   }
}

请注意,整数也很有效:

  Integer[] levels = {0, 1, 2, 3};
  final JComboBox<Integer> skillLevelCombo = new JComboBox<Integer>(levels);