用int填充JComboBox [] [英] Populate a JComboBox with an int[]

查看:86
本文介绍了用int填充JComboBox []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以用int []填充JComboBox?我正在编写一个代码,该代码将使用以年份(整数)填充的JComboBox.

Is it possible to populate a JComboBox with an int[]? I am writing a code that will use a JComboBox populated with years (in integers).

我写的代码是这样的:

int[] birthYear = new int[currentYear]; //currentYear is an int variable
int inc=1;
for(int i=0;i<currentYear;i++)
{
    birthYear[i]= inc;
    inc++;
}

birthYearBox = new JComboBox(birthYear); //this is the line in which the error occurs

我希望它们在ComboBox中为整数,以便可以从中减去.我是否必须用字符串填充ComboBox,然后在输入字符串后使它们成为整数?还是有办法用int []实际填充ComboBox?

I want these to integers in the ComboBox so that I can subtract from them. Do I have to populate the ComboBox with strings, then make them integers after they have been input? Or is there a way to actually populate the ComboBox with the int[]?

推荐答案

JCombobox是泛型的,但是Java泛型不支持原始类型(而int是原始类型).

JCombobox is generic, but Java generics don't support primitive type (and int is a primitive type).

因此,请使用Integer数组:

Integer[] birthYear = new Integer[currentYear]; //currentYear is an int variable
int inc=1;
for(int i=0;i<currentYear;i++){
    birthYear[i]= inc;
    inc++;
}
JComboBox<Integer> birthYearBox = new JComboBox<>(birthYear);

这篇关于用int填充JComboBox []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆