0

JComboBox に 25 から 50 の数字を入力しようとしていますが、これが私が行ったことです。

--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

Try changing

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

to

for(int i=0; i<val.length; i++){
于 2013-03-12T08:10:23.830 に答える
2
val[i] = Integer.toString(i+25);
  • JComboBoxには適切なデータ型を使用し、ComboBoxModelには整数を使用します

  • たとえば(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 に答える