为什么此JAVA代码无法解析构造函数?

问题描述 投票:0回答:1

我正在尝试使用Swing作为大学项目制作Calendar Selection框。

[在编写通过将数组插入JComboBox来初始化项目的代码时,当“ Cannot resolve Constructor 'JComboBox(int[])'”部分中出现“ public JComboBox YearComboBox=new JComboBox(years);”错误时,我不知道该怎么办。

这是我的代码。 (比现有代码短。)

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

public class IDfind {
    private int years[]=setYear();
    public JComboBox YearComboBox=new JComboBox(years);

    public int[] setYear() {
        int nowYear = Calendar.getInstance().get(Calendar.YEAR);
        int[] year = new int[nowYear];
        for (int i = 0; i <= nowYear - 1900; i++) {
            year[i] = 1900 + i;
            System.out.println(i);
        }
        return year;
    }
java swing awt
1个回答
0
投票

因为没有JComboBox的构造函数支持int []作为参数。检查可用的构造函数here

例如使用向量

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

public class test {
    private Vector years = setYear();
    public JComboBox YearComboBox = new JComboBox(years);

    public Vector setYear() {
        int nowYear = Calendar.getInstance().get(Calendar.YEAR);
        Vector year = new Vector(nowYear);
        for (int i = 0; i <= nowYear - 1900; i++) {
            year.add(1900 + i);
            System.out.println(i);
        }
    return year;
    }
}

0
投票

看起来好像您正在尝试将JComboBox​(E[] items)构造函数用于JComboBox<E>。但是,由于int不是引用类型,因此E不能为int,并且E[]不能为int[]

您需要使用Integer[]而不是int[]。从原始类型切换为泛型,您还需要使用JComboBox<Integer>

© www.soinside.com 2019 - 2024. All rights reserved.