0

JDatePicker 是一个用于选择日期的开源 Java GUI 组件

http://sourceforge.net/projects/jdatepicker/

当我将二月设置为创建组件后的月份时,似乎有一个错误。所有其他月份都正常工作。

package jat.examples.DatePicker;

import jat.jdatepicker.JDateComponentFactory;
import jat.jdatepicker.JDatePicker;

import javax.swing.JApplet;
import javax.swing.JComponent;

public class DatePickerExample extends JApplet{

private static final long serialVersionUID = 1920676464239324135L;
JDatePicker depart_date_picker;

public void init() {
    depart_date_picker = JDateComponentFactory.createJDatePicker();
    depart_date_picker.setTextEditable(true);
    depart_date_picker.setShowYearButtons(true);

    add((JComponent) depart_date_picker);

}

public void start() {

    depart_date_picker.getModel().setYear(2010);
    depart_date_picker.getModel().setMonth(1);
    //depart_date_picker.getModel().setMonth(1);
    depart_date_picker.getModel().setDay(15);
    depart_date_picker.getModel().setSelected(true);    
}

}

它不是显示二月,而是显示三月。

在调试器中,我注意到 oldValue 为空。

public void setMonth(int month) {
    int oldMonthValue = this.calendarValue.get(Calendar.MONTH);
    T oldValue = getValue();
    calendarValue.set(Calendar.MONTH, month);
    fireChangeEvent();
    firePropertyChange("month", oldMonthValue, this.calendarValue.get(Calendar.MONTH));
    firePropertyChange("value", oldValue, getValue());
}

果然,当我调用该方法两次时,它正确显示了二月。

    depart_date_picker.getModel().setMonth(1);
    depart_date_picker.getModel().setMonth(1);

可能是变量初始化问题。我是正确的,有人可以解决这个问题,还是我错误地使用了库?

4

2 回答 2

1

我注意到您的包导入是 jat.jdatepicker.JDatePicker,它似乎直接包含在https://sourceforge.net/p/jat的源代码库中

您正在使用的这个版本的 JDatePicker 是原始 JDatePicker 项目的一个分支。虽然开源项目不允许分叉,但如果有需要解决的问题,建议尝试并提交回原始项目。

我建议您宁愿在https://github.com/JDatePicker/JDatePicker使用该项目的最新版本

我针对可以从中央存储库下载的 jdatepicker-1.3.4 测试了您的案例(https://search.maven.org/#artifactdetails%7Corg.jdatepicker%7Cjdatepicker%7C1.3.4%7Cjar

将其包含在 Maven 中:

<dependency>
    <groupId>org.jdatepicker</groupId>
    <artifactId>jdatepicker</artifactId>
    <version>1.3.4</version>
</dependency>

在接下来的测试中,最初选择了二月。

public static void main(String[] args) {
    JFrame testFrame = new JFrame();
    testFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    testFrame.setSize(500, 500);
    JPanel jPanel = new JPanel();

    JDatePicker picker = new JDateComponentFactory().createJDatePicker();
    picker.setTextEditable(true);
    picker.setShowYearButtons(true);
    jPanel.add((JComponent) picker);

    picker.getModel().setYear(2010);
    picker.getModel().setMonth(1);
    //picker.getModel().setMonth(1);
    picker.getModel().setDay(15);
    picker.getModel().setSelected(true);

    JPanel datePanel = new JPanel();
    datePanel.setLayout(new BorderLayout());
    datePanel.add(jPanel, BorderLayout.WEST);
    BorderLayout fb = new BorderLayout();
    testFrame.setLayout(fb);
    testFrame.getContentPane().add(datePanel, BorderLayout.WEST);
    testFrame.setVisible(true);
}
于 2014-10-07T19:12:00.590 回答
0

This may be dependent on when the test is run. I noticed you asked this near the end of a month. And February only has 28 days.

I suspect what happened is that you ran your code on the 29th, 30th, or 31st of the month, so the initial selection was something like 2012-11-30. When you set the month value to 01 (Feb), the day value stays 30, producing a "denormalized" date that rolls over in to the next month when the Calendar tries to determine the actual date from the field values. Like "carrying" the overflow when you're doing decimal addition. This will give you a date like March 1. Then if you set the month field again to February, this time the day field is within the valid range for February, and you don't get overflow.

There is a pending Pull Request on the JDatePicker project to deal with this. In the mean time, you could use one of the methods that sets the entire date in a single call instead of setting the year, month, and day fields separately. Or set the day field before setting the month field to avoid the rollover condition.

于 2015-05-07T22:00:07.327 回答