java程序代码日历,java实现日历

用java写的日历记事本代码?

详细代码

成都创新互联主要为客户提供服务项目涵盖了网页视觉设计、VI标志设计、网络营销推广、网站程序开发、HTML5响应式网站建设公司手机网站制作设计、微商城、网站托管及网站建设维护、WEB系统开发、域名注册、国内外服务器租用、视频、平面设计、SEO优化排名。设计、前端、后端三个建站步骤的完善服务体系。一人跟踪测试的建站服务标准。已经为成都砂岩浮雕行业客户提供了网站制作服务。

//CalendarWindow类:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.io.*;

public class CalendarWindow extends JFrame implements ActionListener,

MouseListener,FocusListener{

int year,month,day;

CalendarMessage calendarMessage;

CalendarPad calendarPad;

NotePad notePad;

JTextField showYear,showMonth;

JTextField [] showDay;

CalendarImage calendarImage;

Clock clock;

JButton nextYear,previousYear,nextMonth,previousMonth;

JButton saveDailyRecord,deleteDailyRecord,readDailyRecord;

File dir;

Color backColor=Color.gray;

public CalendarWindow(){

dir=new File("./dailyRecord");

dir.mkdir();

showDay=new JTextField[42];

for(int i=0;ishowDay.length;i++){

showDay[i]=new JTextField();

showDay[i].setBackground(backColor);

showDay[i].setLayout(new GridLayout(3,3));

showDay[i].addMouseListener(this);

showDay[i].addFocusListener(this);

}

calendarMessage=new CalendarMessage();

calendarPad=new CalendarPad();

notePad=new NotePad();

Calendar calendar=Calendar.getInstance();

calendar.setTime(new Date());

year=calendar.get(Calendar.YEAR);

month=calendar.get(Calendar.MONTH)+1;

day=calendar.get(Calendar.DAY_OF_MONTH);

calendarMessage.setYear(year);

calendarMessage.setMonth(month);

calendarMessage.setDay(day);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.setShowDayTextField(showDay);

notePad.setShowMessage(year,month,day);

calendarPad.showMonthCalendar();

doMark(); //给有日志的号码做标记,见后面的doMark()方法

calendarImage=new CalendarImage();

calendarImage.setImageFile(new File("sea.jpg"));

clock=new Clock();

JSplitPane splitV1=

new JSplitPane(JSplitPane.VERTICAL_SPLIT,calendarPad,calendarImage);

JSplitPane splitV2=

new JSplitPane(JSplitPane.VERTICAL_SPLIT,notePad,clock);

JSplitPane splitH=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,splitV1,splitV2);

add(splitH,BorderLayout.CENTER);

showYear=new JTextField(""+year,6);

showYear.setFont(new Font("TimesRoman",Font.BOLD,12));

showYear.setHorizontalAlignment(JTextField.CENTER);

showMonth=new JTextField(" "+month,4);

showMonth.setFont(new Font("TimesRoman",Font.BOLD,12));

showMonth.setHorizontalAlignment(JTextField.CENTER);

nextYear=new JButton("下年");

previousYear=new JButton("上年");

nextMonth=new JButton("下月");

previousMonth=new JButton("上月");

nextYear.addActionListener(this);

previousYear.addActionListener(this);

nextMonth.addActionListener(this);

previousMonth.addActionListener(this);

showYear.addActionListener(this);

JPanel north=new JPanel();

north.add(previousYear);

north.add(showYear);

north.add(nextYear);

north.add(previousMonth);

north.add(showMonth);

north.add(nextMonth);

add(north,BorderLayout.NORTH);

saveDailyRecord=new JButton("保存日志") ;

deleteDailyRecord=new JButton("删除日志");

readDailyRecord=new JButton("读取日志");

saveDailyRecord.addActionListener(this);

deleteDailyRecord.addActionListener(this);

readDailyRecord.addActionListener(this);

JPanel pSouth=new JPanel();

pSouth.add(saveDailyRecord);

pSouth.add(deleteDailyRecord);

pSouth.add(readDailyRecord);

add(pSouth,BorderLayout.SOUTH);

setVisible(true);//根据参数 b 的值显示或隐藏此 Window

setBounds(60,60,660,480);

validate();//验证此容器及其所有子组件

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置用户在此窗体上发起 "close" 时默认执行的操作

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==nextYear){

year++;

showYear.setText(""+year);

calendarMessage.setYear(year);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.showMonthCalendar();

notePad.setShowMessage(year,month,day);

doMark();

}

else if(e.getSource()==previousYear){

year--;

showYear.setText(""+year);

calendarMessage.setYear(year);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.showMonthCalendar();

notePad.setShowMessage(year,month,day);

doMark();

}

else if(e.getSource()==nextMonth){

month++;

if(month12) month=1;

showMonth.setText(" "+month);

calendarMessage.setMonth(month);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.showMonthCalendar();

notePad.setShowMessage(year,month,day);

doMark();

}

else if(e.getSource()==previousMonth){

month--;

if(month1) month=12;

showMonth.setText(" "+month);

calendarMessage.setMonth(month);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.showMonthCalendar();

notePad.setShowMessage(year,month,day);

doMark();

}

else if(e.getSource()==showYear){

String s=showYear.getText().trim();

char a[]=s.toCharArray();

boolean boo=false;

for(int i=0;ia.length;i++)

if(!(Character.isDigit(a[i])))

boo=true;

if(boo==true) //弹出“警告”消息对话框

JOptionPane.showMessageDialog(this,"您输入了非法年份","警告", JOptionPane.WARNING_MESSAGE);

else if(boo==false)

year=Integer.parseInt(s);

showYear.setText(""+year);

calendarMessage.setYear(year);

calendarPad.setCalendarMessage(calendarMessage);

calendarPad.showMonthCalendar();

notePad.setShowMessage(year,month,day);

doMark();

}

else if(e.getSource()==saveDailyRecord){

notePad.save(dir,year,month,day);

doMark();

}

else if(e.getSource()==deleteDailyRecord){

notePad.delete(dir,year,month,day);

doMark();

}

else if(e.getSource()==readDailyRecord)

notePad.read(dir,year,month,day);

}

public void mousePressed(MouseEvent e){

JTextField text=(JTextField)e.getSource();

String str=text.getText().trim();

try{ day=Integer.parseInt(str);

}

catch(NumberFormatException exp){

}

calendarMessage.setDay(day);

notePad.setShowMessage(year,month,day);

}

public void mouseReleased(MouseEvent e){}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseClicked(MouseEvent e) {}

public void focusGained(FocusEvent e){

Component com=(Component)e.getSource();

com.setBackground(Color.red);

}

public void focusLost(FocusEvent e){

Component com=(Component)e.getSource();

com.setBackground(backColor);

}

public void doMark(){

for(int i=0;ishowDay.length;i++){

showDay[i].removeAll();

String str=showDay[i].getText().trim();

try{

int n=Integer.parseInt(str);

if(isHaveDailyRecord(n)==true){ //见后面的isHaveDailyRecord()方法

JLabel mess=new JLabel("存");

mess.setFont(new Font("TimesRoman",Font.PLAIN,11));

mess.setForeground(Color.black) ;

showDay[i].add(mess);

} }

catch(Exception exp){}

}

calendarPad.repaint();

calendarPad.validate();

}

public boolean isHaveDailyRecord(int n){

String key=""+year+""+month+""+n;

String [] dayFile=dir.list();

boolean boo=false;

for(int k=0;kdayFile.length;k++){

if(dayFile[k].equals(key+".txt")){

boo=true;

break;

} }

return boo;

}

public static void main(String args[]){

new CalendarWindow();

}

}

//CalendarPad类:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

public class CalendarPad extends JPanel{

int year,month,day;

CalendarMessage calendarMessage;

JTextField [] showDay;

JLabel title[];

String [] 星期={"SUN/日","MON/一","TUE/二","WED/三","THU/四","FRI/五","SAT/六"};

JPanel north,center;

public CalendarPad(){

setLayout(new BorderLayout());

north=new JPanel();

north.setLayout(new GridLayout(1,7));

center=new JPanel();

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

add(center,BorderLayout.CENTER);

add(north,BorderLayout.NORTH);

title=new JLabel[7];

for(int j=0;j7;j++){

title[j]=new JLabel();

title[j].setFont(new Font("TimesRoman",Font.BOLD,12));

title[j].setText(星期[j]);

title[j].setHorizontalAlignment(JLabel.CENTER);

title[j].setBorder(BorderFactory.createRaisedBevelBorder());

north.add(title[j]);

}

title[0].setForeground(Color.red);

title[6].setForeground(Color.blue);

}

public void setShowDayTextField(JTextField [] text){

showDay=text;

for(int i=0;ishowDay.length;i++){

showDay[i].setFont(new Font("TimesRoman",Font.BOLD,15));

showDay[i].setHorizontalAlignment(JTextField.CENTER);

showDay[i].setEditable(false);

center.add(showDay[i]);

}

}

public void setCalendarMessage(CalendarMessage calendarMessage){

this.calendarMessage=calendarMessage;

}

public void showMonthCalendar(){

String [] a=calendarMessage.getMonthCalendar();

for(int i=0;i42;i++)

showDay[i].setText(a[i]);

validate();

}

}

用JAVA做日历,要求源代码

import java.util.Scanner; public class Wan{ public static void main(String[] args){ Scanner name = new Scanner(System.in); System.out.print("请输入要查询的年份:"); int year = name.nextInt(); System.out.print("请输入该年的月份"); int month = name.nextInt(); } //累加 该年至输入的月份 天数 //比如 输入2009年的 3月分 // 那就累加 2009年的1月至 3月1号的总天数 public void sumDay(int year,int month){ int day = 0; int sumDay = 0; for(int i = 1;i=month;i++){ switch(i){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: day = 31; break; case 2: if(year % 4 == 0 || year % 400 == 0 year %100!=0){ day = 29; }else{ day = 28; } break; default: day = 30; } //最后一个月份不要累加 因为我们只是要算到该月的一号就可以了 if(i month){ sumDay += day; } } //累加 2000年到该年的一月一号天数 for(int i = 2000;iyear;i++){ if( i % 4 == 0 || i %400== 0 i % 100 != 0){ sumDay += 366; }else{ sumDay += 365; } } //求该月一号为星期几 int week = sumDay % 7 +1; if(week == 7){ week = 0; } } public void fomatDate(int week,int day){ int g = 0; for(int i = 0;iweek;i++){ System.out.print("\t"); } for(int i = 1;i=day;i++){ System.out.print(i+"\t"); g = week + i; if(g % 7 == 0){ System.out.println(); } } } } 给点分哈 写得好累

Java编写程序,输入年份,输出本年度各月份日历

写了个简明的,

import java.util.Calendar;

import java.util.Scanner;

public class Test {

static public void main(String 参数[]){

Calendar c = Calendar.getInstance();

Scanner sc = new Scanner(System.in);

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

int year= sc.nextInt();

c.set(Calendar.YEAR, year);

c.set(Calendar.MONTH, Calendar.JANUARY);

c.set(Calendar.DAY_OF_MONTH, 1);

while(c.get(Calendar.YEAR)==year){

int wday=c.get(Calendar.DAY_OF_WEEK);

int mday=c.get(Calendar.DAY_OF_MONTH);

if(mday==1){

System.out.println("\n日\t一\t二\t三\t四\t五\t六\t第"+(c.get(Calendar.MONTH)+1)+"月");

System.out.println("---------------------------------------------------");

for(int i=0;iwday-1;i++) System.out.print(" \t");

}

System.out.print(mday+"\t");

if(wday==7) System.out.println();

c.add(Calendar.DAY_OF_YEAR, 1);

}

}

}

=======

请输入年份:

2012

日 一 二 三 四 五 六 第1月

---------------------------------------------------

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

日 一 二 三 四 五 六 第2月

---------------------------------------------------

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29

日 一 二 三 四 五 六 第3月

---------------------------------------------------

1 2 3

4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19 20 21 22 23 24

25 26 27 28 29 30 31

日 一 二 三 四 五 六 第4月

---------------------------------------------------

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30

日 一 二 三 四 五 六 第5月

---------------------------------------------------

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

日 一 二 三 四 五 六 第6月

---------------------------------------------------

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

日 一 二 三 四 五 六 第7月

---------------------------------------------------

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

日 一 二 三 四 五 六 第8月

---------------------------------------------------

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29 30 31

日 一 二 三 四 五 六 第9月

---------------------------------------------------

1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30

日 一 二 三 四 五 六 第10月

---------------------------------------------------

1 2 3 4 5 6

7 8 9 10 11 12 13

14 15 16 17 18 19 20

21 22 23 24 25 26 27

28 29 30 31

日 一 二 三 四 五 六 第11月

---------------------------------------------------

1 2 3

4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19 20 21 22 23 24

25 26 27 28 29 30

日 一 二 三 四 五 六 第12月

---------------------------------------------------

1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30 31

怎样用java编写日历

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.GridLayout;

import java.awt.SystemColor;

import java.awt.event.ActionEvent;

import java.awt.event.KeyEvent;

import java.awt.event.MouseEvent;

import java.util.Calendar;

import java.util.GregorianCalendar;

import java.util.Locale;

import java.util.Date;

import java.util.StringTokenizer;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.JToggleButton;

import javax.swing.SwingConstants;

import javax.swing.UIManager;

/**

* pTitle: Swing日历/p

* pDescription: 操作日期/p

* @author duxu2004

* @version 1.0.1

*/

class JCalendar extends JPanel{

//动态表示年月日

private int year=0;

private int month=0;

private int day=0;

//主面板

private JPanel Main = new JPanel();

//日面板

private JPanel jPanelDay = new JPanel();

//月面板

private JPanel jPanelMonth = new JPanel();

//年的输入位置

private JTextField Year = new JTextField();

//月的输入位置

private JTextField Month = new JTextField();

//减少月份

private JButton MonthDown = new JButton();

//增加月份

private JButton MonthUp = new JButton();

private JPanel jPanelButton = new JPanel();

//减少年份

private JButton YearDown = new JButton();

//增加年份

private JButton YearUp = new JButton();

//显示日期的位置

private JLabel Out = new JLabel();

//中国时区,以后可以从这里扩展可以设置时区的功能

private Locale l=Locale.CHINESE;

//主日历

private GregorianCalendar cal=new GregorianCalendar(l);

//星期面板

private JPanel weekPanel=new JPanel();

//天按钮组

private JToggleButton[] days=new JToggleButton[42];

//天面板

private JPanel Days = new JPanel();

//标示

private JLabel jLabel1 = new JLabel();

private JLabel jLabel2 = new JLabel();

private JLabel jLabel3 = new JLabel();

private JLabel jLabel4 = new JLabel();

private JLabel jLabel5 = new JLabel();

private JLabel jLabel6 = new JLabel();

private JLabel jLabel7 = new JLabel();

//当前选择的天数按钮

private JToggleButton cur=null;

//月份天数数组,用来取得当月有多少天

// 1 2 3 4 5 6 7 8 9 10 11 12

private int[] mm={31,28,31,30,31,30,31,31,30,31,30,31};

//空日期构造函数

public JCalendar() {

try {

jbInit();

}

catch(Exception e) {

e.printStackTrace();

}

}

//带日期设置的构造函数

public JCalendar(int year, int month, int day) {

cal.set(year, month, day);

try {

jbInit();

}

catch (Exception e) {

e.printStackTrace();

}

}

//带日历输入的构造函数

public JCalendar(GregorianCalendar calendar) {

cal=calendar;

try {

jbInit();

}

catch (Exception e) {

e.printStackTrace();

}

}

//带日期输入的构造函数

public JCalendar(Date date) {

cal.setTime(date);

try {

jbInit();

}

catch (Exception e) {

e.printStackTrace();

}

}

//初始化组件

private void jbInit() throws Exception {

//初始化年、月、日

iniCalender();

this.setLayout(new BorderLayout());

this.setBorder(BorderFactory.createRaisedBevelBorder());

this.setMaximumSize(new Dimension(200, 200));

this.setMinimumSize(new Dimension(200, 200));

this.setPreferredSize(new Dimension(200, 200));

Main.setLayout(new BorderLayout());

Main.setBackground(SystemColor.info);

Main.setBorder(null);

Out.setBackground(Color.lightGray);

Out.setHorizontalAlignment(SwingConstants.CENTER);

Out.setMaximumSize(new Dimension(100, 19));

Out.setMinimumSize(new Dimension(100, 19));

Out.setPreferredSize(new Dimension(100, 19));

jLabel1.setForeground(Color.red);

jLabel1.setHorizontalAlignment(SwingConstants.CENTER);

jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel1.setText("日");

jLabel2.setForeground(Color.blue);

jLabel2.setHorizontalAlignment(SwingConstants.CENTER);

jLabel2.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel2.setText("六");

jLabel3.setHorizontalAlignment(SwingConstants.CENTER);

jLabel3.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel3.setText("五");

jLabel4.setHorizontalAlignment(SwingConstants.CENTER);

jLabel4.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel4.setText("四");

jLabel5.setHorizontalAlignment(SwingConstants.CENTER);

jLabel5.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel5.setText("三");

jLabel6.setBorder(null);

jLabel6.setHorizontalAlignment(SwingConstants.CENTER);

jLabel6.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel6.setText("二");

jLabel7.setBackground(Color.lightGray);

jLabel7.setForeground(Color.black);

jLabel7.setBorder(null);

jLabel7.setHorizontalAlignment(SwingConstants.CENTER);

jLabel7.setHorizontalTextPosition(SwingConstants.CENTER);

jLabel7.setText("一");

weekPanel.setBackground(UIManager.getColor("InternalFrame.activeTitleGradient"));

weekPanel.setBorder(BorderFactory.createEtchedBorder());

weekPanel.setLayout(new GridLayout(1,7));

weekPanel.add(jLabel1, null);

weekPanel.add(jLabel7, null);

weekPanel.add(jLabel6, null);

weekPanel.add(jLabel5, null);

weekPanel.add(jLabel4, null);

weekPanel.add(jLabel3, null);

weekPanel.add(jLabel2, null);

MonthUp.setAlignmentX((float) 0.0);

MonthUp.setActionMap(null);

jPanelMonth.setBackground(SystemColor.info);

jPanelMonth.setLayout(new BorderLayout());

jPanelMonth.setBorder(BorderFactory.createEtchedBorder());

Month.setBorder(null);

Month.setHorizontalAlignment(SwingConstants.CENTER);

Month.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(MouseEvent e) {

Month_mouseClicked(e);

}

});

Month.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyPressed(KeyEvent e) {

Month_keyPressed(e);

}

});

MonthDown.setBorder(null);

MonthDown.setText("\u25C4");

MonthDown.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

MonthDown_actionPerformed(e);

}

});

MonthUp.setBorder(null);

MonthUp.setText("\u25BA");

MonthUp.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

MonthUp_actionPerformed(e);

}

});

jPanelButton.setLayout(null);

jPanelButton.setBorder(null);

jPanelButton.addComponentListener(new java.awt.event.ComponentAdapter() {

public void componentResized(java.awt.event.ComponentEvent evt) {

jPanelButtonComponentResized(evt);

}

});

Year.setBorder(BorderFactory.createEtchedBorder());

Year.setMaximumSize(new Dimension(80, 25));

Year.setMinimumSize(new Dimension(80, 25));

Year.setPreferredSize(new Dimension(80, 25));

Year.setHorizontalAlignment(SwingConstants.CENTER);

Year.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(MouseEvent e) {

Year_mouseClicked(e);

}

});

Year.addKeyListener(new java.awt.event.KeyAdapter() {

public void keyPressed(KeyEvent e) {

Year_keyPressed(e);

}

});

YearDown.setBorder(null);

YearDown.setMaximumSize(new Dimension(16, 16));

YearDown.setMinimumSize(new Dimension(16, 16));

YearDown.setPreferredSize(new Dimension(16, 16));

YearDown.setSize(new Dimension(16, 16));

YearDown.setText("▼");

YearDown.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

YearDown_actionPerformed(e);

}

});

YearUp.setBorder(null);

YearUp.setMaximumSize(new Dimension(16, 16));

YearUp.setMinimumSize(new Dimension(16, 16));

YearUp.setPreferredSize(new Dimension(16, 16));

YearUp.setSize(new Dimension(16, 16));

YearUp.setText("▲");

YearUp.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

YearUp_actionPerformed(e);

}

});

jPanelDay.setLayout(new BorderLayout());

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

Days.setBackground(SystemColor.info);

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

days[i]=new JToggleButton();

days[i].setBorder(null);

days[i].setBackground(SystemColor.info);

days[i].setHorizontalAlignment(SwingConstants.CENTER);

days[i].setHorizontalTextPosition(SwingConstants.CENTER);

//days[i].setSize(l,l);

days[i].addActionListener(new java.awt.event.ActionListener(){

public void actionPerformed(ActionEvent e) {

day=Integer.parseInt(((JToggleButton)e.getSource()).getText());

showDate();

showDays();

}

});

Days.add(days[i]);

}

this.add(Main, BorderLayout.NORTH);

this.add(jPanelDay, BorderLayout.CENTER);

this.add(jPanelMonth, BorderLayout.SOUTH);

Main.add(Year, BorderLayout.CENTER);

Main.add(Out, BorderLayout.WEST);

Main.add(jPanelButton, BorderLayout.EAST);

jPanelButton.add(YearUp);

jPanelButton.add(YearDown);

jPanelDay.add(weekPanel,BorderLayout.NORTH);

jPanelDay.add(Days, BorderLayout.CENTER);

jPanelMonth.add(Month, BorderLayout.CENTER);

jPanelMonth.add(MonthDown, BorderLayout.WEST);

jPanelMonth.add(MonthUp, BorderLayout.EAST);

showMonth();

showYear();

showDate();

showDays();

}

//自定义重画年选择面板

void jPanelButtonComponentResized(java.awt.event.ComponentEvent evt){

YearUp.setLocation(0,0);

YearDown.setLocation(0,YearUp.getHeight());

jPanelButton.setSize(YearUp.getWidth(),YearUp.getHeight()*2);

jPanelButton.setPreferredSize(new Dimension(YearUp.getWidth(),YearUp.getHeight()*2));

jPanelButton.updateUI();

}

//测试用

public static void main(String[] args){

JFrame f=new JFrame();

f.setContentPane(new JCalendar());

f.pack();

//f.setResizable(false);

f.show();

}

//增加年份

void YearUp_actionPerformed(ActionEvent e) {

year++;

showYear();

showDate();

showDays();

}

//减少年份

void YearDown_actionPerformed(ActionEvent e) {

year--;

showYear();

showDate();

showDays();

}

//减少月份

void MonthDown_actionPerformed(ActionEvent e) {

month--;

if(month0) {

month = 11;

year--;

showYear();

}

showMonth();

showDate();

showDays();

}

//增加月份

void MonthUp_actionPerformed(ActionEvent e) {

month++;

if(month==12) {

month=0;

year++;

showYear();

}

showMonth();

showDate();

showDays();

}

//初始化年月日

void iniCalender(){

year=cal.get(Calendar.YEAR);

month=cal.get(Calendar.MONTH);

day=cal.get(Calendar.DAY_OF_MONTH);

}

//刷新月份

void showMonth(){

Month.setText(Integer.toString(month+1)+"月");

}

//刷新年份

void showYear(){

Year.setText(Integer.toString(year)+"年");

}

//刷新日期

void showDate(){

Out.setText(Integer.toString(year)+"-"+Integer.toString(month+1)+"-"+Integer.toString(day));

}

//重画天数选择面板

void showDays() {

cal.set(year,month,1);

int firstDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

int n=mm[month];

if(cal.isLeapYear(year)month==1) n++;

int i=0;

for(;ifirstDayOfWeek-1;i++){

days[i].setEnabled(false);

days[i].setSelected(false);

days[i].setText("");

}

int d=1;

for(;d=n;d++){

days[i].setText(Integer.toString(d));

days[i].setEnabled(true);

if(d==day) days[i].setSelected(true);

else days[i].setSelected(false);;

i++;

}

for(;i42;i++){

days[i].setEnabled(false);

days[i].setSelected(false);

days[i].setText("");

}

}

//单击年份面板选择整个年份字符串

void SelectionYear(){

Year.setSelectionStart(0);

Year.setSelectionEnd(Year.getText().length());

}

//单击月份面板选择整个月份字符串

void SelectionMonth(){

Month.setSelectionStart(0);

Month.setSelectionEnd(Month.getText().length());

}

//月份面板响应鼠标单击事件

void Month_mouseClicked(MouseEvent e) {

//SelectionMonth();

inputMonth();

}

//检验输入的月份

void inputMonth(){

String s;

if(Month.getText().endsWith("月"))

{

s=Month.getText().substring(0,Month.getText().length()-1);

}

else s=Month.getText();

month=Integer.parseInt(s)-1;

this.showMe();

}

//月份面板键盘敲击事件响应

void Month_keyPressed(KeyEvent e) {

if(e.getKeyChar()==10)

inputMonth();

}

//年份面板响应鼠标单击事件

void Year_mouseClicked(MouseEvent e) {

//SelectionYear();

inputYear();

}

//年份键盘敲击事件响应

void Year_keyPressed(KeyEvent e) {

//System.out.print(new Integer(e.getKeyChar()).byteValue());

if(e.getKeyChar()==10)

inputYear();

}

//检验输入的年份字符串

void inputYear() {

String s;

if(Year.getText().endsWith("年"))

{

s=Year.getText().substring(0,Year.getText().length()-1);

}

else s=Year.getText();

year=Integer.parseInt(s);

this.showMe();

}

//以字符串形式返回日期,yyyy-mm-dd

public String getDate(){return Out.getText();}

//以字符串形式输入日期,yyyy-mm-dd

public void setDate(String date){

if(date!=null){

StringTokenizer f = new StringTokenizer(date, "-");

if(f.hasMoreTokens())

year = Integer.parseInt(f.nextToken());

if(f.hasMoreTokens())

month = Integer.parseInt(f.nextToken());

if(f.hasMoreTokens())

day = Integer.parseInt(f.nextToken());

cal.set(year,month,day);

}

this.showMe();

}

//以日期对象形式输入日期

public void setTime(Date date){

cal.setTime(date);

this.iniCalender();

this.showMe();

}

//返回日期对象

public Date getTime(){return cal.getTime();}

//返回当前的日

public int getDay() {

return day;

}

//设置当前的日

public void setDay(int day) {

this.day = day;

cal.set(this.year,this.month,this.day);

this.showMe();

}

//设置当前的年

public void setYear(int year) {

this.year = year;

cal.set(this.year,this.month,this.day);

this.showMe();

}

//返回当前的年

public int getYear() {

return year;

}

//返回当前的月

public int getMonth() {

return month;

}

//设置当前的月

public void setMonth(int month) {

this.month = month;

cal.set(this.year,this.month,this.day);

this.showMe();

}

//刷新

public void showMe(){

this.showDays();

this.showMonth();

this.showYear();

this.showDate();

}

}

public class TestJCalendar {

public static void main(String[] args) {

JFrame f=new JFrame();

f.setContentPane(new JCalendar());

f.pack();

//f.setResizable(false);

f.show();

}

}

如何用JAVA写日历?

按照你的要求编写的Java日历验证程序如下

UI.java

import java.util.Scanner;

public class UI {

static Scanner sc=new Scanner(System.in);

public static int askInt(String s){

System.out.print(s);

return sc.nextInt();

}

public static void println(String s){

System.out.println(s);

}

}

EE.java

public class EE {

public void validateDateCore(){

int year =UI.askInt("Enter the year: ");

int month=UI.askInt("Enter the month: ");

int day=UI.askInt("Enter the day: ");

if(year  1){

UI.println("The year is not a valid number.");

return;

}

if(month1 || month12){

UI.println("The month is not a valid number.");

return;

}

int monthDay=0;

switch(month){

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:monthDay=31;break;

case 4:

case 6:

case 9:

case 11:monthDay=30;break;

case 2:

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

monthDay=29;

}else{

monthDay=28;

}

break;

}

if(day1 || daymonthDay){

UI.println("The day is not a valid number.");

return;

}else{

UI.println("It is "+day+"/"+month+"/"+year+".");

}

}

public static void main(String[] args) {

new EE().validateDateCore();

}

}

运行结果


当前文章:java程序代码日历,java实现日历
文章分享:http://cdiso.cn/article/phspip.html

其他资讯