当前位置:   article > 正文

java写日历(源码)_2011年日历java编写

2011年日历java编写

用到java图形交互的基础知识,可以练练手。 

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.text.DateFormatSymbols;
  4. import java.util.*;
  5. import javax.swing.*;
  6. public class WindowCalendar extends JFrame implements ActionListener {
  7. private JButton previousButton, nextButton;
  8. private JComboBox monthComboBox, yearComboBox;
  9. private JLabel monthLabel, yearLabel;
  10. private JPanel calendarPanel;
  11. public WindowCalendar() {
  12. setTitle("日历");
  13. // 创建月份下拉框
  14. String[] monthStrings = new DateFormatSymbols().getMonths();
  15. monthComboBox = new JComboBox(monthStrings);
  16. monthComboBox.setSelectedIndex(Calendar.getInstance().get(Calendar.MONTH));
  17. monthComboBox.addActionListener(this);
  18. // 创建年份下拉框
  19. Integer[] yearIntegers = new Integer[200];
  20. int currentYear = Calendar.getInstance().get(Calendar.YEAR);
  21. //System.out.println(currentYear);
  22. for (int i = 0; i < yearIntegers.length; i++) {
  23. yearIntegers[i] = new Integer(currentYear - 122 + i);
  24. // System.out.println(yearIntegers[i]);
  25. }
  26. yearComboBox = new JComboBox(yearIntegers);
  27. yearComboBox.setSelectedItem(new Integer(currentYear));
  28. yearComboBox.addActionListener(this);
  29. // 创建月份和年份标签
  30. monthLabel = new JLabel("Month:");
  31. yearLabel = new JLabel("Year:");
  32. // 创建上一个和下一个按钮
  33. previousButton = new JButton("<");
  34. previousButton.addActionListener(this);
  35. nextButton = new JButton(">");
  36. nextButton.addActionListener(this);
  37. // 创建日历面板
  38. calendarPanel = new JPanel(new GridLayout(6, 7));
  39. calendarPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  40. updateCalendar();
  41. // 将组件添加到容器中
  42. JPanel topPanel = new JPanel();
  43. topPanel.add(monthLabel);
  44. topPanel.add(monthComboBox);
  45. topPanel.add(yearLabel);
  46. topPanel.add(yearComboBox);
  47. topPanel.add(previousButton);
  48. topPanel.add(nextButton);
  49. add(topPanel, BorderLayout.NORTH);
  50. add(calendarPanel, BorderLayout.CENTER);
  51. // 设置窗口属性
  52. pack();
  53. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54. setLocationRelativeTo(null);
  55. setVisible(true);
  56. }
  57. public void actionPerformed(ActionEvent event) {
  58. Object source = event.getSource();
  59. if (source == previousButton) {
  60. moveBackwards();
  61. updateCalendar();
  62. }
  63. else if (source == nextButton) {
  64. moveForwards();
  65. updateCalendar();
  66. }
  67. else if (source == monthComboBox || source == yearComboBox) {
  68. updateCalendar();
  69. }
  70. }
  71. private void moveBackwards() {
  72. Calendar calendar = Calendar.getInstance();
  73. calendar.set(yearComboBox.getSelectedIndex() + 1900, monthComboBox.getSelectedIndex(), 1);
  74. calendar.add(Calendar.MONTH, -1);
  75. monthComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
  76. yearComboBox.setSelectedItem(new Integer(calendar.get(Calendar.YEAR)));
  77. }
  78. private void moveForwards() {
  79. Calendar calendar = Calendar.getInstance();
  80. calendar.set(yearComboBox.getSelectedIndex() + 1900, monthComboBox.getSelectedIndex(), 1);
  81. calendar.add(Calendar.MONTH, 1);
  82. monthComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
  83. yearComboBox.setSelectedItem(new Integer(calendar.get(Calendar.YEAR)));
  84. }
  85. private void updateCalendar() {
  86. int year = ((Integer)yearComboBox.getSelectedItem()).intValue();
  87. int month = monthComboBox.getSelectedIndex();
  88. Calendar calendar = Calendar.getInstance();
  89. calendar.set(year, month, 1);
  90. // 清除日历面板上的所有组件
  91. calendarPanel.removeAll();
  92. // 添加星期标签
  93. String[] weekdayStrings = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
  94. for (int i = 0; i < weekdayStrings.length; i++) {
  95. JLabel label = new JLabel(weekdayStrings[i], JLabel.CENTER);
  96. calendarPanel.add(label);
  97. }
  98. // 添加日期按钮
  99. int weekday = calendar.get(Calendar.DAY_OF_WEEK);
  100. int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  101. for (int i = 1; i < weekday; i++) {
  102. JLabel label = new JLabel();
  103. calendarPanel.add(label);
  104. }
  105. for (int i = 1; i <= maxDays; i++) {
  106. JButton button = new JButton(String.valueOf(i));
  107. button.addActionListener(new ButtonListener());
  108. calendarPanel.add(button);
  109. }
  110. // 重新绘制日历
  111. revalidate();
  112. repaint();
  113. }
  114. private class ButtonListener implements ActionListener {
  115. public void actionPerformed(ActionEvent event) {
  116. JOptionPane.showMessageDialog(WindowCalendar.this, "You clicked on day " + ((JButton)event.getSource()).getText());
  117. }
  118. }
  119. public static void main(String[] args) {
  120. new WindowCalendar();
  121. }
  122. }

效果图:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/597866
推荐阅读
相关标签
  

闽ICP备14008679号