0

我正在使用 jComboBox 显示日期、月份、年份,并将 jComboBox 中的第一项添加为 DD、MM、YY(我在 jComboBox 的模型属性中将第一项设置为“DD、YY”)。显示日期和我用于循环的年份值。代码如下..

    //for date
    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    for(int k=1;k<=31;k++)
    {

        jComboBox1.addItem(k);
    }
} 

    //for year
    private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    for(int n=1980;n<=2012;n++)
    {

        jComboBox3.addItem(n);
    }
}                          

所以我的问题是当我选择 DD 和 YY jComboBox 时,它没有动态显示值。例如,当我单击列表中的 DD jComboBox 时,它显示另一个项目为 DD,然后如果我只选择意味着它显示所有值对于 DD 和 YY。为什么??以及如何动态显示它。

4

4 回答 4

1

每次用户单击它时,您的代码都会初始化 JComboBox,这是错误的

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           

////////////here you put the action like getSelectedItem() which return the item 

} 
于 2012-12-28T13:42:24.733 回答
1
于 2012-12-28T12:58:03.153 回答
1

检查此代码

public class ComboBoxDatePicker extends JPanel implements ItemListener {

    private static final long serialVersionUID = 1L;
    private JComboBox myDay;
    private JComboBox myMonth;
    private JComboBox myYear;
    private Collection<ItemListener> myListeners;

    public ComboBoxDatePicker(String name) {
        this(name, new Date(System.currentTimeMillis()));
    }

    public ComboBoxDatePicker(String name, Date date) {
        super(new FlowLayout(FlowLayout.LEFT));
        myListeners = new HashSet<ItemListener>();

        myDay = new JComboBox(new RangeModel(1, 31));
        myMonth = new JComboBox(new RangeModel(1, 12));
        myYear = new JComboBox(new RangeModel(2000, 2020));

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        myDay.setSelectedItem(calendar.get(Calendar.DAY_OF_MONTH));
        myMonth.setSelectedItem(calendar.get(Calendar.MONTH) + 1);
        myYear.setSelectedItem(calendar.get(Calendar.YEAR));

        myDay.addItemListener(this);
        myMonth.addItemListener(this);
        myYear.addItemListener(this);

        add(new JLabel(name));
        add(myDay);        
        add(myMonth);
        add(myYear);

        doLayout();
    }

    public void setEnabled(boolean enabled) {
        myDay.setEnabled(enabled);
        myMonth.setEnabled(enabled);
        myYear.setEnabled(enabled);
    }

    public Date getDate() {
        if (!myDay.isEnabled()) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.DAY_OF_MONTH, (Integer) myDay.getSelectedItem());
        calendar.set(Calendar.MONTH, (Integer) myMonth.getSelectedItem() - 1);
        calendar.set(Calendar.YEAR, (Integer) myYear.getSelectedItem());
        return calendar.getTime();
    }

    private static class RangeModel implements ComboBoxModel {

        private int myMin;
        private int myMax;
        private Object mySelection;

        public RangeModel(int min, int max) {
            myMin = Math.min(min, max);
            myMax = Math.max(min, max);
            mySelection = myMin;
        }
        public Object getSelectedItem() {
            return mySelection;
        }
        public void setSelectedItem(Object anItem) {
            mySelection = anItem;
        }
        public int getSize() {
            return myMax - myMin + 1;
        }
        public Object getElementAt(int index) {
            return myMin + index;
        }
        public void addListDataListener(ListDataListener l) {
        }
        public void removeListDataListener(ListDataListener l) {
        }
    }

    public void addItemListener(ItemListener l) {
        myListeners.add(l);
    }

    public void itemStateChanged(ItemEvent e) {
        for (ItemListener listener : myListeners) {
            listener.itemStateChanged(e);
        }
    }
}

你可以在任何需要的地方使用它

  JFrame frame = new JFrame() ;
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  ComboBoxDatePicker chooser = new ComboBoxDatePicker("Choose Date");
  frame.add(chooser);
  frame.pack();
  frame.setVisible(true);
于 2012-12-28T14:22:58.620 回答
0

如果您使用 jframe,那么您可以编写代码以WindowActivated在 jframe 的事件中动态地将值添加到组合框,如下所示:

private void formWindowActivated(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
    //for Day
    for(int i=1;i<=31;i++)
    {
        jComboBox1.addItem(i);
    }
    //for Month
    for(int i=1;i<=12;i++)
    {
        jComboBox2.addItem(i);
    }
}
于 2015-07-30T10:25:03.343 回答