这篇“java如何实现日历应用程序设计”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“java如何实现日历应用程序设计”文章吧。
1、效果和要求
*在文本框inputYear中输入年份,验证年份的有效性;按回车键后,显示输入年份的正确日历
* 单击previousMonth按钮可以显示当前月的上一月的日历;如果月份小于1,则显示上一年的12月
* 单击nextMonth按钮,可以显示当前月的下一月的日历;如果月份大于1,则显示下一年的1月
2、源码
CalendarBean.java
import java.util.Calendar;public class CalendarBean { String day[]; int year = 2013, month = 0; 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; } //返回某年某月1号开始的日期数组 public String[] getDays() { String a[] = new String[42]; Calendar 日历 = Calendar.getInstance(); //注意:1月份是从0开始,所以要减1 日历.set(year, month - 1, 1); int 星期几 = 日历.get(Calendar.DAY_OF_WEEK) - 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; } } for (int i = 星期几, n = 1; i < 星期几 + day; i++) { a[i] = String.valueOf(n); n++; } return a; }}
CalendarFrame.java
import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class CalendarFrame extends JFrame{ JLabel labelDay[] = new JLabel[42]; JLabel labelYear; JButton titleName[] = new JButton[7]; JButton nextMonth, previousMonth; JLabel showMessage; JTextField inputYear; CalendarBean calendar; String name[] = { "日", "一", "二", "三", "四", "五", "六" }; int year = 2013, month = 1; String days[]; public CalendarFrame() { super("日历应用程序"); calendar = new CalendarBean(); calendar.setYear(year); calendar.setMonth(month); days = calendar.getDays(); // 窗口添加scrollPane在中心区 add(getCenterPanel(),BorderLayout.CENTER); // 窗口添加pNorth 在北面区域 add(getNorthPanel(),BorderLayout.NORTH); // 窗口添加pSouth 在南区域。 add(getSouthPanel(),BorderLayout.SOUTH); } private JPanel getNorthPanel() { JPanel panel = new JPanel(); labelYear = new JLabel("请输入年份:"); inputYear = new JTextField(10); previousMonth = new JButton("上月"); nextMonth = new JButton("下月"); inputYear.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try{ year = Integer.parseInt(inputYear.getText()); }catch (NumberFormatException el){ JOptionPane.showMessageDialog(null,"请输入正确年份!"); } calendar.setYear(year); UpdateUI(); } }); previousMonth.addActionListener(new ActionListener() {//上个月 @Override public void actionPerformed(ActionEvent e) { month--;//获取上个月的时间 //System.out.println("Hello World"); if(month<=0){//跳到上一年 year--; month = 12; } UpdateUI();; } }); nextMonth.addActionListener(new ActionListener() {//下个月 @Override public void actionPerformed(ActionEvent e) { month++; if(month>12){//到达下一年 year++; month=1; } UpdateUI();; } }); panel.add(labelYear,new FlowLayout()); panel.add(inputYear,new FlowLayout()); panel.add(previousMonth,new FlowLayout()); panel.add(nextMonth,new FlowLayout()); return panel; } private JPanel getCenterPanel() { JPanel panel = new JPanel(); // 将panel的布局设置为7行7列的GridLayout布局 panel.setLayout(new GridLayout(7,7)); for (int i = 0; i < 7; i++) { titleName[i] = new JButton(name[i]); // panel添加组件titleName[i] panel.add(titleName[i]); } for (int i = 0; i < 42; i++) { labelDay[i] = new JLabel(days[i], JLabel.CENTER); // panel添加组件labelDay[i] panel.add(labelDay[i]); } return panel; } private JPanel getSouthPanel() { JPanel panel = new JPanel(); showMessage = new JLabel("日历: " + year + "年" + month + "月"); panel.add(showMessage); return panel; } //添加的方法 private void UpdateUI(){ calendar.setYear(year); calendar.setMonth(month); days = calendar.getDays(); for (int i = 0; i < 42; i++) { labelDay[i].setText(days[i]); } showMessage.setText("日历:"+calendar.getYear()+"年"+calendar.getMonth()+"月"); }}
CalendarMainClass.java
import javax.swing.*;public class CalendarMainClass { public static void main(String args[]) { CalendarFrame frame = new CalendarFrame(); frame.setBounds(100, 100, 360, 320); frame.setVisible(true); frame.validate(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}
以上就是关于“java如何实现日历应用程序设计”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注编程网行业资讯频道。