Java课设之万年历

该程序使用Java Swing库创建了一个图形用户界面的万年历应用,允许用户查询1900年至2028年间任意年月的日期与星期。通过组合框选择年份和月份,点击查询按钮后,程序会根据所选日期计算出当月的日历,并显示在文本区域中,包括日期和对应的星期。核心算法涉及了对闰年和平年判断以及不同月份天数的计算,确保了日历的准确性。
摘要由CSDN通过智能技术生成

用Java语言编写一个万年历,满足以下要求:

(1)使用图形用户界面

(2)实现日期与星期的查询

package test;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class Test2 extends JFrame{
	//pUp面板用于盛放年、月和查询组件
	//pCenter用于盛放滚动组件
	JPanel pUp=new JPanel();
	JPanel pCenter=new JPanel();
	JLabel lblYear=new JLabel("年");                                                                                                                                                                                                                                                                                                                                                  
	JLabel lblMonth=new JLabel("月");
	JComboBox cbxYear=new JComboBox();
	JComboBox cbxMonth=new JComboBox();
	JButton btnQuery=new JButton("查询");
	//建立一个15行60列的文本区
	JTextArea jtaContent=new JTextArea(15,60);
	public Test2() {
		super("万年历");
		//在构造方法中添加各个组件
		pUp.add(lblYear);pUp.add(cbxYear);
		pUp.add(lblMonth);pUp.add(cbxMonth);pUp.add(btnQuery);
		pCenter.add(jtaContent);
		//采用边框式布局
		this.add(pUp,BorderLayout.NORTH);
		this.add(pCenter,BorderLayout.CENTER);
		for(int i=1900;i<2029;i++) {
			//为cbxYear添加一个可以选择的范围;
            //String.valueOf(i)将i由int变为String类型;
			cbxYear.addItem(String.valueOf(i));
		}
		//获取当前时间,将cbxYear默认设置为当前年份
		Calendar calendar=Calendar.getInstance();
		cbxYear.setSelectedItem(String.valueOf(calendar.get(Calendar.YEAR)));
		for(int i=1;i<=12;i++) {
			cbxMonth.addItem(String.valueOf(i));
		}
		cbxMonth.setSelectedItem(String.valueOf(calendar.get(Calendar.MONTH)+1));
		//“查询”按钮添加事件
		btnQuery.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//获取cbxYear中的值,将其转换为字符串并赋给y,将y转换为int型赋给yt
				String y=cbxYear.getSelectedItem().toString();
				int yt=Integer.valueOf(y);
				int mt=Integer.valueOf(cbxMonth.getSelectedItem().toString());
				int day=0;
				int totalDays=0;
				boolean isLeap;
				if(yt%400==0||(yt%4==0&&yt%100!=0))
					isLeap=true;
				else
					isLeap=false;
				//统计从1900年到指定年月(该月的前一个月)的总天数,1900年的第一天恰好为周一
				for(int i=1900;i<yt;i++) {
					if(i%400==0||(i%4==0&&i%100!=0)) 
						totalDays+=366;
					else
						totalDays+=365;
				}
				for(int i=1;i<=mt;i++) {
					switch(i) {
					case 1:
					case 3:
					case 5:
					case 7:
					case 8:
					case 10:
					case 12:
						day=31;
						break;
					case 4:
					case 6:
					case 9:
					case 11:
						day=30;
						break;
					case 2:
						if(isLeap)
							day=29;
						else
							day=28;
					}
					if(i!=mt)
						totalDays+=day;
				}
				//确定该月的第一天所在行前面有几个"\t"
				int t=(totalDays+1)%7;
				jtaContent.setText("日\t一\t二\t三\t四\t五\t六\n");
				for(int i=0;i<t;i++) {
					jtaContent.append("\t");
				}
				for(int i=1;i<=day;i++) {
					jtaContent.append(i+"\t");
					if((t+i)%7==0)
						jtaContent.append("\n");
				}
			}
		});
		
	}
	public static void main(String[] args) {
		Test2 t=new Test2();
		t.setSize(700, 400);
		t.setVisible(true);
		//将窗口设置为屏幕中间位置
		t.setLocationRelativeTo(null);
		t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

该程序中最核心的算法就是对年份月份的判断和最初日期的选择,如不同的年份,月份它们的天数不同。如1,3,5,7,8,10,12月均有31天,4,6,9,11月均有30天,平年2月有28天,闰年2月有29天。因为万年历中还要知道某一天具体是周几,所以我们要确定第一天是星期几,后面的日期依次填充即可。本程序的查询范围为1900年1月至2028年12月,故第一天是1900年1月1日,经查询当天是周一,故1900年1月2日为周二,1900年1月3日为周三,依次类推,可以知道后面所有日期具体是周几。

运行结果:

  • 7
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
java万年历课程设计,有源代码 源代码片段:public class wannianli extends JFrame implements ActionListener, MouseListener { private Calendar cld = Calendar.getInstance(); private String [] astr = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"}; private DefaultTableModel dtm = new DefaultTableModel(null,astr); private JTable table = new JTable(dtm); //装日期的表格 private JScrollPane sp = new JScrollPane(table); private JButton bLastYear = new JButton("上一年"); private JButton bNextYear = new JButton("下一年"); private JButton bLastMonth = new JButton("上月"); private JButton bNextMonth = new JButton("下月"); private JTextField jtfYear = new JTextField(5);//jtfYear年份显示和输入文本框,允许编辑单行文本 private JTextField jtfMonth = new JTextField(2);//jtfMonth月份显示文本框 private JPanel p1 = new JPanel(); //装入控制日期按钮的模块 private JPanel p2 = new JPanel(); private JPanel p3 = new JPanel(new BorderLayout()); private JPanel p4 = new JPanel(new GridLayout(2,1)); private JPanel p5 = new JPanel(new BorderLayout()); private JButton bAdd = new JButton("保存日志"); private JButton bDel = new JButton("删除日志"); private JTextArea jta = new JTextArea(); //JTextArea 是一个显示纯文本的多行区域 private JScrollPane jsp = new JScrollPane(jta); //管理视口、可选的垂直和水平滚动条以及可选的行和列标题视口 private JLabel l = new JLabel("小提示:可直接输入年份,提高查询效率!"); private JLabel lt = new JLabel();//系统 private JLabel ld = new JLabel();//日期是否被选择 private int lastTime; JMenuBar jmb = new JMenuBar(); JMenu view;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值