我在我的独立Java应用程序中使用Toeder 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
无法设置为正确的日期,因此我的程序会以不受欢迎的方式运行。
问题:为什么setDate
类的JDateChooser
方法会导致其余行不通过返回调用者来执行?这有一个已知的错误吗?你有什么线索吗?这看起来很不可能,但它正在发生。我使用的是Java 6。
完整方法内容如下。上面的代码片段是您在下面看到的简化版本。在程序中,我有两个jButton,下周和前一周命名。只要单击其中一个按钮,相关的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());
}
}
答案 0 :(得分:0)
尝试捕捉 Throwable 而不是Exception。