fork download
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.text.*;
  4. import java.util.*;
  5. import javax.swing.*;
  6.  
  7. public class MyWidget extends JFrame implements ActionListener, Runnable {
  8. String[] w = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", };
  9. String[] m = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", };
  10. JPanel p2;
  11. JLabel lblMonthYear, lblTime;
  12. JButton btnPrevMonth, btnNextMonth;
  13. int year, month;
  14. Calendar calendar;
  15. DateFormat formatter;
  16.  
  17. public MyWidget() {
  18. this.setResizable(false);
  19. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  20. this.setLayout(new BorderLayout());
  21. JPanel p1 = new JPanel(new BorderLayout());
  22. this.p2 = new JPanel(new GridLayout(0, 7));
  23. this.calendar = Calendar.getInstance();
  24. this.year = calendar.get(Calendar.YEAR);
  25. this.month = calendar.get(Calendar.MONTH);
  26. this.btnPrevMonth = new JButton("<");
  27. this.btnPrevMonth.addActionListener(this);
  28. this.lblMonthYear = new JLabel();
  29. this.lblMonthYear.setHorizontalAlignment(JLabel.CENTER);
  30. this.btnNextMonth = new JButton(">");
  31. this.btnNextMonth.addActionListener(this);
  32. p1.add(btnPrevMonth, BorderLayout.WEST);
  33. p1.add(lblMonthYear, BorderLayout.CENTER);
  34. p1.add(btnNextMonth, BorderLayout.EAST);
  35. this.lblTime = new JLabel();
  36. this.formatter = new SimpleDateFormat("HH:mm:ss");
  37. JPanel pnlHeader = new JPanel(new BorderLayout());
  38. JPanel pnlCenter = new JPanel(new BorderLayout());
  39. JPanel pnlFooter = new JPanel();
  40. pnlHeader.add(p1, BorderLayout.NORTH);
  41. pnlCenter.add(p2, BorderLayout.CENTER);
  42. pnlFooter.add(lblTime);
  43. this.add(pnlHeader, BorderLayout.NORTH);
  44. this.add(pnlCenter, BorderLayout.CENTER);
  45. this.add(pnlFooter, BorderLayout.SOUTH);
  46. showCalendar();
  47. this.pack();
  48. }
  49.  
  50. private void showCalendar() {
  51. p2.removeAll();
  52. p2.repaint();
  53. for(int i=0; i<7; i++) {
  54. JLabel l = new JLabel(w[i]);
  55. l.setHorizontalAlignment(JLabel.CENTER);
  56. p2.add(l);
  57. }
  58. this.lblMonthYear.setText(m[month] + " " + year);
  59. // 縦がどっちで横がどっちか考えるの('A`)マンドクセ. どっちも7でいーや.
  60. JLabel[][] days = new JLabel[7][7];
  61. calendar.set(Calendar.YEAR, year);
  62. calendar.set(Calendar.MONTH, month);
  63. calendar.set(Calendar.DATE, 1);
  64. // DAY_OF_WEEKは日曜に1, 月曜に2, ... 土曜に7を返すんだってー.
  65. // 俺はカレンダーは月曜始まりが好きだから, 月曜が0, 日曜が6になってほしいんだよね.
  66. // だったら5を足せばいいんじゃね?
  67. int dayOfWeekOn1st = (calendar.get(Calendar.DAY_OF_WEEK) + 5) % 7;
  68. for(int i=0; i<dayOfWeekOn1st; i++) {
  69. days[0][i] = new JLabel();
  70. days[0][i].setHorizontalAlignment(JLabel.CENTER);
  71. p2.add(days[0][i]);
  72. }
  73. int day = 1;
  74. for(int i=dayOfWeekOn1st; i<7; i++) {
  75. days[0][i] = new JLabel("" + (day++));
  76. days[0][i].setHorizontalAlignment(JLabel.CENTER);
  77. p2.add(days[0][i]);
  78. }
  79. here: for(int r=1; r<7; r++) {
  80. days[r] = new JLabel[7];
  81. for(int i=0; i<7; i++) {
  82. if(day > calendar.getActualMaximum(Calendar.DATE)) { break here; }
  83. days[r][i] = new JLabel("" + day);
  84. days[r][i].setHorizontalAlignment(JLabel.CENTER);
  85. day += 1;
  86. p2.add(days[r][i]);
  87. }
  88. }
  89. }
  90.  
  91. @Override public void run() {
  92. for( ; ; ) {
  93. try {
  94. Calendar now = Calendar.getInstance();
  95. lblTime.setText(formatter.format(now.getTime()));
  96. Thread.sleep(500);
  97. } catch(InterruptedException ex) {
  98. ex.printStackTrace();
  99. }
  100. }
  101. }
  102.  
  103. @Override public void actionPerformed(ActionEvent e) {
  104. Object source = e.getSource();
  105. if(source == btnNextMonth) {
  106. if(month == 11) {
  107. year += 1;
  108. month = 0;
  109. } else {
  110. month += 1;
  111. }
  112. showCalendar();
  113. } else if(source == btnPrevMonth) {
  114. if(month == 0) {
  115. year -= 1;
  116. month = 11;
  117. } else {
  118. month -= 1;
  119. }
  120. showCalendar();
  121. }
  122. }
  123.  
  124. public static void main(String[] args) {
  125. MyWidget self = new MyWidget();
  126. new Thread(self).start();
  127. self.setVisible(true);
  128. }
  129. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty