import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
String[] w
= { "Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
}; String[] m
= { "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
}; JButton btnPrevMonth, btnNextMonth
; int year, month;
public MyWidget() {
this.setResizable(false);
this.
setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE); this.
year = calendar.
get(Calendar.
YEAR); this.
month = calendar.
get(Calendar.
MONTH); this.
btnPrevMonth = new JButton("<"); this.btnPrevMonth.addActionListener(this);
this.
lblMonthYear = new JLabel(); this.
lblMonthYear.
setHorizontalAlignment(JLabel.
CENTER); this.
btnNextMonth = new JButton(">"); this.btnNextMonth.addActionListener(this);
pnlFooter.add(lblTime);
showCalendar();
this.pack();
}
private void showCalendar() {
p2.removeAll();
p2.repaint();
for(int i=0; i<7; i++) {
l.
setHorizontalAlignment(JLabel.
CENTER); p2.add(l);
}
this.lblMonthYear.setText(m[month] + " " + year);
// 縦がどっちで横がどっちか考えるの('A`)マンドクセ. どっちも7でいーや.
// DAY_OF_WEEKは日曜に1, 月曜に2, ... 土曜に7を返すんだってー.
// 俺はカレンダーは月曜始まりが好きだから, 月曜が0, 日曜が6になってほしいんだよね.
// だったら5を足せばいいんじゃね?
int dayOfWeekOn1st
= (calendar.
get(Calendar.
DAY_OF_WEEK) + 5) % 7; for(int i=0; i<dayOfWeekOn1st; i++) {
days
[0][i
].
setHorizontalAlignment(JLabel.
CENTER); p2.add(days[0][i]);
}
int day = 1;
for(int i=dayOfWeekOn1st; i<7; i++) {
days
[0][i
] = new JLabel("" + (day
++)); days
[0][i
].
setHorizontalAlignment(JLabel.
CENTER); p2.add(days[0][i]);
}
here: for(int r=1; r<7; r++) {
for(int i=0; i<7; i++) {
if(day
> calendar.
getActualMaximum(Calendar.
DATE)) { break here
; } days
[r
][i
] = new JLabel("" + day
); days
[r
][i
].
setHorizontalAlignment(JLabel.
CENTER); day += 1;
p2.add(days[r][i]);
}
}
}
@Override public void run() {
for( ; ; ) {
try {
lblTime.setText(formatter.format(now.getTime()));
ex.printStackTrace();
}
}
}
Object source
= e.
getSource(); if(source == btnNextMonth) {
if(month == 11) {
year += 1;
month = 0;
} else {
month += 1;
}
showCalendar();
} else if(source == btnPrevMonth) {
if(month == 0) {
year -= 1;
month = 11;
} else {
month -= 1;
}
showCalendar();
}
}
public static void main
(String[] args
) { MyWidget self = new MyWidget();
self.setVisible(true);
}
}
aW1wb3J0IGphdmEuYXd0Lio7CmltcG9ydCBqYXZhLmF3dC5ldmVudC4qOwppbXBvcnQgamF2YS50ZXh0Lio7CmltcG9ydCBqYXZhLnV0aWwuKjsKaW1wb3J0IGphdmF4LnN3aW5nLio7CgpwdWJsaWMgY2xhc3MgTXlXaWRnZXQgZXh0ZW5kcyBKRnJhbWUgaW1wbGVtZW50cyBBY3Rpb25MaXN0ZW5lciwgUnVubmFibGUgewogICAgU3RyaW5nW10gdyA9IHsgIk1vbiIsICJUdWUiLCAiV2VkIiwgIlRodSIsICJGcmkiLCAiU2F0IiwgIlN1biIsIH07CiAgICBTdHJpbmdbXSBtID0geyAiSmFuIiwgIkZlYiIsICJNYXIiLCAiQXByIiwgIk1heSIsICJKdW4iLCAiSnVsIiwgIkF1ZyIsICJTZXAiLCAiT2N0IiwgIk5vdiIsICJEZWMiLCB9OwogICAgSlBhbmVsIHAyOwogICAgSkxhYmVsIGxibE1vbnRoWWVhciwgbGJsVGltZTsKICAgIEpCdXR0b24gYnRuUHJldk1vbnRoLCBidG5OZXh0TW9udGg7CiAgICBpbnQgeWVhciwgbW9udGg7CiAgICBDYWxlbmRhciBjYWxlbmRhcjsKICAgIERhdGVGb3JtYXQgZm9ybWF0dGVyOwoKICAgIHB1YmxpYyBNeVdpZGdldCgpIHsKICAgICAgICB0aGlzLnNldFJlc2l6YWJsZShmYWxzZSk7CiAgICAgICAgdGhpcy5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pOwogICAgICAgIHRoaXMuZm9ybWF0dGVyID0gbmV3IFNpbXBsZURhdGVGb3JtYXQoIkhIOm1tOnNzIik7CiAgICAgICAgSlBhbmVsIHBubEhlYWRlciA9IG5ldyBKUGFuZWwobmV3IEJvcmRlckxheW91dCgpKTsKICAgICAgICBKUGFuZWwgcG5sQ2VudGVyID0gbmV3IEpQYW5lbChuZXcgQm9yZGVyTGF5b3V0KCkpOwogICAgICAgIEpQYW5lbCBwbmxGb290ZXIgPSBuZXcgSlBhbmVsKCk7CiAgICAgICAgcG5sSGVhZGVyLmFkZChwMSwgQm9yZGVyTGF5b3V0Lk5PUlRIKTsKICAgICAgICBwbmxDZW50ZXIuYWRkKHAyLCBCb3JkZXJMYXlvdXQuQ0VOVEVSKTsKICAgICAgICBwbmxGb290ZXIuYWRkKGxibFRpbWUpOwogICAgICAgIHRoaXMuYWRkKHBubEhlYWRlciwgQm9yZGVyTGF5b3V0Lk5PUlRIKTsKICAgICAgICB0aGlzLmFkZChwbmxDZW50ZXIsIEJvcmRlckxheW91dC5DRU5URVIpOwogICAgICAgIHRoaXMuYWRkKHBubEZvb3RlciwgQm9yZGVyTGF5b3V0LlNPVVRIKTsKICAgICAgICBzaG93Q2FsZW5kYXIoKTsKICAgICAgICB0aGlzLnBhY2soKTsKICAgIH0KCiAgICBwcml2YXRlIHZvaWQgc2hvd0NhbGVuZGFyKCkgewogICAgICAgIHAyLnJlbW92ZUFsbCgpOwogICAgICAgIHAyLnJlcGFpbnQoKTsKICAgICAgICBmb3IoaW50IGk9MDsgaTw3OyBpKyspIHsKICAgICAgICAgICAgSkxhYmVsIGwgPSBuZXcgSkxhYmVsKHdbaV0pOwogICAgICAgICAgICBsLnNldEhvcml6b250YWxBbGlnbm1lbnQoSkxhYmVsLkNFTlRFUik7CiAgICAgICAgICAgIHAyLmFkZChsKTsKICAgICAgICB9CiAgICAgICAgdGhpcy5sYmxNb250aFllYXIuc2V0VGV4dChtW21vbnRoXSArICIgIiArIHllYXIpOwogICAgICAgIC8vIOe4puOBjOOBqeOBo+OBoeOBp+aoquOBjOOBqeOBo+OBoeOBi+iAg+OBiOOCi+OBrignQWAp776P776d776E776e7724772+LiDjganjgaPjgaHjgoI344Gn44GE44O844KELgogICAgICAgIEpMYWJlbFtdW10gZGF5cyA9IG5ldyBKTGFiZWxbN11bN107CiAgICAgICAgY2FsZW5kYXIuc2V0KENhbGVuZGFyLllFQVIsIHllYXIpOwogICAgICAgIGNhbGVuZGFyLnNldChDYWxlbmRhci5NT05USCwgbW9udGgpOwogICAgICAgIGNhbGVuZGFyLnNldChDYWxlbmRhci5EQVRFLCAxKTsKICAgICAgICAvLyBEQVlfT0ZfV0VFS+OBr+aXpeabnOOBqzEsIOaciOabnOOBqzIsIC4uLiDlnJ/mm5zjgas344KS6L+U44GZ44KT44Gg44Gj44Gm44O8LiAKICAgICAgICAvLyDkv7rjga/jgqvjg6zjg7Pjg4Djg7zjga/mnIjmm5zlp4vjgb7jgorjgYzlpb3jgY3jgaDjgYvjgoksIOaciOabnOOBjDAsIOaXpeabnOOBjDbjgavjgarjgaPjgabjgbvjgZfjgYTjgpPjgaDjgojjga0uCiAgICAgICAgLy8g44Gg44Gj44Gf44KJNeOCkui2s+OBm+OBsOOBhOOBhOOCk+OBmOOCg+OBrT8KICAgICAgICBpbnQgZGF5T2ZXZWVrT24xc3QgPSAoY2FsZW5kYXIuZ2V0KENhbGVuZGFyLkRBWV9PRl9XRUVLKSArIDUpICUgNzsKICAgICAgICBmb3IoaW50IGk9MDsgaTxkYXlPZldlZWtPbjFzdDsgaSsrKSB7CiAgICAgICAgICAgIGRheXNbMF1baV0gPSBuZXcgSkxhYmVsKCk7CiAgICAgICAgICAgIGRheXNbMF1baV0uc2V0SG9yaXpvbnRhbEFsaWdubWVudChKTGFiZWwuQ0VOVEVSKTsKICAgICAgICAgICAgcDIuYWRkKGRheXNbMF1baV0pOwogICAgICAgIH0KICAgICAgICBpbnQgZGF5ID0gMTsKICAgICAgICBmb3IoaW50IGk9ZGF5T2ZXZWVrT24xc3Q7IGk8NzsgaSsrKSB7CiAgICAgICAgICAgIGRheXNbMF1baV0gPSBuZXcgSkxhYmVsKCIiICsgKGRheSsrKSk7CiAgICAgICAgICAgIGRheXNbMF1baV0uc2V0SG9yaXpvbnRhbEFsaWdubWVudChKTGFiZWwuQ0VOVEVSKTsKICAgICAgICAgICAgcDIuYWRkKGRheXNbMF1baV0pOwogICAgICAgIH0KaGVyZTogZm9yKGludCByPTE7IHI8NzsgcisrKSB7CiAgICAgICAgICAgIGRheXNbcl0gPSBuZXcgSkxhYmVsWzddOwogICAgICAgICAgICBmb3IoaW50IGk9MDsgaTw3OyBpKyspIHsKICAgICAgICAgICAgICAgIGlmKGRheSA+IGNhbGVuZGFyLmdldEFjdHVhbE1heGltdW0oQ2FsZW5kYXIuREFURSkpIHsgYnJlYWsgaGVyZTsgfQogICAgICAgICAgICAgICAgZGF5c1tyXVtpXSA9IG5ldyBKTGFiZWwoIiIgKyBkYXkpOwogICAgICAgICAgICAgICAgZGF5c1tyXVtpXS5zZXRIb3Jpem9udGFsQWxpZ25tZW50KEpMYWJlbC5DRU5URVIpOwogICAgICAgICAgICAgICAgZGF5ICs9IDE7CiAgICAgICAgICAgICAgICBwMi5hZGQoZGF5c1tyXVtpXSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgQE92ZXJyaWRlIHB1YmxpYyB2b2lkIHJ1bigpIHsKICAgICAgICBmb3IoIDsgOyApIHsKICAgICAgICAgICAgdHJ5IHsKICAgICAgICAgICAgICAgIENhbGVuZGFyIG5vdyA9IENhbGVuZGFyLmdldEluc3RhbmNlKCk7CiAgICAgICAgICAgICAgICBsYmxUaW1lLnNldFRleHQoZm9ybWF0dGVyLmZvcm1hdChub3cuZ2V0VGltZSgpKSk7CiAgICAgICAgICAgICAgICBUaHJlYWQuc2xlZXAoNTAwKTsKICAgICAgICAgICAgfSBjYXRjaChJbnRlcnJ1cHRlZEV4Y2VwdGlvbiBleCkgewogICAgICAgICAgICAgICAgZXgucHJpbnRTdGFja1RyYWNlKCk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgQE92ZXJyaWRlIHB1YmxpYyB2b2lkIGFjdGlvblBlcmZvcm1lZChBY3Rpb25FdmVudCBlKSB7CiAgICAgICAgT2JqZWN0IHNvdXJjZSA9IGUuZ2V0U291cmNlKCk7CiAgICAgICAgaWYoc291cmNlID09IGJ0bk5leHRNb250aCkgewogICAgICAgICAgICBpZihtb250aCA9PSAxMSkgewogICAgICAgICAgICAgICAgeWVhciArPSAxOwogICAgICAgICAgICAgICAgbW9udGggPSAwOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgbW9udGggKz0gMTsKICAgICAgICAgICAgfQogICAgICAgICAgICBzaG93Q2FsZW5kYXIoKTsKICAgICAgICB9IGVsc2UgaWYoc291cmNlID09IGJ0blByZXZNb250aCkgewogICAgICAgICAgICBpZihtb250aCA9PSAwKSB7CiAgICAgICAgICAgICAgICB5ZWFyIC09IDE7CiAgICAgICAgICAgICAgICBtb250aCA9IDExOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgbW9udGggLT0gMTsKICAgICAgICAgICAgfQogICAgICAgICAgICBzaG93Q2FsZW5kYXIoKTsKICAgICAgICB9CiAgICB9CgogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIE15V2lkZ2V0IHNlbGYgPSBuZXcgTXlXaWRnZXQoKTsKICAgICAgICBuZXcgVGhyZWFkKHNlbGYpLnN0YXJ0KCk7CiAgICAgICAgc2VsZi5zZXRWaXNpYmxlKHRydWUpOwogICAgfQp9