在我的独立Java应用程序中使用Toeder JDateChooser
,如下面的代码片段所示。这里的目的是:
- 将日历实例移动到一年中所需的星期,然后
- 设置一周的开始日期和结束日期。这些日期保存在两个JDateChooser实例中,即
jDateChooserBookBegin
和jDateChooserBookEnd
。
Calendar c = Calendar.getInstance();
int currentWeekOfYear = c.get(Calendar.WEEK_OF_YEAR);
int desiredWeekOfYear = jComboBookWeekMainPanel.getSelectedIndex() + 1;
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
for (int i = currentWeekOfYear; i < desiredWeekOfYear; i++) {
c.add(Calendar.DAY_OF_WEEK, 7); //e.g. set to next Monday
}
jDateChooserBookBegin.setDate(c.getTime()); // ***** HERE *****
c.add(Calendar.DAY_OF_WEEK, 6); //set to the end of week e.g. Sunday
jDateChooserBookEnd.setDate(c.getTime());
问题:假设上面的代码片段在一个方法中执行。我在neatbeans的调试模式下检查了代码,我发誓当执行到达用虚线箭头标记的行时,其余行根本不执行,并且封装方法立即返回给调用者。这导致jDateChooserBookEnd
没有被设置为正确的日期,因此我的程序以不希望的方式运行。
问题:为什么JDateChooser
类的setDate
方法通过返回调用者导致其余行不被执行?这上面有已知的bug吗?你有什么线索吗?这看起来不太可能,但它正在发生。我正在使用Java 6.
完整的方法内容如下所示。上面的代码片段是下面代码的简化版本。在程序中,我有两个jbutton,分别命名为next week和previous week。每当单击其中一个按钮时,相关的actionPerformed方法就会调用下面的方法。
private void update_DateFieldsInMainPanel() {
Calendar c = Calendar.getInstance();
int currentWeekOfYear = c.get(Calendar.WEEK_OF_YEAR);
int desiredWeekOfYear = jComboBookWeekMainPanel.getSelectedIndex() + 1; c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
if (desiredWeekOfYear ==currentWeekOfYear) {
jDateChooserBokMainFrom.setDate(c.getTime());
c.add(Calendar.DAY_OF_WEEK, 6);
jDateChooserBokMainTill.setDate(c.getTime());
}
else if (desiredWeekOfYear > currentWeekOfYear) {
for (int i = currentWeekOfYear; i < desiredWeekOfYear; i++) {
c.add(Calendar.DAY_OF_WEEK, 7);
}
jDateChooserBokMainFrom.setDate(c.getTime());
c.add(Calendar.DAY_OF_WEEK, 6);
jDateChooserBokMainTill.setDate(c.getTime());
}
else {
for (int i = currentWeekOfYear; i > desiredWeekOfYear; i--) {
c.add(Calendar.DAY_OF_WEEK, -7);
}
jDateChooserBokMainFrom.setDate(c.getTime());
c.add(Calendar.DAY_OF_WEEK, 6);
jDateChooserBokMainTill.setDate(c.getTime());
}
}
尝试捕获Throwable而不是Exception。