今天小编给大家分享一下java如何实现日历窗口小程序的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
标签:java基础
功能:
1.按月翻页。
2.输入月份年份,直接跳转。
效果图:
//CalendarMainClass.java public class CalendarMainClass{ public static void main(String args[]){ CalendarFrame frame = new CalendarFrame(); frame.setBounds(100, 100, 360, 300); frame.setVisible(true); frame.setYearAndMonth(2017, 12); } }
//CalendarBean.java import java.util.Calendar; public class CalendarBean{ int year = 2017, month = 12; public void setYear(int year){ this.year = year; } public int getYear(){ return year; } public void setMonth(int month){ this.month = month; } public int getMonth(){ return month; } public String [] getCalendar(){ String [] a = new String[42]; //日历最多可达6行 Calendar rili = Calendar.getInstance(); rili.set(year, month - 1, 1); //模拟翻日历 int weekDay = rili.get(Calendar.DAY_OF_WEEK) - 2; ///计算出1号的星期 int day = 0; if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) day = 31; if(month == 4 || month == 6 || month == 9 || month == 11) day = 30; if(month == 2){ if(((year % 4 == 0) && (year % 100 != 0)) || year % 400 == 0) day = 29; else day = 28; } //if(weekDay == 0) weekDay += 7; for(int i = 0; i < weekDay; i++) a[i] = " "; //日历顺序输出,格式控制 for(int i = weekDay, n = 1; i < weekDay + day; i++){ a[i] = String.valueOf(n); n++; } for(int i = weekDay + day; i < a.length; i++) a[i] = " "; return a; } }
//CalendarFrame.java import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class CalendarFrame extends JFrame implements ActionListener{ JLabel labelDay[] = new JLabel[42]; JButton titleName [] = new JButton[7]; String name [] = {"一", "二", "三", "四", "五", "六", "日"}; JButton nextMonth, previousMonth, go; JTextField textYear, textMonth; CalendarBean calendar; JLabel showMessage = new JLabel("", JLabel.CENTER); int year = 2017, month = 12; public CalendarFrame(){ JPanel pCenter = new JPanel(); pCenter.setLayout(new GridLayout(7, 7)); for(int i = 0; i < 7; i++){ titleName[i] = new JButton(name[i]); titleName[i].setBorder(new SoftBevelBorder(BevelBorder.RAISED)); pCenter.add(titleName[i]); } for(int i = 0; i < 42; i++){ labelDay[i] = new JLabel("", JLabel.CENTER); labelDay[i].setBorder(new SoftBevelBorder(BevelBorder.RAISED)); pCenter.add(labelDay[i]); } calendar = new CalendarBean(); nextMonth = new JButton("next"); previousMonth = new JButton("previous"); go = new JButton("goto"); textYear = new JTextField(4); textMonth = new JTextField(2); nextMonth.addActionListener(this); previousMonth.addActionListener(this); go.addActionListener(this); JPanel pNorth = new JPanel(), pSouth = new JPanel(); pNorth.add(previousMonth); pNorth.add(showMessage); pNorth.add(nextMonth); pSouth.add(textYear); pSouth.add(textMonth); pSouth.add(go); add(pCenter, BorderLayout.CENTER); add(pNorth, BorderLayout.NORTH); add(pSouth, BorderLayout.SOUTH); setYearAndMonth(year, month); setDefaultCloseOperation(DISPOSE_ON_CLOSE); } public void setYearAndMonth(int y, int m){ calendar.setYear(y); calendar.setMonth(m); String day[] = calendar.getCalendar(); /* for(int i = 0; i < 42; i++){ if(i % 7 == 0) System.out.println(""); System.out.printf("%4s", day[i]); } */ for(int i = 0; i < 42; i++) labelDay[i].setText(day[i]); showMessage.setText(calendar.getYear() + "/" + calendar.getMonth()); } public void actionPerformed(ActionEvent e){ if(e.getSource() == nextMonth){ //下一个月 month += 1; if(month > 12){ year += 1; month = 1; } calendar.setYear(year); calendar.setMonth(month); String day[] = calendar.getCalendar(); for(int i = 0; i < 42; i++) labelDay[i].setText(day[i]); } else if(e.getSource() == previousMonth){ //上一个月 month -= 1; if(month < 1){ year -= 1; month = 12; } calendar.setYear(year); calendar.setMonth(month); String day[] = calendar.getCalendar(); for(int i = 0; i < 42; i++) labelDay[i].setText(day[i]); } else if(e.getSource() == go){ //跳转 year = Integer.parseInt(textYear.getText().trim()); month = Integer.parseInt(textMonth.getText().trim()); calendar.setYear(year); calendar.setMonth(month); String day[] = calendar.getCalendar(); for(int i = 0; i < 42; i++) labelDay[i].setText(day[i]); } showMessage.setText(calendar.getYear() + "/" + calendar.getMonth()); } }
以上就是“java如何实现日历窗口小程序”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。