簡體   English   中英

jcalendar選擇了2月3月30日

[英]JCalendar sets to March 30 when February is chosen

代碼只包含月份和年份,可以在中選擇,默認情況下,2 月的日期為 30 和 28,當我選擇 2 月時出現問題,自動設置為 3 月 30 日; 這是代碼; 它在PropertyChange事件中。

Calendar cal = PFI.getCalendar();
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
if(month==1){
    cal.set(year, 1 , 28);
    PFI.setCalendar(cal);
}
else
{
    cal.set(year, month , 30);
    PFI.setCalendar(cal);
}

結合@Ole VV@Catalina Island的建議,下面的片段說明了GridLayout中的JYearChooserJMonthChooser 然后,一個普通的偵聽器使用java.time來顯示所選年份和月份的最后一天。

年月圖片


JPanel panel = new JPanel(new GridLayout(1, 0));
JMonthChooser jmc = new JMonthChooser();
JYearChooser jyc = new JYearChooser();
JLabel label = new JLabel("Last day of month:");
label.setHorizontalAlignment(JLabel.RIGHT);
JTextField last = new JTextField(8);
label.setLabelFor(last);
PropertyChangeListener myListener = new PropertyChangeListener() {
    @Override
    public void propertyChange(PropertyChangeEvent e) {
        LocalDate ld = LocalDate.of(jyc.getYear(), jmc.getMonth() + 1, 1)
            .with(TemporalAdjusters.lastDayOfMonth());
        last.setText(ld.format(DateTimeFormatter.ISO_DATE));
    }
};
myListener.propertyChange(null);
jmc.addPropertyChangeListener("month", myListener);
jyc.addPropertyChangeListener("year", myListener);
panel.add(jyc);
panel.add(jmc);
panel.add(label);
panel.add(last);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM