0

我正在尝试用数字 25 到 50 填充我的 JComboBox,这就是我所做的,

--at the variable declarations--

String[] val = new String[25];
JComboBox box1 = new JComboBox();

--and in the "main"--

for(int i=0; i==val.length; i++){
   val[i] = Integer.toString(i+25);
}

box1.setModel(new DefaultComboBoxModel(val));

但最后,JComboBox 只显示空白 25 个空格,但不显示应保存在数字 25 - 50 的字符串数组中的数字。请帮助。

4

2 回答 2

4

尝试改变

for(int i=0; i==val.length; i++){

for(int i=0; i<val.length; i++){
于 2013-03-12T08:10:23.830 回答
2
val[i] = Integer.toString(i+25);
  • 为 JComboBox 使用正确的数据类型,为 ComboBoxModel 使用 Integer

  • 例如(在模拟 ComboBoxModel 的相同代码的 API 中实现的代码中使用 Vector)

在此处输入图像描述

import java.awt.GridLayout;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class ComboBoxIntegerModel {

    private JComboBox comboBoxDouble;
    private JComboBox comboBoxInteger;
    private JComboBox comboBoxBoolean;
    private JComboBox comboBoxIcon;
    private Vector<Double> doubleVector = new Vector<Double>();
    private Vector<Integer> integerVector = new Vector<Integer>();
    private Vector<Boolean> booleanVector = new Vector<Boolean>();
    private Vector<Icon> iconVector = new Vector<Icon>();
    private Icon icon1 = ((UIManager.getIcon("OptionPane.errorIcon")));
    private Icon icon2 = (UIManager.getIcon("OptionPane.informationIcon"));
    private Icon icon3 = (UIManager.getIcon("OptionPane.warningIcon"));
    private Icon icon4 = (UIManager.getIcon("OptionPane.questionIcon"));

    public ComboBoxIntegerModel() {
        doubleVector.addElement(1.001);
        doubleVector.addElement(10.00);
        doubleVector.addElement(0.95);
        doubleVector.addElement(4.2);
        comboBoxDouble = new JComboBox(doubleVector);
        integerVector.addElement(1);
        integerVector.addElement(2);
        integerVector.addElement(3);
        integerVector.addElement(4);
        comboBoxInteger = new JComboBox(integerVector);
        booleanVector.add(Boolean.TRUE);
        booleanVector.add(Boolean.FALSE);
        comboBoxBoolean = new JComboBox(booleanVector);
        iconVector.addElement(icon1);
        iconVector.addElement(icon2);
        iconVector.addElement(icon3);
        iconVector.addElement(icon4);
        comboBoxIcon = new JComboBox(iconVector);
        JFrame frame = new JFrame("");
        frame.setLayout(new GridLayout(2,2,5,5));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(comboBoxDouble);
        frame.add(comboBoxInteger);
        frame.add(comboBoxBoolean);
        frame.add(comboBoxIcon);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ComboBoxIntegerModel comboBoxModel = new ComboBoxIntegerModel();
            }
        });
    }
}
于 2013-03-12T08:27:24.670 回答