此類是一個swing選擇日期控件。
效果如下圖:
?????????????????????
??1
//
?日期按鈕類
??2
@SuppressWarnings(
"
serial
"
)
??3
public
?
class
?DateChooserJButton?
extends
?JButton?
{
??4
??5
????
private
?DateChooser?dateChooser;
??6
????
??7
????
private
?JTextComponent??com;
??8
????
??9
????
private
?String?preLabel?
=
?
""
;
?10
?11
????
public
?
void
?setJTextComponent(JTextComponent?com)
{
?12
????????
this
.com
=
com;
?13
????}
?14
????
?15
????
public
?DateChooserJButton()?
{
?16
????????
this
(
null
,?getNowDate(),?
null
);
?17
????}
?18
?19
????
public
?DateChooserJButton(String?text)?
{
?20
????????
this
(text,?getNowDate(),?
null
);
?21
????}
?22
?23
????
public
?DateChooserJButton(Icon?icon)?
{
?24
????????
this
(
null
,?getNowDate(),?icon);
?25
????}
?26
?27
????
public
?DateChooserJButton(Date?date)?
{
?28
????????
this
(
null
,?date,?
null
);
?29
????}
?30
?31
????
public
?DateChooserJButton(String?text,?Date?date,?Icon?icon)?
{
?32
????????
if
?(text?
!=
?
null
)
?33
????????????
super
.setText(text);
?34
????????
if
?(icon?
!=
?
null
)
?35
????????????
super
.setIcon(icon);
?36
????????setBorder(
null
);
?37
????????setCursor(
new
?Cursor(Cursor.HAND_CURSOR));
?38
????????
super
.addActionListener(
new
?ActionListener()?
{
?39
????????????
public
?
void
?actionPerformed(ActionEvent?e)?
{
?40
????????????????
if
?(dateChooser?
==
?
null
)
?41
????????????????????dateChooser?
=
?
new
?DateChooser();
?42
????????????????Point?p?
=
?getLocationOnScreen();
?43
????????????????p.y?
=
?p.y?
+
?
30
;
?44
????????????????dateChooser.showDateChooser(p);
?45
????????????}
?46
????????}
);
?47
????}
?48
?49
????
public
?DateChooserJButton(SimpleDateFormat?df,?String?dateString)?
{
?50
????????
this
();
?51
????????setText(df,?dateString);
?52
????}
?53
?54
????
private
?
static
?Date?getNowDate()?
{
?55
????????
return
?Calendar.getInstance().getTime();
?56
????}
?57
?58
????
private
?
static
?SimpleDateFormat?getDefaultDateFormat()?
{
?59
????????
return
?
new
?SimpleDateFormat(
"
yyyy-MM-dd
"
);
?60
????}
?61
?62
????
public
?
void
?setText(SimpleDateFormat?df,?String?s)?
{
?63
????????Date?date;
?64
????????
try
?
{
?65
????????????date?
=
?df.parse(s);
?66
????????}
?
catch
?(ParseException?e)?
{
?67
????????????date?
=
?getNowDate();
?68
????????}
?69
????????setDate(date);
?70
????}
?71
?72
????
public
?
void
?setDate(Date?date)?
{
?73
????????com.setText(getDefaultDateFormat().format(date));
?74
????}
?75
?76
????
public
?Date?getDate()?
{
?77
????????String?dateString?
=
?getText().substring(preLabel.length());
?78
????????
try
?
{
?79
????????????
return
?getDefaultDateFormat().parse(dateString);
?80
????????}
?
catch
?(ParseException?e)?
{
?81
????????????
return
?getNowDate();
?82
????????}
?83
????}
?84
?85
????
//
?覆蓋父類的方法使之無效
?86
????
public
?
void
?addActionListener(ActionListener?listener)?
{
?87
????}
?88
?89
????
private
?
class
?DateChooser?
extends
?JPanel?
implements
?ActionListener,
?90
????????????ChangeListener?
{
?91
????????
int
?startYear?
=
?
1980
;?
//
?默認【最小】顯示年份
?92
?93
????????
int
?lastYear?
=
?
2050
;?
//
?默認【最大】顯示年份
?94
?95
????????
int
?width?
=
?
200
;?
//
?界面寬度
?96
?97
????????
int
?height?
=
?
200
;?
//
?界面高度
?98
?99
????????Color?backGroundColor?
=
?Color.gray;?
//
?底色
100
101
????????
//
?月歷表格配色----------------
//
102
103
????????Color?palletTableColor?
=
?Color.white;?
//
?日歷表底色
104
105
????????Color?todayBackColor?
=
?Color.orange;?
//
?今天背景色
106
107
????????Color?weekFontColor?
=
?Color.blue;?
//
?星期文字色
108
109
????????Color?dateFontColor?
=
?Color.black;?
//
?日期文字色
110
111
????????Color?weekendFontColor?
=
?Color.red;?
//
?周末文字色
112
113
????????
//
?控制條配色------------------
//
114
????????Color?controlLineColor?
=
?Color.pink;?
//
?控制條底色
115
116
????????Color?controlTextColor?
=
?Color.white;?
//
?控制條標簽文字色
117
118
????????Color?rbFontColor?
=
?Color.white;?
//
?RoundBox文字色
119
120
????????Color?rbBorderColor?
=
?Color.red;?
//
?RoundBox邊框色
121
122
????????Color?rbButtonColor?
=
?Color.pink;?
//
?RoundBox按鈕色
123
124
????????Color?rbBtFontColor?
=
?Color.red;?
//
?RoundBox按鈕文字色
125
126
????????JDialog?dialog;
127
128
????????JSpinner?yearSpin;
129
130
????????JSpinner?monthSpin;
131
132
????????JSpinner?hourSpin;
133
134
????????JButton[][]?daysButton?
=
?
new
?JButton[
6
][
7
];
135
136
????????DateChooser()?
{
137
138
????????????setLayout(
new
?BorderLayout());
139
????????????setBorder(
new
?LineBorder(backGroundColor,?
2
));
140
????????????setBackground(backGroundColor);
141
142
????????????JPanel?topYearAndMonth?
=
?createYearAndMonthPanal();
143
????????????add(topYearAndMonth,?BorderLayout.NORTH);
144
????????????JPanel?centerWeekAndDay?
=
?createWeekAndDayPanal();
145
????????????add(centerWeekAndDay,?BorderLayout.CENTER);
146
147
????????}
148
149
????????
private
?JPanel?createYearAndMonthPanal()?
{
150
????????????Calendar?c?
=
?getCalendar();
151
????????????
int
?currentYear?
=
?c.get(Calendar.YEAR);
152
????????????
int
?currentMonth?
=
?c.get(Calendar.MONTH)?
+
?
1
;
153
????????????
int
?currentHour?
=
?c.get(Calendar.HOUR_OF_DAY);
154
155
????????????JPanel?result?
=
?
new
?JPanel();
156
????????????result.setLayout(
new
?FlowLayout());
157
????????????result.setBackground(controlLineColor);
158
159
????????????yearSpin?
=
?
new
?JSpinner(
new
?SpinnerNumberModel(currentYear,
160
????????????????????startYear,?lastYear,?
1
));
161
????????????yearSpin.setPreferredSize(
new
?Dimension(
48
,?
20
));
162
????????????yearSpin.setName(
"
Year
"
);
163
????????????yearSpin.setEditor(
new
?JSpinner.NumberEditor(yearSpin,?
"
####
"
));
164
????????????yearSpin.addChangeListener(
this
);
165
????????????result.add(yearSpin);
166
167
????????????JLabel?yearLabel?
=
?
new
?JLabel(
"
年
"
);
168
????????????yearLabel.setForeground(controlTextColor);
169
????????????result.add(yearLabel);
170
171
????????????monthSpin?
=
?
new
?JSpinner(
new
?SpinnerNumberModel(currentMonth,?
1
,
172
????????????????????
12
,?
1
));
173
????????????monthSpin.setPreferredSize(
new
?Dimension(
35
,?
20
));
174
????????????monthSpin.setName(
"
Month
"
);
175
????????????monthSpin.addChangeListener(
this
);
176
????????????result.add(monthSpin);
177
178
????????????JLabel?monthLabel?
=
?
new
?JLabel(
"
月
"
);
179
????????????monthLabel.setForeground(controlTextColor);
180
????????????result.add(monthLabel);
181
182
????????????hourSpin?
=
?
new
?JSpinner(
new
?SpinnerNumberModel(currentHour,?
0
,?
23
,
183
????????????????????
1
));
184
????????????hourSpin.setPreferredSize(
new
?Dimension(
35
,?
20
));
185
????????????hourSpin.setName(
"
Hour
"
);
186
????????????hourSpin.addChangeListener(
this
);
187
????????????result.add(hourSpin);
188
189
????????????JLabel?hourLabel?
=
?
new
?JLabel(
"
時
"
);
190
????????????hourLabel.setForeground(controlTextColor);
191
????????????result.add(hourLabel);
192
193
????????????
return
?result;
194
????????}
195
196
????????
private
?JPanel?createWeekAndDayPanal()?
{
197
????????????String?colname[]?
=
?
{?
"
日
"
,?
"
一
"
,?
"
二
"
,?
"
三
"
,?
"
四
"
,?
"
五
"
,?
"
六
"
?}
;
198
????????????JPanel?result?
=
?
new
?JPanel();
199
????????????
//
?設置固定字體,以免調用環境改變影響界面美觀
200
????????????result.setFont(
new
?Font(
"
宋體
"
,?Font.PLAIN,?
12
));
201
????????????result.setLayout(
new
?GridLayout(
7
,?
7
));
202
????????????result.setBackground(Color.white);
203
????????????JLabel?cell;
204
205
????????????
for
?(
int
?i?
=
?
0
;?i?
<
?
7
;?i
++
)?
{
206
????????????????cell?
=
?
new
?JLabel(colname[i]);
207
????????????????cell.setHorizontalAlignment(JLabel.RIGHT);
208
????????????????
if
?(i?
==
?
0
?
||
?i?
==
?
6
)
209
????????????????????cell.setForeground(weekendFontColor);
210
????????????????
else
211
????????????????????cell.setForeground(weekFontColor);
212
????????????????result.add(cell);
213
????????????}
214
215
????????????
int
?actionCommandId?
=
?
0
;
216
????????????
for
?(
int
?i?
=
?
0
;?i?
<
?
6
;?i
++
)
217
????????????????
for
?(
int
?j?
=
?
0
;?j?
<
?
7
;?j
++
)?
{
218
????????????????????JButton?numberButton?
=
?
new
?JButton();
219
????????????????????numberButton.setBorder(
null
);
220
????????????????????numberButton.setHorizontalAlignment(SwingConstants.RIGHT);
221
????????????????????numberButton.setActionCommand(String
222
????????????????????????????.valueOf(actionCommandId));
223
????????????????????numberButton.addActionListener(
this
);
224
????????????????????numberButton.setBackground(palletTableColor);
225
????????????????????numberButton.setForeground(dateFontColor);
226
????????????????????
if
?(j?
==
?
0
?
||
?j?
==
?
6
)
227
????????????????????????numberButton.setForeground(weekendFontColor);
228
????????????????????
else
229
????????????????????????numberButton.setForeground(dateFontColor);
230
????????????????????daysButton[i][j]?
=
?numberButton;
231
????????????????????result.add(numberButton);
232
????????????????????actionCommandId
++
;
233
????????????????}
234
235
????????????
return
?result;
236
????????}
237
238
????????
private
?JDialog?createDialog(Frame?owner)?
{
239
????????????JDialog?result?
=
?
new
?JDialog(owner,?
"
日期時間選擇
"
,?
true
);
240
????????????result.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
241
????????????result.getContentPane().add(
this
,?BorderLayout.CENTER);
242
????????????result.pack();
243
????????????result.setSize(width,?height);
244
????????????
return
?result;
245
????????}
246
247
????????@SuppressWarnings(
"
deprecation
"
)
248
????????
void
?showDateChooser(Point?position)?
{
249
????????????Frame?owner?
=
?(Frame)?SwingUtilities
250
????????????????????.getWindowAncestor(DateChooserJButton.
this
);
251
????????????
if
?(dialog?
==
?
null
?
||
?dialog.getOwner()?
!=
?owner)
252
????????????????dialog?
=
?createDialog(owner);
253
????????????dialog.setLocation(getAppropriateLocation(owner,?position));
254
????????????flushWeekAndDay();
255
????????????dialog.show();
256
????????}
257
258
????????Point?getAppropriateLocation(Frame?owner,?Point?position)?
{
259
????????????Point?result?
=
?
new
?Point(position);
260
????????????Point?p?
=
?owner.getLocation();
261
????????????
int
?offsetX?
=
?(position.x?
+
?width)?
-
?(p.x%

??2

??3



??4

??5

??6

??7

??8

??9

?10

?11



?12

?13

?14

?15



?16

?17

?18

?19



?20

?21

?22

?23



?24

?25

?26

?27



?28

?29

?30

?31



?32

?33

?34

?35

?36

?37

?38



?39



?40

?41

?42

?43

?44

?45

?46

?47

?48

?49



?50

?51

?52

?53

?54



?55

?56

?57

?58



?59

?60

?61

?62



?63

?64



?65

?66



?67

?68

?69

?70

?71

?72



?73

?74

?75

?76



?77

?78



?79

?80



?81

?82

?83

?84

?85

?86



?87

?88

?89

?90



?91

?92

?93

?94

?95

?96

?97

?98

?99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136



137

138

139

140

141

142

143

144

145

146

147

148

149



150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196



197



198

199

200

201

202

203

204

205



206

207

208

209

210

211

212

213

214

215

216

217



218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238



239

240

241

242

243

244

245

246

247

248



249

250

251

252

253

254

255

256

257

258



259

260

261
