这个没啥难点的,主要是界面设计吧
界面就是这个样子
运行时请在src同目录下放上我女神的照片
-----------------------------------代码如下-----------------------------------
1 import java.awt.*; 2 import java.awt.event.*; 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Calendar; 6 import java.util.Date; 7 import javax.swing.*; 8 9 10 public class MyCalendar extends JFrame{ 11 /** 12 * 13 */ 14 private static final long serialVersionUID = 1193245796699646333L; 15 private JPanel imagePanel,datePanel,p,p2,p3; 16 private JLabel label,l,l2,l3,l4,l5; 17 private ImageIcon background; 18 private JButton b,b2; 19 private JTextField t; 20 private JComboBox<String> month_list = new JComboBox<String>(); 21 private Font f = new Font("微软雅黑",Font.PLAIN,15); 22 private String columns[] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"}; 23 private Date now = new Date(); 24 private JLabel[] day = new JLabel[42]; 25 public MyCalendar(){ 26 super("Calendar By XJX"); 27 Image img=Toolkit.getDefaultToolkit().getImage("title.png");//窗口图标 28 setIconImage(img); 29 setSize(620,490); 30 setResizable(false); 31 setLayout(null); 32 setLocationRelativeTo(null); 33 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 34 35 //添加背景图片 36 background = new ImageIcon("background.jpg"); 37 label = new JLabel(background); 38 label.setBounds(0, 0, this.getWidth(), this.getHeight()); 39 imagePanel = (JPanel) this.getContentPane(); 40 imagePanel.setOpaque(false); 41 this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE)); 42 43 //设置布局 44 l = new JLabel(); 45 l.setFont(new Font("微软雅黑",Font.ITALIC,40)); 46 l.setForeground(Color.red);//设置字体颜色 47 add(l); 48 l.setBounds(5, 5, 80, 45); 49 l2 = new JLabel(); 50 l2.setFont(new Font("微软雅黑",Font.BOLD,20)); 51 l2.setForeground(Color.red);//设置字体颜色 52 add(l2); 53 l2.setBounds(90, 10, 150, 40); 54 l3 = new JLabel(); 55 l3.setFont(new Font("华文彩云",Font.BOLD,30)); 56 l3.setForeground(Color.red);//设置字体颜色 57 add(l3); 58 l3.setBounds(500, 17, 100, 30); 59 60 p = new JPanel(); 61 add(p); 62 p.setBounds(5, 45, 600, 2); 63 p.setBorder(BorderFactory.createLineBorder(Color.black)); 64 65 l4 = new JLabel("年份:"); 66 add(l4); 67 l4.setBounds(10, 50, 80, 20); 68 l4.setFont(f); 69 t = new JTextField(); 70 add(t); 71 t.setBounds(60, 50, 60, 20); 72 t.setFont(f); 73 l5 = new JLabel("月份:"); 74 add(l5); 75 l5.setBounds(130, 50, 80, 20); 76 l5.setFont(f); 77 add(month_list); 78 month_list.setBounds(180, 50, 70, 20); 79 month_list.setFont(f); 80 month_list.setOpaque(false); 81 82 for(int i = 0;i < 12;i++) 83 { 84 month_list.addItem(columns[i]); 85 } 86 b = new JButton("上个月"); 87 add(b); 88 b.setBounds(430, 50, 80, 20); 89 b.setFont(f); 90 b.setOpaque(false); 91 b2 = new JButton("下个月"); 92 add(b2); 93 b2.setBounds(520, 50, 80, 20); 94 b2.setFont(f); 95 b2.setOpaque(false); 96 97 p2 = new JPanel(); 98 add(p2); 99 p2.setBounds(15, 85, 580, 1); 100 p2.setBorder(BorderFactory.createLineBorder(Color.blue)); 101 102 p3 = new JPanel(); 103 p3.setOpaque(false); 104 add(p3); 105 p3.setBounds(5, 90, 600, 20); 106 p3.setLayout(new GridLayout(1,7,2,2)); 107 JLabel[] week = new JLabel[7]; 108 week[0] = new JLabel(" SUN"); 109 week[1] = new JLabel(" MON"); 110 week[2] = new JLabel(" TUE"); 111 week[3] = new JLabel(" WED"); 112 week[4] = new JLabel(" THU"); 113 week[5] = new JLabel(" FRI"); 114 week[6] = new JLabel(" SAT"); 115 for(int i = 0;i < 7;i++) 116 { 117 week[i].setForeground(Color.black);//设置字体颜色 118 week[i].setFont(f); 119 p3.add(week[i]); 120 } 121 122 123 datePanel = new JPanel(); 124 datePanel.setOpaque(false); 125 add(datePanel); 126 datePanel.setBounds(5, 110, 610, 340); 127 datePanel.setLayout(new GridLayout(6,7,2,5)); 128 129 for(int i = 0;i < 42;i++) 130 { 131 day[i] = new JLabel(""); 132 day[i].setForeground(Color.red);//设置字体颜色 133 //day[i].setBorder(BorderFactory.createLineBorder(Color.black)); 134 day[i].setFont(new Font("Jokerman",Font.BOLD,20)); 135 datePanel.add(day[i]); 136 } 137 138 Calendar cale = null; 139 cale = Calendar.getInstance(); 140 cale.add(Calendar.MONTH, 0); 141 cale.set(Calendar.DAY_OF_MONTH, 1); 142 int dow = cale.get(Calendar.DAY_OF_WEEK);//获取本月第一天是星期几 143 144 cale = Calendar.getInstance(); 145 cale.add(Calendar.MONTH, 1); 146 cale.set(Calendar.DAY_OF_MONTH, 0); 147 int maxDate = cale.get(Calendar.DAY_OF_MONTH);//获取本月有多少天 148 int ptr = 1; 149 int ptr2 = dow -1; 150 int today = now.getDate(); 151 for(int i = 0;i < maxDate;i++) 152 { 153 day[ptr2].setText(" " + ptr); 154 if(ptr == today) 155 { 156 day[ptr2].setForeground(Color.black); 157 day[ptr2].setFont(new Font("Jokerman",Font.BOLD,35)); 158 day[ptr2].setText(" " + ptr); 159 day[ptr2].setBorder(BorderFactory.createLineBorder(new Color(220,20,60))); 160 } 161 ptr++; 162 ptr2++; 163 } 164 165 //初始化顶部信息 166 int m = now.getMonth() + 1; 167 l.setText("" + m); 168 //设置下拉列表的选中月份为当前月份 169 month_list.setSelectedIndex( m-1); 170 171 switch (m) { 172 case 1: 173 l2.setText("January"); 174 break; 175 case 2: 176 l2.setText("February"); 177 break; 178 case 3: 179 l2.setText("March"); 180 break; 181 case 4: 182 l2.setText("April"); 183 break; 184 case 5: 185 l2.setText("May"); 186 break; 187 case 6: 188 l2.setText("June"); 189 break; 190 case 7: 191 l2.setText("July"); 192 break; 193 case 8: 194 l2.setText("August"); 195 break; 196 case 9: 197 l2.setText("September"); 198 break; 199 case 10: 200 l2.setText("October"); 201 break; 202 case 11: 203 l2.setText("November"); 204 break; 205 case 12: 206 l2.setText("December"); 207 break; 208 } 209 int y = now.getYear() + 1900; 210 l3.setText("" + y); 211 t.setText("" + y); 212 213 //添加监听器 214 month_list.addItemListener(new ItemListener(){ 215 public void itemStateChanged(ItemEvent e){ 216 Calendar cale = null; 217 String year; 218 boolean flag = true; 219 if(e.getStateChange() == ItemEvent.SELECTED) 220 { 221 year = t.getText(); 222 if(year.equals("")) 223 { 224 JOptionPane.showMessageDialog(null,"年份不能为空!"); 225 } 226 else 227 { 228 for(int i = 0;i < year.length();i++) 229 { 230 if(year.charAt(i) >= 48 && year.charAt(i) <= 57){} 231 else 232 { 233 JOptionPane.showMessageDialog(null,"请按照正确格式输入年份!"); 234 flag = false; 235 break; 236 } 237 } 238 239 if(flag) 240 { 241 String date = ""; 242 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 243 int y = Integer.valueOf(year).intValue(); 244 int m = month_list.getSelectedIndex() + 1; 245 System.out.println(y); 246 System.out.println(m); 247 cale = Calendar.getInstance(); 248 date = "" + year + "-" + m + "-" + 1; 249 try { 250 cale.setTime(format.parse(date)); 251 } catch (ParseException e1) { 252 // TODO 自动生成的 catch 块 253 e1.printStackTrace(); 254 } 255 int dow = cale.get(Calendar.DAY_OF_WEEK);//获取本月第一天是星期几 256 System.out.println(dow); 257 258 cale.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天 259 int maxDate = cale.get(Calendar.DAY_OF_MONTH);//获取本月有多少天 260 System.out.println(maxDate); 261 int ptr = 1; 262 int ptr2 = dow -1; 263 int today = now.getDate(); 264 //再重新设置内容前重置所有内容 265 for(int i = 0;i < 42;i++) 266 { 267 day[i].setText(""); 268 day[i].setForeground(Color.red);//设置字体颜色 269 day[i].setFont(new Font("Jokerman",Font.BOLD,20)); 270 day[i].setBorder(null); 271 } 272 273 for(int i = 0;i < maxDate;i++) 274 { 275 day[ptr2].setText(" " + ptr); 276 if(ptr == today) 277 { 278 if(y == now.getYear() + 1900 && m == now.getMonth() +1) 279 { 280 day[ptr2].setForeground(Color.black); 281 day[ptr2].setFont(new Font("Jokerman",Font.BOLD,35)); 282 day[ptr2].setText(" " + ptr); 283 day[ptr2].setBorder(BorderFactory.createLineBorder(new Color(220,20,60))); 284 } 285 } 286 ptr++; 287 ptr2++; 288 } 289 290 //重置l,l2,l3的信息 291 l.setText("" + m); 292 switch (m) { 293 case 1: 294 l2.setText("January"); 295 break; 296 case 2: 297 l2.setText("February"); 298 break; 299 case 3: 300 l2.setText("March"); 301 break; 302 case 4: 303 l2.setText("April"); 304 break; 305 case 5: 306 l2.setText("May"); 307 break; 308 case 6: 309 l2.setText("June"); 310 break; 311 case 7: 312 l2.setText("July"); 313 break; 314 case 8: 315 l2.setText("August"); 316 break; 317 case 9: 318 l2.setText("September"); 319 break; 320 case 10: 321 l2.setText("October"); 322 break; 323 case 11: 324 l2.setText("November"); 325 break; 326 case 12: 327 l2.setText("December"); 328 break; 329 } 330 l3.setText("" + y); 331 } 332 } 333 } 334 } 335 }); 336 337 //上个月 338 b.addActionListener(new ActionListener(){ 339 public void actionPerformed(ActionEvent e){ 340 Calendar cale = null; 341 String year; 342 boolean flag = true; 343 344 year = t.getText(); 345 if(year.equals("")) 346 { 347 JOptionPane.showMessageDialog(null,"年份不能为空!"); 348 } 349 else 350 { 351 for(int i = 0;i < year.length();i++) 352 { 353 if(year.charAt(i) >= 48 && year.charAt(i) <= 57){} 354 else 355 { 356 JOptionPane.showMessageDialog(null,"请按照正确格式输入年份!"); 357 flag = false; 358 break; 359 } 360 } 361 362 if(flag) 363 { 364 String date = ""; 365 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 366 int y = Integer.valueOf(year).intValue(); 367 int m = month_list.getSelectedIndex(); 368 if(m==0) 369 { 370 m = 12;//1月往前就是去年12月 371 y -= 1;//去年 372 } 373 System.out.println(y); 374 System.out.println(m); 375 cale = Calendar.getInstance(); 376 date = "" + year + "-" + m + "-" + 1; 377 try { 378 cale.setTime(format.parse(date)); 379 } catch (ParseException e1) { 380 // TODO 自动生成的 catch 块 381 e1.printStackTrace(); 382 } 383 int dow = cale.get(Calendar.DAY_OF_WEEK);//获取本月第一天是星期几 384 System.out.println(dow); 385 386 cale.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天 387 int maxDate = cale.get(Calendar.DAY_OF_MONTH);//获取本月有多少天 388 System.out.println(maxDate); 389 int ptr = 1; 390 int ptr2 = dow -1; 391 int today = now.getDate(); 392 //再重新设置内容前重置所有内容 393 for(int i = 0;i < 42;i++) 394 { 395 day[i].setText(""); 396 day[i].setForeground(Color.red);//设置字体颜色 397 day[i].setFont(new Font("Jokerman",Font.BOLD,20)); 398 day[i].setBorder(null); 399 } 400 401 for(int i = 0;i < maxDate;i++) 402 { 403 day[ptr2].setText(" " + ptr); 404 if(ptr == today) 405 { 406 if(y == now.getYear() + 1900 && m == now.getMonth() +1) 407 { 408 day[ptr2].setForeground(Color.black); 409 day[ptr2].setFont(new Font("Jokerman",Font.BOLD,35)); 410 day[ptr2].setText(" " + ptr); 411 day[ptr2].setBorder(BorderFactory.createLineBorder(new Color(220,20,60))); 412 } 413 } 414 ptr++; 415 ptr2++; 416 } 417 418 //重置l,l2,l3的信息 419 t.setText("" + y); 420 l.setText("" + m); 421 //设置下拉列表的选中月份为当前月份 422 month_list.setSelectedIndex( m-1 ); 423 switch (m) { 424 case 1: 425 l2.setText("January"); 426 break; 427 case 2: 428 l2.setText("February"); 429 break; 430 case 3: 431 l2.setText("March"); 432 break; 433 case 4: 434 l2.setText("April"); 435 break; 436 case 5: 437 l2.setText("May"); 438 break; 439 case 6: 440 l2.setText("June"); 441 break; 442 case 7: 443 l2.setText("July"); 444 break; 445 case 8: 446 l2.setText("August"); 447 break; 448 case 9: 449 l2.setText("September"); 450 break; 451 case 10: 452 l2.setText("October"); 453 break; 454 case 11: 455 l2.setText("November"); 456 break; 457 case 12: 458 l2.setText("December"); 459 break; 460 } 461 l3.setText("" + y); 462 } 463 } 464 } 465 }); 466 467 //下个月 468 b2.addActionListener(new ActionListener(){ 469 public void actionPerformed(ActionEvent e){ 470 Calendar cale = null; 471 String year; 472 boolean flag = true; 473 474 year = t.getText(); 475 if(year.equals("")) 476 { 477 JOptionPane.showMessageDialog(null,"年份不能为空!"); 478 } 479 else 480 { 481 for(int i = 0;i < year.
请发表评论