回答編集履歴

10

setFocusable 削除

2023/02/20 09:22

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -23,7 +23,6 @@
23
23
  setTitle("万年カレンダー3");
24
24
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
25
25
  setBounds(100, 100, 500, 400);
26
- setFocusable(true);
27
26
 
28
27
  curYear = year;
29
28
  curMonth = month;

9

祝日処理仮実装

2023/02/20 09:19

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -1,8 +1,5 @@
1
1
  Calendar クラスを使いたいのか使いたくないのかよく分かりません…元のコードの感じを残すようには気にしてみましたが…。
2
2
  ```java
3
- package teratail_java.q_bs7lg251rknmrx;
4
- //package calPro3.calPack;
5
-
6
3
  import java.awt.Color;
7
4
  import java.awt.GridLayout;
8
5
  import java.awt.event.ActionEvent;
@@ -103,9 +100,9 @@
103
100
  System.out.println("最後の空白は「 " + lastBlank + " 」日分");
104
101
 
105
102
  int i = 0;
106
- while(i < firstBlank) setDay(i++, "+", false);
103
+ while(i < firstBlank) setDay(i++, "+", false, false);
107
- for(int j = 1; j <= dayNum; j++) setDay(i++, ""+j, true);
104
+ for(int date = 1; date <= dayNum; date++) setDay(i++, ""+date, true, isHoliday(year, month, date));
108
- while(i < dayLabels.length) setDay(i++, "-", false);
105
+ while(i < dayLabels.length) setDay(i++, "-", false, false);
109
106
  }
110
107
 
111
108
  private int getDayNum(int year, int month) {
@@ -135,11 +132,22 @@
135
132
  return (year + year / 4 - year / 100 + year / 400 + (13 * month + 8) / 5 + date) % 7;
136
133
  }
137
134
 
138
- private void setDay(int i, String text, boolean isValidDay) {
135
+ private void setDay(int i, String text, boolean isValidDay, boolean isHoliday) {
139
136
  int weekday = isValidDay ? i % 7 : -1;
140
- Color foreground = weekday == 0 ? Color.RED : weekday == 6 ? Color.BLUE : Color.BLACK;
137
+ Color foreground = isHoliday || weekday == 0 ? Color.RED : weekday == 6 ? Color.BLUE : Color.BLACK;
141
138
  dayLabels[i].setForeground(foreground);
142
139
  dayLabels[i].setText(text);
140
+ }
141
+
142
+ /** 祝日
143
+ * @param year 年
144
+ * @param month 月(1~12)
145
+ * @param date 日(1~31)
146
+ * @return 祝日なら true */
147
+ private boolean isHoliday(int year, int month, int date) {
148
+ //TODO: 祝日判定
149
+ if(month == 1 & date == 1) return true; //元日
150
+ return false;
143
151
  }
144
152
  }
145
153
  }

8

red → RED

2023/02/20 08:47

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -137,7 +137,7 @@
137
137
 
138
138
  private void setDay(int i, String text, boolean isValidDay) {
139
139
  int weekday = isValidDay ? i % 7 : -1;
140
- Color foreground = weekday == 0 ? Color.red : weekday == 6 ? Color.BLUE : Color.BLACK;
140
+ Color foreground = weekday == 0 ? Color.RED : weekday == 6 ? Color.BLUE : Color.BLACK;
141
141
  dayLabels[i].setForeground(foreground);
142
142
  dayLabels[i].setText(text);
143
143
  }

7

nextMonth 処理を Action 化、土日色付け

2023/02/20 08:44

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -1,5 +1,9 @@
1
1
  Calendar クラスを使いたいのか使いたくないのかよく分かりません…元のコードの感じを残すようには気にしてみましたが…。
2
2
  ```java
3
+ package teratail_java.q_bs7lg251rknmrx;
4
+ //package calPro3.calPack;
5
+
6
+ import java.awt.Color;
3
7
  import java.awt.GridLayout;
4
8
  import java.awt.event.ActionEvent;
5
9
  import java.awt.event.KeyEvent;
@@ -28,6 +32,16 @@
28
32
  curMonth = month;
29
33
  System.out.println(curYear+" curYear 年 "+curMonth +" curMonth 月");
30
34
 
35
+ Action nextMonthAction = new AbstractAction("翌月↓") {
36
+ @Override
37
+ public void actionPerformed(ActionEvent e) {
38
+ //System.out.println("ボタンプッシュ");
39
+ //System.out.println("下が押されました");
40
+ System.out.println("翌月処理");
41
+ nextMonth();
42
+ }
43
+ };
44
+
31
45
  JPanel contentPane = new JPanel(null);
32
46
  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
33
47
  setContentPane(contentPane);
@@ -40,27 +54,16 @@
40
54
  calendarPanel.setBounds(12, 126, 460, 225);
41
55
  contentPane.add(calendarPanel);
42
56
 
43
- JButton nextMonthButton = new JButton("翌月↓");
57
+ JButton nextMonthButton = new JButton(nextMonthAction);
44
58
  nextMonthButton.setBounds(278, 39, 91, 21);
45
59
  contentPane.add(nextMonthButton);
46
60
 
47
61
  setCalendar(curYear, curMonth);
48
62
 
49
- nextMonthButton.addActionListener(e -> {
50
- System.out.println("ボタンプッシュ");
51
- nextMonth();
52
- });
53
-
54
63
  JRootPane root = getRootPane();
55
64
  KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
56
65
  root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ks, ACTION_NEXTMONTH);
57
- root.getActionMap().put(ACTION_NEXTMONTH, new AbstractAction() {
66
+ root.getActionMap().put(ACTION_NEXTMONTH, nextMonthAction);
58
- @Override
59
- public void actionPerformed(ActionEvent e) {
60
- System.out.println("下が押されました");
61
- nextMonth();
62
- }
63
- });
64
67
  }
65
68
 
66
69
  private void nextMonth() {
@@ -100,9 +103,9 @@
100
103
  System.out.println("最後の空白は「 " + lastBlank + " 」日分");
101
104
 
102
105
  int i = 0;
103
- while(i < firstBlank) dayLabels[i++].setText("+");
106
+ while(i < firstBlank) setDay(i++, "+", false);
104
- for(int j = 1; j <= dayNum; j++) dayLabels[i++].setText("" + j);
107
+ for(int j = 1; j <= dayNum; j++) setDay(i++, ""+j, true);
105
- while(i < dayLabels.length) dayLabels[i++].setText("-");
108
+ while(i < dayLabels.length) setDay(i++, "-", false);
106
109
  }
107
110
 
108
111
  private int getDayNum(int year, int month) {
@@ -118,6 +121,11 @@
118
121
  return 31;
119
122
  }
120
123
 
124
+ /** 曜日
125
+ * @param year 年
126
+ * @param month 月(1~12)
127
+ * @param date 日(1~31)
128
+ * @return 曜日(0=日, 1=月, ..., 6=土) */
121
129
  private int getWeekday(int year, int month, int date) {
122
130
  //ツェラーの公式から
123
131
  if(month == 1 || month == 2) {
@@ -126,6 +134,13 @@
126
134
  }
127
135
  return (year + year / 4 - year / 100 + year / 400 + (13 * month + 8) / 5 + date) % 7;
128
136
  }
137
+
138
+ private void setDay(int i, String text, boolean isValidDay) {
139
+ int weekday = isValidDay ? i % 7 : -1;
140
+ Color foreground = weekday == 0 ? Color.red : weekday == 6 ? Color.BLUE : Color.BLACK;
141
+ dayLabels[i].setForeground(foreground);
142
+ dayLabels[i].setText(text);
143
+ }
129
144
  }
130
145
  }
131
146
  ```

6

"↓" 入力処理を InputMap に変更、他

2023/02/19 18:19

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -1,13 +1,15 @@
1
1
  Calendar クラスを使いたいのか使いたくないのかよく分かりません…元のコードの感じを残すようには気にしてみましたが…。
2
2
  ```java
3
3
  import java.awt.GridLayout;
4
+ import java.awt.event.ActionEvent;
4
5
  import java.awt.event.KeyEvent;
5
- import java.awt.event.KeyListener;
6
6
 
7
7
  import javax.swing.*;
8
8
  import javax.swing.border.EmptyBorder;
9
9
 
10
10
  public class PerpetualCalendar3 extends JFrame {
11
+ private static final String ACTION_NEXTMONTH = "NEXT_MONTH";
12
+
11
13
  public static void main(String[] args) {
12
14
  SwingUtilities.invokeLater(() -> new PerpetualCalendar3(2023, 2).setVisible(true));
13
15
  }
@@ -46,28 +48,27 @@
46
48
 
47
49
  nextMonthButton.addActionListener(e -> {
48
50
  System.out.println("ボタンプッシュ");
49
- if(++curMonth > 12) {
50
- curMonth = 1;
51
+ nextMonth();
51
- curYear ++;
52
- }
53
- setCalendar(curYear, curMonth);
54
52
  });
55
53
 
56
- addKeyListener(new KeyListener() {
54
+ JRootPane root = getRootPane();
55
+ KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
56
+ root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ks, ACTION_NEXTMONTH);
57
+ root.getActionMap().put(ACTION_NEXTMONTH, new AbstractAction() {
57
58
  @Override
58
- public void keyTyped(KeyEvent e) { /*ignore*/ }
59
- @Override
60
- public void keyPressed(KeyEvent e) {
59
+ public void actionPerformed(ActionEvent e) {
61
- switch(e.getKeyCode()) {
62
- case KeyEvent.VK_DOWN:
63
- System.out.println("下が押されました");
60
+ System.out.println("下が押されました");
64
- nextMonthButton.doClick();
61
+ nextMonth();
65
- break;
66
- }
67
62
  }
68
- @Override
69
- public void keyReleased(KeyEvent e) { /*ignore*/ }
70
63
  });
64
+ }
65
+
66
+ private void nextMonth() {
67
+ if(++curMonth > 12) {
68
+ curMonth = 1;
69
+ curYear ++;
70
+ }
71
+ setCalendar(curYear, curMonth);
71
72
  }
72
73
 
73
74
  private void setCalendar(int year, int month) {
@@ -81,7 +82,7 @@
81
82
  CalendarPanel() {
82
83
  super(new GridLayout(6, 7, 0, 0));
83
84
 
84
- dayLabels = new JLabel[42];
85
+ dayLabels = new JLabel[6 * 7];
85
86
  for(int i = 0; i < dayLabels.length; i++) {
86
87
  dayLabels[i] = new JLabel();
87
88
  add(dayLabels[i]);
@@ -99,9 +100,9 @@
99
100
  System.out.println("最後の空白は「 " + lastBlank + " 」日分");
100
101
 
101
102
  int i = 0;
102
- for(int j = 0; j < firstBlank; j++, i++) dayLabels[i].setText("+");
103
+ while(i < firstBlank) dayLabels[i++].setText("+");
103
- for(int j = 1; j <= dayNum; j++, i++) dayLabels[i].setText("" + j);
104
+ for(int j = 1; j <= dayNum; j++) dayLabels[i++].setText("" + j);
104
- for( ; i < dayLabels.length; i++) dayLabels[i].setText("-");
105
+ while(i < dayLabels.length) dayLabels[i++].setText("-");
105
106
  }
106
107
 
107
108
  private int getDayNum(int year, int month) {

5

フォーマット

2023/02/18 07:24

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -71,7 +71,7 @@
71
71
  }
72
72
 
73
73
  private void setCalendar(int year, int month) {
74
- headerLabel.setText(year+ "年" + month +"月");
74
+ headerLabel.setText(year + "年" + month + "月");
75
75
  calendarPanel.set(year, month);
76
76
  }
77
77
 

4

バグっぽいのを修正

2023/02/18 07:23

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -71,7 +71,7 @@
71
71
  }
72
72
 
73
73
  private void setCalendar(int year, int month) {
74
- headerLabel.setText(curYear+ "年" + curMonth +"月");
74
+ headerLabel.setText(year+ "年" + month +"月");
75
75
  calendarPanel.set(year, month);
76
76
  }
77
77
 

3

修正

2023/02/18 07:21

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -1,4 +1,4 @@
1
- Calendar クラスを使いたいのか使いたくないのかよく分かりません…元のコードの感じを残すようにしてみました。
1
+ Calendar クラスを使いたいのか使いたくないのかよく分かりません…元のコードの感じを残すようには気にしてみましたが…
2
2
  ```java
3
3
  import java.awt.GridLayout;
4
4
  import java.awt.event.KeyEvent;

2

コード編集

2023/02/18 07:15

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -38,11 +38,11 @@
38
38
  calendarPanel.setBounds(12, 126, 460, 225);
39
39
  contentPane.add(calendarPanel);
40
40
 
41
- setCalendar(curYear, curMonth);
42
-
43
41
  JButton nextMonthButton = new JButton("翌月↓");
44
42
  nextMonthButton.setBounds(278, 39, 91, 21);
45
43
  contentPane.add(nextMonthButton);
44
+
45
+ setCalendar(curYear, curMonth);
46
46
 
47
47
  nextMonthButton.addActionListener(e -> {
48
48
  System.out.println("ボタンプッシュ");

1

コード修正

2023/02/18 07:10

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -75,7 +75,7 @@
75
75
  calendarPanel.set(year, month);
76
76
  }
77
77
 
78
- private class CalendarPanel extends JPanel {
78
+ private static class CalendarPanel extends JPanel {
79
79
  private JLabel[] dayLabels;
80
80
 
81
81
  CalendarPanel() {
@@ -86,19 +86,6 @@
86
86
  dayLabels[i] = new JLabel();
87
87
  add(dayLabels[i]);
88
88
  }
89
- }
90
-
91
- private int getDayNum(int year, int month) {
92
- switch(month) {
93
- case 2:
94
- return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 29 : 28;
95
- case 4:
96
- case 6:
97
- case 9:
98
- case 11:
99
- return 30;
100
- }
101
- return 31;
102
89
  }
103
90
 
104
91
  void set(int year, int month) {
@@ -117,6 +104,19 @@
117
104
  for( ; i < dayLabels.length; i++) dayLabels[i].setText("-");
118
105
  }
119
106
 
107
+ private int getDayNum(int year, int month) {
108
+ switch(month) {
109
+ case 2:
110
+ return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 29 : 28;
111
+ case 4:
112
+ case 6:
113
+ case 9:
114
+ case 11:
115
+ return 30;
116
+ }
117
+ return 31;
118
+ }
119
+
120
120
  private int getWeekday(int year, int month, int date) {
121
121
  //ツェラーの公式から
122
122
  if(month == 1 || month == 2) {