资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java万年历带界面代码 JAVA万年历

JAVA万年历代码

/*

成都创新互联长期为近1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为瓯海企业提供专业的成都网站制作、成都网站设计,瓯海网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。

题目:输出任意年份任意月份的日历表(公元后)

思路:

1.已知1年1月1日是星期日,1 % 7 = 1 对应的是星期日,2 % 7 = 2 对应的是星期一,以此类推;

2.计算当年以前所有天数+当年当月1号之前所有天数;

a.年份分平年闰年,平年365天,闰年366天;

b.闰年的判断方法year % 400 == 0 || (year % 100 != 0  year % 4 == 0)若为真,则为闰年否则为平年;

c.定义平年/闰年数组,包含各月天数;

d.遍历数组求和,计算当年当月前总天数;

e.当年以前所有天数+当年当月前总天数+1即为1年1月1日到当年当月1日的总天数;

3.总天数对7取模,根据结果判断当月1号是星期几,输出空白区域;

4.输出当月日历表,逢星期六换行

*/

import java.util.Scanner;

class FindMonthList {

public static void main(String[] args){

Scanner sc = new Scanner(System.in);

System.out.println("请输入年份:");

int year = sc.nextInt();            //年份

if (year  1) {                        //判断非法输入年份

System.out.println("输入错误!");

return;

}

System.out.println("请输入月份:");

int month = sc.nextInt();            //月份

if (month  1 || month  12) {        //判断非法输入月份

System.out.println("输入错误!");

return;

}

//输出表头

System.out.println("-------" + year + " 年 " + month + " 月 " + "-------");

System.out.println();

System.out.println("日  一  二  三  四  五  六");

//计算当前年份以前所有天数beforeYearTotalDay;每4年一个闰年,闰年366天,平年365天

int beforeYearTotalDay = ((year - 1) / 4 * 366) + (year-1 - ((year - 1) / 4)) * 365;

int[] arrLeapYear = {0,31,29,31,30,31,30,31,31,30,31,30,31};    //闰年各月天数    int数组

int[] arrNormalYear = {0,31,28,31,30,31,30,31,31,30,31,30,31};    //平年各月天数    int数组

int beforeMonthTotalDay = 0;                                    //定义本年当月之前月份的总天数

if (year % 400 == 0 || (year % 100 != 0  year % 4 == 0)) {    //判断当前年份是否是闰年

for (int i = 0 ; i  month ; i ++ ) {    //for循环计算当月之前总天数

//计算当前月份之前的所有天数

beforeMonthTotalDay = beforeMonthTotalDay + arrLeapYear[i];

}

//判断当月1日是星期几

int totalDay = beforeYearTotalDay + beforeMonthTotalDay + 1;

int week = totalDay % 7;//已知1年1月1日是星期日,即模7得1对应的是星期日

for (int i = 0 ; i  (week - 1 + 7) % 7 ; i ++) {    //如果写成i  (week-1)会出现i-1的情况

System.out.print("    ");//输出开头空白

}

for (int i = 1 ;i = arrLeapYear[month] ;i ++ ) {    //for循环输出各月天数

System.out.print(i + "  ");

if (i  10 ) {        //小于10的数补一个空格,以便打印整齐

System.out.print(" ");

}

if (i % 7 == ((7-(week - 1)) % 7 ) || i == arrLeapYear[month]) {//每逢星期六/尾数换行

System.out.println();

}

}

} else {        //不是闰年就是平年

for (int i = 0 ; i  month ; i ++ ) {    //for循环计算出当月之前月份总天数

beforeMonthTotalDay = beforeMonthTotalDay + arrNormalYear[i];

}

//判断当月1日是星期几

int totalDay = beforeYearTotalDay + beforeMonthTotalDay + 1;

int week = totalDay % 7;//已知1年1月1日是星期日,即模7得1对应的是星期日

for (int i = 0 ; i  (week - 1 + 7) % 7 ; i ++) {    //如果写成i  (week-1)会出现i-1的情况

System.out.print("    ");//输出开头空白

}

for (int i = 1 ;i = arrNormalYear[month] ;i ++ ) {//for循环输出各月天数

System.out.print(i + "  ");

if (i  10 ) {            //小于10的数补一个空格,以便打印整齐

System.out.print(" ");

}

if (i % 7 == ((7-(week - 1)) % 7 ) || i == arrNormalYear[month]) {//每逢星期六/尾数换行

System.out.println();

}

}

}

}

}

显示效果:

java万年历,我想在右下角加一个白色的框能够查询年月日,求在代码中补充我需要的代码

自己简单写了一下代码,可以学习下:

界面代码:

inputDateText=new JTextField();

inputDateText.setText("yyyy-MM-dd");

JPanel inputDatePanel=new JPanel();

inputDatePanel.setLayout(new GridLayout(3, 1));// 网格布局,分成两行一列

inputDatePanel.add(inputDateLabel);

inputDatePanel.add(inputDateText);

// 添加确认按钮

JPanel btnPanel=new JPanel();

confirmBtn=new JButton("确认");

confirmBtn.addActionListener(this);

btnPanel.add(confirmBtn); // JPanel默认就是FlowLaout居中对齐

inputDatePanel.add(btnPanel);

JPanel emptyPanel=new JPanel();

emptyPanel.setSize(150, 240);

p3.add(emptyPanel); //右侧中间部分填充

p3.add(inputDatePanel,BorderLayout.SOUTH);

2、按钮事件

if (e.getSource() == confirmBtn) { // 点击输入日期文本框下方的确认按钮

String dateText = inputDateText.getText();

if (dateText==null || "".equals(dateText.trim())) {

JOptionPane.showMessageDialog(null, "请输入日期!");

inputDateText.setText("yyyy-MM-dd");

return;

} else {

dateText=dateText.trim();

if (dateText.trim().length()==10 || dateText.matches(DATEREGEX)) {

// 

} else {

JOptionPane.showMessageDialog(null, "输入的日期不符合格式,请重新输入!");

inputDateText.setText("yyyy-MM-dd");

return;

}

SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");

Date inputDate=null;

try {

inputDate=dateFormat.parse(dateText);

} catch (ParseException e1) { // 用户输入的日期不符合格式

e1.printStackTrace();

inputDateText.setText("yyyy-MM-dd");

JOptionPane.showMessageDialog(null, "输入的日期不符合格式,请重新输入!");

return;

}

Calendar calendar=Calendar.getInstance();

calendar.setTime(inputDate);

jtfYear.setText(calendar.get(Calendar.YEAR)+""); // 根据输入的日期解析出年月填写到年月文本框

jtfMonth.setText(calendar.get(Calendar.MONTH)+1+"");

calendar.setTime(new Date());

}

}

求一个java swing带界面的万年历代码

按照你的要求编写的Java swing 带界面的万年历代码如下

//日历

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Calendar;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class CCI extends JFrame implements ActionListener{

JButton jb1=new JButton("");

JButton jb2=new JButton("");

JButton jb3=new JButton("");

JButton jb4=new JButton("");

JPanel jp1=new JPanel();

JPanel jp2=new JPanel();

JPanel jp3=new JPanel();

JPanel jp4=new JPanel();

JLabel jl1=new JLabel();

JLabel jl2=new JLabel();

JLabel[]jl=new JLabel[49];

String []week={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

Calendar c=Calendar.getInstance();

int year,month,day;

int nowyear,nowmonth,nowday;

CCI(){

super("简单日历");

nowyear=c.get(Calendar.YEAR);

nowmonth=c.get(Calendar.MONTH)+1;

nowday=c.get(Calendar.DAY_OF_MONTH);

year=nowyear;

month=nowmonth;

day=nowday;

String s=year+"年"+month+"月";

jl1.setForeground(Color.RED);

jl1.setFont(new Font(null,Font.BOLD,20));

jl1.setText(s);

jb1.addActionListener(this);

jb2.addActionListener(this);

jb3.addActionListener(this);

jb4.addActionListener(this);

jp1.add(jb1);jp1.add(jb2);jp1.add(jl1);jp1.add(jb3);jp1.add(jb4);

jp2.setLayout(null);

createMonthPanel();

jp2.add(jp3);

jl2.setFont(new Font(null,Font.BOLD,20));

jl2.setText("今天是"+nowyear+"年"+nowmonth+"月"+nowday+"日");

jp4.add(jl2);

add(jp1,BorderLayout.NORTH);

add(jp2,BorderLayout.CENTER);

add(jp4,BorderLayout.SOUTH);

setSize(500,500);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

setVisible(true);

}

@Override

public void actionPerformed(ActionEvent ae) {

if(ae.getSource()==jb1){

year=year-1;

String s=year+"年"+month+"月";

jl1.setText(s);

jp3.removeAll();

createMonthPanel();

jp3.validate();

}

if(ae.getSource()==jb2){

if(month==1){

year=year-1;

month=12;

}else{

month=month-1;

}

String s=year+"年"+month+"月";

jl1.setText(s);

jp3.removeAll();

createMonthPanel();

jp3.validate();

}

if(ae.getSource()==jb3){

if(month==12){

year=year+1;

month=1;

}else{

month=month+1;

}

String s=year+"年"+month+"月";

jl1.setText(s);

jp3.removeAll();

createMonthPanel();

jp3.validate();

}

if(ae.getSource()==jb4){

year=year+1;

String s=year+"年"+month+"月";

jl1.setText(s);

jp3.removeAll();

createMonthPanel();

jp3.validate();

}

}

public static void main(String[] args) {

new CCI();

}

public int getMonthDays(int year, int month) { 

switch (month) {

case 1: 

case 3: 

case 5: 

case 7:

case 8: 

case 10: 

case 12:

return 31; 

case 2: 

if ((year%4==0year%100!=0)||year%400==0) { 

return 29; 

} else { 

return 28; 

default: 

return 30; 

public void createMonthPanel(){

c.set(year, month-1, getMonthDays(year,month));

int weekOfMonth=c.get(Calendar.WEEK_OF_MONTH);

if(weekOfMonth==6){

jp3.setLayout(new GridLayout(7,7));

jp3.setBounds(50, 20, 420, 350);

}else{

jp3.setLayout(new GridLayout(6,7));

jp3.setBounds(50, 20, 420, 300);

}

jp3.setBorder(BorderFactory.createEtchedBorder());

for(int i=0;i7;i++){

jl[i]=new JLabel(week[i],JLabel.CENTER);

jl[i].setFont(new Font(null,Font.BOLD,20));

jl[i].setBorder(BorderFactory.createEtchedBorder());

jp3.add(jl[i]);

}

c.set(year, month-1, 1);

int emptyFirst=c.get(Calendar.DAY_OF_WEEK)-1;

int daysOfMonth=getMonthDays(year,month);

for(int i=6+emptyFirst;i=7;i--){

int intyear=year;

int intmonth=month;

if(intmonth==1){

intyear=intyear-1;

intmonth=12;

}else{

intmonth=intmonth-1;

}

int intdays=getMonthDays(intyear,intmonth);

jl[i]=new JLabel((intdays+7-i)+"",JLabel.CENTER);

jl[i].setFont(new Font(null,Font.BOLD,20));

jl[i].setForeground(Color.GRAY);

jl[i].setBorder(BorderFactory.createEtchedBorder());

jp3.add(jl[i]);

}

for(int i=7+emptyFirst;idaysOfMonth+7+emptyFirst;i++){

jl[i]=new JLabel((i-7-emptyFirst+1)+"",JLabel.CENTER);

jl[i].setFont(new Font(null,Font.BOLD,20));

if((i+1)%7==0 || (i+1)%7==1){

jl[i].setForeground(Color.RED);

}else if((i-7-emptyFirst+1)==nowdaymonth==nowmonthyear==nowyear)

jl[i].setForeground(Color.BLUE);

else

jl[i].setForeground(Color.BLACK);

jl[i].setBorder(BorderFactory.createEtchedBorder());

jp3.add(jl[i]);

}

if(weekOfMonth==6)

for(int i=48;i=daysOfMonth+emptyFirst+7;i--){

jl[i]=new JLabel((49-i)+"",JLabel.CENTER);

jl[i].setFont(new Font(null,Font.BOLD,20));

jl[i].setForeground(Color.GRAY);

jl[i].setBorder(BorderFactory.createEtchedBorder());

jp3.add(jl[i]);

}

else

for(int i=41;i=daysOfMonth+emptyFirst+7;i--){

jl[i]=new JLabel((42-i)+"",JLabel.CENTER);

jl[i].setFont(new Font(null,Font.BOLD,20));

jl[i].setForeground(Color.GRAY);

jl[i].setBorder(BorderFactory.createEtchedBorder());

jp3.add(jl[i]);

}

}

}


本文标题:java万年历带界面代码 JAVA万年历
URL标题:http://cdkjz.cn/article/docpghi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220