perpetual calendar

From , 4 Years ago, written in Java, viewed 53 times.
URL https://pastebin.vip/view/b24d516b
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.GridLayout;
  5. import java.awt.SystemColor;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.MouseEvent;
  9. import java.util.Calendar;
  10. import java.util.GregorianCalendar;
  11. import java.util.Locale;
  12. import java.util.Date;
  13. import java.util.StringTokenizer;
  14.  
  15. import javax.swing.BorderFactory;
  16. import javax.swing.JButton;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JPanel;
  20. import javax.swing.JTextField;
  21. import javax.swing.JToggleButton;
  22. import javax.swing.SwingConstants;
  23. import javax.swing.UIManager;
  24.  
  25. /**
  26. * <p>Title: Swing日历</p>
  27. * <p>Description: 操作日期</p>
  28. * @author duxu2004
  29. * @version 1.0.1
  30. */
  31.  
  32. class JCalendar extends JPanel{
  33. //动态表示年月日
  34. private int year=0;
  35. private int month=0;
  36. private int day=0;
  37. //主面板
  38. private JPanel Main = new JPanel();
  39. //日面板
  40. private JPanel jPanelDay = new JPanel();
  41. //月面板
  42. private JPanel jPanelMonth = new JPanel();
  43. //年的输入位置
  44. private JTextField Year = new JTextField();
  45. //月的输入位置
  46. private JTextField Month = new JTextField();
  47. //减少月份
  48. private JButton MonthDown = new JButton();
  49. //增加月份
  50. private JButton MonthUp = new JButton();
  51.  
  52. private JPanel jPanelButton = new JPanel();
  53. //减少年份
  54. private JButton YearDown = new JButton();
  55. //增加年份
  56. private JButton YearUp = new JButton();
  57. //显示日期的位置
  58. private JLabel Out = new JLabel();
  59. //中国时区,以后可以从这里扩展可以设置时区的功能
  60. private Locale l=Locale.CHINESE;
  61. //主日历
  62. //星期面板
  63. private JPanel weekPanel=new JPanel();
  64. //天按钮组
  65. private JToggleButton[] days=new JToggleButton[42];
  66. //天面板
  67. private JPanel Days = new JPanel();
  68. //标示
  69. private JLabel jLabel1 = new JLabel();
  70. private JLabel jLabel2 = new JLabel();
  71. private JLabel jLabel3 = new JLabel();
  72. private JLabel jLabel4 = new JLabel();
  73. private JLabel jLabel5 = new JLabel();
  74. private JLabel jLabel6 = new JLabel();
  75. private JLabel jLabel7 = new JLabel();
  76. //当前选择的天数按钮
  77. private JToggleButton cur=null;
  78. //月份天数数组,用来取得当月有多少天
  79. // 1 2 3 4 5 6 7 8 9 10 11 12
  80. private int[] mm={31,28,31,30,31,30,31,31,30,31,30,31};
  81.  
  82. //空日期构造函数
  83. public JCalendar() {
  84. try {
  85. jbInit();
  86. }
  87. catch(Exception e) {
  88. e.printStackTrace();
  89. }
  90.  
  91. }
  92. //带日期设置的构造函数
  93. public JCalendar(int year, int month, int day) {
  94. cal.set(year, month, day);
  95. try {
  96. jbInit();
  97. }
  98. catch (Exception e) {
  99. e.printStackTrace();
  100. }
  101. }
  102. //带日历输入的构造函数
  103. public JCalendar(GregorianCalendar calendar) {
  104. cal=calendar;
  105. try {
  106. jbInit();
  107. }
  108. catch (Exception e) {
  109. e.printStackTrace();
  110. }
  111. }
  112. //带日期输入的构造函数
  113. public JCalendar(Date date) {
  114. cal.setTime(date);
  115. try {
  116. jbInit();
  117. }
  118. catch (Exception e) {
  119. e.printStackTrace();
  120. }
  121. }
  122. //初始化组件
  123. private void jbInit() throws Exception {
  124. //初始化年、月、日
  125. iniCalender();
  126.  
  127. this.setLayout(new BorderLayout());
  128. this.setBorder(BorderFactory.createRaisedBevelBorder());
  129. this.setMaximumSize(new Dimension(200, 200));
  130. this.setMinimumSize(new Dimension(200, 200));
  131. this.setPreferredSize(new Dimension(200, 200));
  132.  
  133. Main.setLayout(new BorderLayout());
  134. Main.setBackground(SystemColor.info);
  135. Main.setBorder(null);
  136.  
  137. Out.setBackground(Color.lightGray);
  138. Out.setHorizontalAlignment(SwingConstants.CENTER);
  139. Out.setMaximumSize(new Dimension(100, 19));
  140. Out.setMinimumSize(new Dimension(100, 19));
  141. Out.setPreferredSize(new Dimension(100, 19));
  142.  
  143. jLabel1.setForeground(Color.red);
  144. jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
  145. jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
  146. jLabel1.setText("日");
  147. jLabel2.setForeground(Color.blue);
  148. jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
  149. jLabel2.setHorizontalTextPosition(SwingConstants.CENTER);
  150. jLabel2.setText("六");
  151. jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
  152. jLabel3.setHorizontalTextPosition(SwingConstants.CENTER);
  153. jLabel3.setText("五");
  154. jLabel4.setHorizontalAlignment(SwingConstants.CENTER);
  155. jLabel4.setHorizontalTextPosition(SwingConstants.CENTER);
  156. jLabel4.setText("四");
  157. jLabel5.setHorizontalAlignment(SwingConstants.CENTER);
  158. jLabel5.setHorizontalTextPosition(SwingConstants.CENTER);
  159. jLabel5.setText("三");
  160. jLabel6.setBorder(null);
  161. jLabel6.setHorizontalAlignment(SwingConstants.CENTER);
  162. jLabel6.setHorizontalTextPosition(SwingConstants.CENTER);
  163. jLabel6.setText("二");
  164. jLabel7.setBackground(Color.lightGray);
  165. jLabel7.setForeground(Color.black);
  166. jLabel7.setBorder(null);
  167. jLabel7.setHorizontalAlignment(SwingConstants.CENTER);
  168. jLabel7.setHorizontalTextPosition(SwingConstants.CENTER);
  169. jLabel7.setText("一");
  170.  
  171. weekPanel.setBackground(UIManager.getColor("InternalFrame.activeTitleGradient"));
  172. weekPanel.setBorder(BorderFactory.createEtchedBorder());
  173. weekPanel.setLayout(new GridLayout(1,7));
  174. weekPanel.add(jLabel1, null);
  175. weekPanel.add(jLabel7, null);
  176. weekPanel.add(jLabel6, null);
  177. weekPanel.add(jLabel5, null);
  178. weekPanel.add(jLabel4, null);
  179. weekPanel.add(jLabel3, null);
  180. weekPanel.add(jLabel2, null);
  181.  
  182. MonthUp.setAlignmentX((float) 0.0);
  183. MonthUp.setActionMap(null);
  184.  
  185. jPanelMonth.setBackground(SystemColor.info);
  186. jPanelMonth.setLayout(new BorderLayout());
  187. jPanelMonth.setBorder(BorderFactory.createEtchedBorder());
  188.  
  189. Month.setBorder(null);
  190. Month.setHorizontalAlignment(SwingConstants.CENTER);
  191. Month.addMouseListener(new java.awt.event.MouseAdapter() {
  192. public void mouseClicked(MouseEvent e) {
  193. Month_mouseClicked(e);
  194. }
  195. });
  196. Month.addKeyListener(new java.awt.event.KeyAdapter() {
  197. public void keyPressed(KeyEvent e) {
  198. Month_keyPressed(e);
  199. }
  200. });
  201.  
  202. MonthDown.setBorder(null);
  203. MonthDown.setText("\u25C4");
  204. MonthDown.addActionListener(new java.awt.event.ActionListener() {
  205. public void actionPerformed(ActionEvent e) {
  206. MonthDown_actionPerformed(e);
  207. }
  208. });
  209. MonthUp.setBorder(null);
  210. MonthUp.setText("\u25BA");
  211. MonthUp.addActionListener(new java.awt.event.ActionListener() {
  212. public void actionPerformed(ActionEvent e) {
  213. MonthUp_actionPerformed(e);
  214. }
  215. });
  216.  
  217. jPanelButton.setLayout(null);
  218. jPanelButton.setBorder(null);
  219. jPanelButton.addComponentListener(new java.awt.event.ComponentAdapter() {
  220. public void componentResized(java.awt.event.ComponentEvent evt) {
  221. jPanelButtonComponentResized(evt);
  222. }
  223. });
  224.  
  225. Year.setBorder(BorderFactory.createEtchedBorder());
  226. Year.setMaximumSize(new Dimension(80, 25));
  227. Year.setMinimumSize(new Dimension(80, 25));
  228. Year.setPreferredSize(new Dimension(80, 25));
  229. Year.setHorizontalAlignment(SwingConstants.CENTER);
  230. Year.addMouseListener(new java.awt.event.MouseAdapter() {
  231. public void mouseClicked(MouseEvent e) {
  232. Year_mouseClicked(e);
  233. }
  234. });
  235. Year.addKeyListener(new java.awt.event.KeyAdapter() {
  236. public void keyPressed(KeyEvent e) {
  237. Year_keyPressed(e);
  238. }
  239. });
  240.  
  241. YearDown.setBorder(null);
  242. YearDown.setMaximumSize(new Dimension(16, 16));
  243. YearDown.setMinimumSize(new Dimension(16, 16));
  244. YearDown.setPreferredSize(new Dimension(16, 16));
  245. YearDown.setSize(new Dimension(16, 16));
  246. YearDown.setText("▼");
  247. YearDown.addActionListener(new java.awt.event.ActionListener() {
  248. public void actionPerformed(ActionEvent e) {
  249. YearDown_actionPerformed(e);
  250. }
  251. });
  252. YearUp.setBorder(null);
  253. YearUp.setMaximumSize(new Dimension(16, 16));
  254. YearUp.setMinimumSize(new Dimension(16, 16));
  255. YearUp.setPreferredSize(new Dimension(16, 16));
  256. YearUp.setSize(new Dimension(16, 16));
  257. YearUp.setText("▲");
  258. YearUp.addActionListener(new java.awt.event.ActionListener() {
  259. public void actionPerformed(ActionEvent e) {
  260. YearUp_actionPerformed(e);
  261. }
  262. });
  263.  
  264. jPanelDay.setLayout(new BorderLayout());
  265.  
  266. Days.setLayout(new GridLayout(6,7));
  267. Days.setBackground(SystemColor.info);
  268.  
  269. for(int i=0;i<42;i++){
  270. days[i]=new JToggleButton();
  271. days[i].setBorder(null);
  272. days[i].setBackground(SystemColor.info);
  273. days[i].setHorizontalAlignment(SwingConstants.CENTER);
  274. days[i].setHorizontalTextPosition(SwingConstants.CENTER);
  275. //days[i].setSize(l,l);
  276. days[i].addActionListener(new java.awt.event.ActionListener(){
  277. public void actionPerformed(ActionEvent e) {
  278. day=Integer.parseInt(((JToggleButton)e.getSource()).getText());
  279. showDate();
  280. showDays();
  281. }
  282. });
  283. Days.add(days[i]);
  284. }
  285.  
  286. this.add(Main, BorderLayout.NORTH);
  287. this.add(jPanelDay, BorderLayout.CENTER);
  288. this.add(jPanelMonth, BorderLayout.SOUTH);
  289.  
  290. Main.add(Year, BorderLayout.CENTER);
  291. Main.add(Out, BorderLayout.WEST);
  292. Main.add(jPanelButton, BorderLayout.EAST);
  293.  
  294. jPanelButton.add(YearUp);
  295. jPanelButton.add(YearDown);
  296.  
  297. jPanelDay.add(weekPanel,BorderLayout.NORTH);
  298. jPanelDay.add(Days, BorderLayout.CENTER);
  299.  
  300. jPanelMonth.add(Month, BorderLayout.CENTER);
  301. jPanelMonth.add(MonthDown, BorderLayout.WEST);
  302. jPanelMonth.add(MonthUp, BorderLayout.EAST);
  303.  
  304. showMonth();
  305. showYear();
  306. showDate();
  307. showDays();
  308. }
  309.  
  310. //自定义重画年选择面板
  311. void jPanelButtonComponentResized(java.awt.event.ComponentEvent evt){
  312. YearUp.setLocation(0,0);
  313. YearDown.setLocation(0,YearUp.getHeight());
  314. jPanelButton.setSize(YearUp.getWidth(),YearUp.getHeight()*2);
  315. jPanelButton.setPreferredSize(new Dimension(YearUp.getWidth(),YearUp.getHeight()*2));
  316. jPanelButton.updateUI();
  317. }
  318.  
  319. //测试用
  320. public static void main(String[] args){
  321. JFrame f=new JFrame();
  322. f.setContentPane(new JCalendar());
  323. f.pack();
  324. //f.setResizable(false);
  325. f.show();
  326. }
  327.  
  328. //增加年份
  329. void YearUp_actionPerformed(ActionEvent e) {
  330. year++;
  331. showYear();
  332. showDate();
  333. showDays();
  334. }
  335.  
  336. //减少年份
  337. void YearDown_actionPerformed(ActionEvent e) {
  338. year--;
  339. showYear();
  340. showDate();
  341. showDays();
  342. }
  343.  
  344. //减少月份
  345. void MonthDown_actionPerformed(ActionEvent e) {
  346. month--;
  347. if(month<0) {
  348. month = 11;
  349. year--;
  350. showYear();
  351. }
  352. showMonth();
  353. showDate();
  354. showDays();
  355. }
  356.  
  357. //增加月份
  358. void MonthUp_actionPerformed(ActionEvent e) {
  359. month++;
  360. if(month==12) {
  361. month=0;
  362. year++;
  363. showYear();
  364. }
  365. showMonth();
  366. showDate();
  367. showDays();
  368. }
  369.  
  370. //初始化年月日
  371. void iniCalender(){
  372. year=cal.get(Calendar.YEAR);
  373. month=cal.get(Calendar.MONTH);
  374. day=cal.get(Calendar.DAY_OF_MONTH);
  375. }
  376.  
  377. //刷新月份
  378. void showMonth(){
  379. Month.setText(Integer.toString(month+1)+"月");
  380. }
  381.  
  382. //刷新年份
  383. void showYear(){
  384. Year.setText(Integer.toString(year)+"年");
  385. }
  386.  
  387. //刷新日期
  388. void showDate(){
  389. Out.setText(Integer.toString(year)+"-"+Integer.toString(month+1)+"-"+Integer.toString(day));
  390. }
  391.  
  392. //重画天数选择面板
  393. void showDays() {
  394. cal.set(year,month,1);
  395. int firstDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
  396. int n=mm[month];
  397. if(cal.isLeapYear(year)&&month==1) n++;
  398. int i=0;
  399. for(;i<firstDayOfWeek-1;i++){
  400. days[i].setEnabled(false);
  401. days[i].setSelected(false);
  402. days[i].setText("");
  403. }
  404. int d=1;
  405. for(;d<=n;d++){
  406. days[i].setText(Integer.toString(d));
  407. days[i].setEnabled(true);
  408. if(d==day) days[i].setSelected(true);
  409. else days[i].setSelected(false);;
  410. i++;
  411. }
  412. for(;i<42;i++){
  413. days[i].setEnabled(false);
  414. days[i].setSelected(false);
  415. days[i].setText("");
  416. }
  417. }
  418.  
  419. //单击年份面板选择整个年份字符串
  420. void SelectionYear(){
  421. Year.setSelectionStart(0);
  422. Year.setSelectionEnd(Year.getText().length());
  423. }
  424.  
  425. //单击月份面板选择整个月份字符串
  426. void SelectionMonth(){
  427. Month.setSelectionStart(0);
  428. Month.setSelectionEnd(Month.getText().length());
  429. }
  430.  
  431. //月份面板响应鼠标单击事件
  432. void Month_mouseClicked(MouseEvent e) {
  433. //SelectionMonth();
  434. inputMonth();
  435. }
  436.  
  437. //检验输入的月份
  438. void inputMonth(){
  439. if(Month.getText().endsWith("月"))
  440. {
  441. s=Month.getText().substring(0,Month.getText().length()-1);
  442. }
  443. else s=Month.getText();
  444. month=Integer.parseInt(s)-1;
  445. this.showMe();
  446. }
  447.  
  448. //月份面板键盘敲击事件响应
  449. void Month_keyPressed(KeyEvent e) {
  450. if(e.getKeyChar()==10)
  451. inputMonth();
  452. }
  453.  
  454. //年份面板响应鼠标单击事件
  455. void Year_mouseClicked(MouseEvent e) {
  456. //SelectionYear();
  457. inputYear();
  458. }
  459.  
  460. //年份键盘敲击事件响应
  461. void Year_keyPressed(KeyEvent e) {
  462. //System.out.print(new Integer(e.getKeyChar()).byteValue());
  463. if(e.getKeyChar()==10)
  464. inputYear();
  465. }
  466.  
  467. //检验输入的年份字符串
  468. void inputYear() {
  469. if(Year.getText().endsWith("年"))
  470. {
  471. s=Year.getText().substring(0,Year.getText().length()-1);
  472. }
  473. else s=Year.getText();
  474. year=Integer.parseInt(s);
  475. this.showMe();
  476. }
  477.  
  478. //以字符串形式返回日期,yyyy-mm-dd
  479. public String getDate(){return Out.getText();}
  480.  
  481. //以字符串形式输入日期,yyyy-mm-dd
  482. public void setDate(String date){
  483. if(date!=null){
  484. StringTokenizer f = new StringTokenizer(date, "-");
  485. if(f.hasMoreTokens())
  486. year = Integer.parseInt(f.nextToken());
  487. if(f.hasMoreTokens())
  488. month = Integer.parseInt(f.nextToken());
  489. if(f.hasMoreTokens())
  490. day = Integer.parseInt(f.nextToken());
  491. cal.set(year,month,day);
  492. }
  493. this.showMe();
  494. }
  495.  
  496. //以日期对象形式输入日期
  497. public void setTime(Date date){
  498. cal.setTime(date);
  499. this.iniCalender();
  500. this.showMe();
  501. }
  502.  
  503. //返回日期对象
  504. public Date getTime(){return cal.getTime();}
  505.  
  506. //返回当前的日
  507. public int getDay() {
  508. return day;
  509. }
  510.  
  511. //设置当前的日
  512. public void setDay(int day) {
  513. this.day = day;
  514. cal.set(this.year,this.month,this.day);
  515. this.showMe();
  516. }
  517.  
  518. //设置当前的年
  519. public void setYear(int year) {
  520. this.year = year;
  521. cal.set(this.year,this.month,this.day);
  522. this.showMe();
  523. }
  524.  
  525. //返回当前的年
  526. public int getYear() {
  527. return year;
  528. }
  529.  
  530. //返回当前的月
  531. public int getMonth() {
  532. return month;
  533. }
  534.  
  535. //设置当前的月
  536. public void setMonth(int month) {
  537. this.month = month;
  538. cal.set(this.year,this.month,this.day);
  539. this.showMe();
  540. }
  541.  
  542. //刷新
  543. public void showMe(){
  544. this.showDays();
  545. this.showMonth();
  546. this.showYear();
  547. this.showDate();
  548. }
  549.  
  550. }
  551.  
  552. public class TestJCalendar {
  553. public static void main(String[] args) {
  554. JFrame f=new JFrame();
  555. f.setContentPane(new JCalendar());
  556. f.pack();
  557. //f.setResizable(false);
  558. f.show();
  559. }
  560. }

Reply to "perpetual calendar"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website