java实现日历窗口小程序
本文实例为大家分享了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()); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)