日历记事java代码 java编程实现日历记事本

怎么用java写日历

以下是两个类,请楼主分别存成两个java文件:

前锋网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。创新互联自2013年起到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。

其中

MainFrame.java是显示日历程序,Clock.java是日历计算程序。编译后运行MainFrame这个类即可。

1.MainFrame.java

---

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.GridLayout;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.Date;

import java.util.Calendar;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class MainFrame extends JFrame {

/** *//**

*

*/

private static final long serialVersionUID = 1L;

JPanel panel = new JPanel(new BorderLayout());

JPanel panel1 = new JPanel();

JPanel panel2 = new JPanel(new GridLayout(7, 7));

JPanel panel3 = new JPanel();

JLabel[] label = new JLabel[49];

JLabel y_label = new JLabel("年份");

JLabel m_label = new JLabel("月份");

JComboBox com1 = new JComboBox();

JComboBox com2 = new JComboBox();

int re_year, re_month;

int x_size, y_size;

String year_num;

Calendar now = Calendar.getInstance(); // 实例化Calendar

MainFrame() {

super("万年历");

setSize(300, 350);

x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());

y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());

setLocation((x_size - 300) / 2, (y_size - 350) / 2);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel1.add(y_label);

panel1.add(com1);

panel1.add(m_label);

panel1.add(com2);

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

label[i] = new JLabel("", JLabel.CENTER);// 将显示的字符设置为居中

panel2.add(label[i]);

}

panel3.add(new Clock(this));

panel.add(panel1, BorderLayout.NORTH);

panel.add(panel2, BorderLayout.CENTER);

panel.add(panel3, BorderLayout.SOUTH);

panel.setBackground(Color.white);

panel1.setBackground(Color.white);

panel2.setBackground(Color.white);

panel3.setBackground(Color.white);

Init();

com1.addActionListener(new ClockAction());

com2.addActionListener(new ClockAction());

setContentPane(panel);

setVisible(true);

setResizable(false);

}

class ClockAction implements ActionListener {

public void actionPerformed(ActionEvent arg0) {

int c_year, c_month, c_week;

c_year = Integer.parseInt(com1.getSelectedItem().toString()); // 得到当前所选年份

c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11

c_week = use(c_year, c_month); // 调用函数use,得到星期几

Resetday(c_week, c_year, c_month); // 调用函数Resetday

}

}

public void Init() {

int year, month_num, first_day_num;

String log[] = { "日", "一", "二", "三", "四", "五", "六" };

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

label[i].setText(log[i]);

}

for (int i = 0; i 49; i = i + 7) {

label[i].setForeground(Color.red); // 将星期日的日期设置为红色

}

for (int i = 6; i 49; i = i + 7) {

label[i].setForeground(Color.green);// 将星期六的日期设置为绿色

}

for (int i = 1; i 10000; i++) {

com1.addItem("" + i);

}

for (int i = 1; i 13; i++) {

com2.addItem("" + i);

}

month_num = (int) (now.get(Calendar.MONTH)); // 得到当前时间的月份

year = (int) (now.get(Calendar.YEAR)); // 得到当前时间的年份

com1.setSelectedIndex(year - 1); // 设置下拉列表显示为当前年

com2.setSelectedIndex(month_num); // 设置下拉列表显示为当前月

first_day_num = use(year, month_num);

Resetday(first_day_num, year, month_num);

}

public int use(int reyear, int remonth) {

int week_num;

now.set(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天

week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期

return week_num;

}

@SuppressWarnings("deprecation")

public void Resetday(int week_log, int year_log, int month_log) {

int month_day_score; // 存储月份的天数

int count;

month_day_score = 0;

count = 1;

Date date = new Date(year_log, month_log + 1, 1); // now

Calendar cal = Calendar.getInstance();

cal.setTime(date);

cal.add(Calendar.MONTH, -1); // 前个月

month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天

for (int i = 7; i 49; i++) { // 初始化标签

label[i].setText("");

}

week_log = week_log + 6; // 将星期数加6,使显示正确

month_day_score = month_day_score + week_log;

for (int i = week_log; i month_day_score; i++, count++) {

label[i].setText(count + "");

}

}

public static void main(String[] args) {

JFrame.setDefaultLookAndFeelDecorated(true);

new MainFrame();

}

}

2.Clock.java

-----

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.GridLayout;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.Date;

import java.util.Calendar;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

//显示时间的类:Clock

/** *//**

* Clock.java

* Summary 数字时间显示

* Created on

* @author

* remark

*/

import java.awt.Canvas;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.text.SimpleDateFormat;

import java.util.Calendar;

class Clock extends Canvas implements Runnable{

/** *//**

*

*/

private static final long serialVersionUID = 3660124045489727166L;

MainFrame mf;

Thread t;

String time;

public Clock(MainFrame mf){

this.mf=mf;

setSize(280,40);

setBackground(Color.white);

t=new Thread(this); //实例化线程

t.start(); //调用线程

}

public void run(){

while(true){

try{

Thread.sleep(1000); //休眠1秒钟

}catch(InterruptedException e){

System.out.println("异常");

}

this.repaint(100);

}

}

public void paint(Graphics g){

Font f=new Font("宋体",Font.BOLD,16);

SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化时间显示类型

Calendar now=Calendar.getInstance();

time=SDF.format(now.getTime()); //得到当前日期和时间

g.setFont(f);

g.setColor(Color.orange);

g.drawString(time,45,25);

}

}

java 日历的代码问题

首先,控制台输入肯定要是形如2013-03-06这样的格式,这是SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD")所要求的。输入2013-03会进入异常处理提示输入格式不正确。至于打印日历,你可以算出该月的第一号是星期几,然后按顺序排下去,满7天换行就是了

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

详细代码

//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代码 java编程实现日历记事本
分享地址:http://azwzsj.com/article/ddijpjd.html