求Java 日历的小程序的源代码
也不知道你具体需求是什么,以前改过一个日历程序,一共四个java类,放在同一个包里。经测试可以运行。
10年的齐河网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。网络营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整齐河建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“齐河网站设计”,“齐河网站推广”以来,每个客户项目都认真落实执行。
//Start.java
import java.awt.*;
import javax.swing.*;
class Start{
public static void main(String [] args){
DateFrame frame=new DateFrame();
frame.setLocationRelativeTo(frame);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
//DateInfo.java
import java.util.*;
public class DateInfo{
private int mYear, mMonth;
private int mDayOfMonth, mFristWeek;
public DateInfo(int year, int month) throws DateException{
mYear = year;
if (month 0 || month 12){
throw (new DateException());
}
mMonth = month;
mDayOfMonth = getDayOfMonth(mYear, mMonth);
mFristWeek = getFristWeek(mYear, mMonth);
}
private int getDayOfMonth(int year, int month){
int[][] ary = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
return (ary[isLeapYear(year)][month]);
}
private int isLeapYear(int year){
if (year % 4 == 0 year % 100 != 0 ||year % 400 == 0){
return (1);
}
else{
return (0);
}
}
private int getFristWeek(int year, int month){
java.util.Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, 1);
return (cal.get(Calendar.DAY_OF_WEEK) - 1);
}
public String toString(){
String str;
str = "\t\t" + mYear + "年" + mMonth + "月\n";
str += "日\t一\t二\t三\t四\t五\t六\n";
int i;
for (i = 1; i = mFristWeek; i++){
str += " \t";
}
for (int j = 1; j = mDayOfMonth; j++, i++){
str +=j+"\t" ;
if (i % 7 == 0){
str += "\n";
}
}
return (str);
}
}
//DateFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;
class DateFrame extends JFrame implements Runnable{
Calendar date=Calendar.getInstance();
String[] str={"1","2","3","4","5","6","7","8","9","10","11","12"};
JLabel lblYear=new JLabel("年 ");
JLabel lblMonth=new JLabel("月 ");
JLabel lblDate=new JLabel("现在的时间是:");
JLabel lblShowDate=new JLabel();
// javax.swing.JTextField trxt =new JTextField(10);
// trxt.setHorizontalAlignment(JTextField.RIGHT); //设置文本从右边输入
JComboBox cboMonth=new JComboBox(str);
JComboBox cboYear=new JComboBox();
JTextArea txaShow=new JTextArea();
JPanel pnlNorth=new JPanel();
JPanel pnlSOUTH=new JPanel();
JButton btnShow=new JButton("显示");
JButton btnClose=new JButton("关闭");
JScrollPane jsp=new JScrollPane(txaShow);
Container c=this.getContentPane();
public DateFrame(){
Thread thread=new Thread(this);
thread.start();
this.setTitle("玩玩日历拉!!!");
this.setSize(300,260);
for (int i = 1990; i=2025; i++) {
cboYear.addItem(""+i);
}
cboYear.setSelectedItem(""+(date.get(Calendar.YEAR)));
cboMonth.setSelectedItem(""+(date.get(Calendar.MONTH)+1));
pnlNorth.add(cboYear);
txaShow.setTabSize(4); //设置tab键的距离
txaShow.setForeground(Color.GREEN);
pnlNorth.add(lblYear);
pnlNorth.add(cboMonth);
pnlNorth.add(lblMonth);
pnlNorth.add(lblDate);
pnlNorth.add(lblShowDate);
c.add(pnlNorth,BorderLayout.NORTH);
c.add(jsp);
pnlSOUTH.add(btnShow);
pnlSOUTH.add(btnClose);
c.add(pnlSOUTH,BorderLayout.SOUTH);
btnShow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int year=Integer.parseInt((String)cboYear.getSelectedItem());
int month=Integer.parseInt((String)cboMonth.getSelectedItem());
try {
DateInfo date=new DateInfo(year,month);
txaShow.setText(""+date);
}
catch (DateException ex) {
ex.printStackTrace();
}
}
});
btnClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
}
public void run(){
try {
while(true){
Thread.sleep(1000);
int hour=date.get(Calendar.HOUR);
int minute=date.get(Calendar.MINUTE);
int second=date.get(Calendar.SECOND);
String str=hour+":"+minute+":"+second;
lblShowDate.setText(str);
//this.repaint();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
//DateException.java
public class DateException extends Exception{
public DateException(){
super("日期数据不合法.");
}
}
JAVA日历代码,怎么做?
import java.util.Date;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.border.LineBorder;
/**
* @company:NEUSOFT
* @Title:日期选择控件
* @Description:在原有基础上修改了以下内容:
* 1. 将容器由Frame改为了Dialog,以便在基于对话框的程序中也能够使用
* 2. 将最小日期由1980改为了1950,考虑到目前球员的出生日期可能早于1980年
* 3. 将初始显示格式设置为 yyyy年MM月dd日 格式,原有的小时去掉了,不适合于出生日期字段
*/
public class DateChooserJButton extends JButton {
private DateChooser dateChooser = null;
private String preLabel = "";
public DateChooserJButton() {
this(getNowDate());
}
public DateChooserJButton(SimpleDateFormat df, String dateString) {
this();
setText(df, dateString);
}
public DateChooserJButton(Date date) {
this("", date);
}
public DateChooserJButton(String preLabel, Date date) {
if (preLabel != null)
this.preLabel = preLabel;
setDate(date);
setBorder(null);
setCursor(new Cursor(Cursor.HAND_CURSOR));
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (dateChooser == null)
dateChooser = new DateChooser();
Point p = getLocationOnScreen();
p.y = p.y + 30;
dateChooser.showDateChooser(p);
}
});
}
private static Date getNowDate() {
return Calendar.getInstance().getTime();
}
private static SimpleDateFormat getDefaultDateFormat() {
return new SimpleDateFormat("yyyy年MM月dd日");
}
// 覆盖父类的方法
public void setText(String s) {
Date date;
try {
date = getDefaultDateFormat().parse(s);
} catch (ParseException e) {
date = getNowDate();
}
setDate(date);
}
public void setText(SimpleDateFormat df, String s) {
Date date;
try {
date = df.parse(s);
} catch (ParseException e) {
date = getNowDate();
}
setDate(date);
}
public void setDate(Date date) {
super.setText(preLabel + getDefaultDateFormat().format(date));
}
public Date getDate() {
String dateString = this.getText().substring(preLabel.length());
try {
return getDefaultDateFormat().parse(dateString);
} catch (ParseException e) {
return getNowDate();
}
}
public String getDateString()
{
Date birth =getDate();
DateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
return formatDate.format(birth).toString();
//return this.getText().substring(preLabel.length());
}
// 覆盖父类的方法使之无效
//public void addActionListener(ActionListener listener) {
//}
private class DateChooser extends JPanel implements ActionListener,
ChangeListener {
int startYear = 1950; // 默认【最小】显示年份
int lastYear = 2050; // 默认【最大】显示年份
int width = 200; // 界面宽度
int height = 200; // 界面高度
Color backGroundColor = Color.gray; // 底色
// 月历表格配色----------------//
Color palletTableColor = Color.white; // 日历表底色
Color todayBackColor = Color.orange; // 今天背景色
Color weekFontColor = Color.blue; // 星期文字色
Color dateFontColor = Color.black; // 日期文字色
Color weekendFontColor = Color.red; // 周末文字色
// 控制条配色------------------//
Color controlLineColor = Color.pink; // 控制条底色
Color controlTextColor = Color.white; // 控制条标签文字色
Color rbFontColor = Color.white; // RoundBox文字色
Color rbBorderColor = Color.red; // RoundBox边框色
Color rbButtonColor = Color.pink; // RoundBox按钮色
Color rbBtFontColor = Color.red; // RoundBox按钮文字色
JDialog dialog;
JSpinner yearSpin;
JSpinner monthSpin;
JSpinner hourSpin;
JButton[][] daysButton = new JButton[6][7];
DateChooser() {
setLayout(new BorderLayout());
setBorder(new LineBorder(backGroundColor, 2));
setBackground(backGroundColor);
JPanel topYearAndMonth = createYearAndMonthPanal();
add(topYearAndMonth, BorderLayout.NORTH);
JPanel centerWeekAndDay = createWeekAndDayPanal();
add(centerWeekAndDay, BorderLayout.CENTER);
}
private JPanel createYearAndMonthPanal() {
Calendar c = getCalendar();
int currentYear = c.get(Calendar.YEAR);
int currentMonth = c.get(Calendar.MONTH) + 1;
int currentHour = c.get(Calendar.HOUR_OF_DAY);
JPanel result = new JPanel();
result.setLayout(new FlowLayout());
result.setBackground(controlLineColor);
yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,
startYear, lastYear, 1));
yearSpin.setPreferredSize(new Dimension(48, 20));
yearSpin.setName("Year");
yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));
yearSpin.addChangeListener(this);
result.add(yearSpin);
JLabel yearLabel = new JLabel("年");
yearLabel.setForeground(controlTextColor);
result.add(yearLabel);
monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,
12, 1));
monthSpin.setPreferredSize(new Dimension(35, 20));
monthSpin.setName("Month");
monthSpin.addChangeListener(this);
result.add(monthSpin);
JLabel monthLabel = new JLabel("月");
monthLabel.setForeground(controlTextColor);
result.add(monthLabel);
hourSpin = new JSpinner(new SpinnerNumberModel(currentHour, 0, 23,
1));
hourSpin.setPreferredSize(new Dimension(35, 20));
hourSpin.setName("Hour");
hourSpin.addChangeListener(this);
result.add(hourSpin);
JLabel hourLabel = new JLabel("时");
hourLabel.setForeground(controlTextColor);
result.add(hourLabel);
return result;
}
private JPanel createWeekAndDayPanal() {
String colname[] = { "日", "一", "二", "三", "四", "五", "六" };
JPanel result = new JPanel();
// 设置固定字体,以免调用环境改变影响界面美观
result.setFont(new Font("宋体", Font.PLAIN, 12));
result.setLayout(new GridLayout(7, 7));
result.setBackground(Color.white);
JLabel cell;
for (int i = 0; i 7; i++) {
cell = new JLabel(colname[i]);
cell.setHorizontalAlignment(JLabel.RIGHT);
if (i == 0 || i == 6)
cell.setForeground(weekendFontColor);
else
cell.setForeground(weekFontColor);
result.add(cell);
}
int actionCommandId = 0;
for (int i = 0; i 6; i++)
for (int j = 0; j 7; j++) {
JButton numberButton = new JButton();
numberButton.setBorder(null);
numberButton.setHorizontalAlignment(SwingConstants.RIGHT);
numberButton.setActionCommand(String
.valueOf(actionCommandId));
numberButton.addActionListener(this);
numberButton.setBackground(palletTableColor);
numberButton.setForeground(dateFontColor);
if (j == 0 || j == 6)
numberButton.setForeground(weekendFontColor);
else
numberButton.setForeground(dateFontColor);
daysButton[i][j] = numberButton;
result.add(numberButton);
actionCommandId++;
}
return result;
}
private JDialog createDialog(JDialog owner) {
JDialog result = new JDialog(owner, "日期时间选择", true);
result.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
result.getContentPane().add(this, BorderLayout.CENTER);
result.pack();
result.setSize(width, height);
return result;
}
void showDateChooser(Point position) {
JDialog owner = (JDialog) SwingUtilities
.getWindowAncestor(DateChooserJButton.this);
if (dialog == null || dialog.getOwner() != owner)
dialog = createDialog(owner);
dialog.setLocation(getAppropriateLocation(owner, position));
flushWeekAndDay();
dialog.setVisible(true);
}
Point getAppropriateLocation(JDialog owner, Point position) {
Point result = new Point(position);
Point p = owner.getLocation();
int offsetX = (position.x + width) - (p.x + owner.getWidth());
int offsetY = (position.y + height) - (p.y + owner.getHeight());
if (offsetX 0) {
result.x -= offsetX;
}
if (offsetY 0) {
result.y -= offsetY;
}
return result;
}
private Calendar getCalendar() {
Calendar result = Calendar.getInstance();
result.setTime(getDate());
return result;
}
private int getSelectedYear() {
return ((Integer) yearSpin.getValue()).intValue();
}
private int getSelectedMonth() {
return ((Integer) monthSpin.getValue()).intValue();
}
private int getSelectedHour() {
return ((Integer) hourSpin.getValue()).intValue();
}
private void dayColorUpdate(boolean isOldDay) {
Calendar c = getCalendar();
int day = c.get(Calendar.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH, 1);
int actionCommandId = day - 2 + c.get(Calendar.DAY_OF_WEEK);
int i = actionCommandId / 7;
int j = actionCommandId % 7;
if (isOldDay)
daysButton[i][j].setForeground(dateFontColor);
else
daysButton[i][j].setForeground(todayBackColor);
}
private void flushWeekAndDay() {
Calendar c = getCalendar();
c.set(Calendar.DAY_OF_MONTH, 1);
int maxDayNo = c.getActualMaximum(Calendar.DAY_OF_MONTH);
int dayNo = 2 - c.get(Calendar.DAY_OF_WEEK);
for (int i = 0; i 6; i++) {
for (int j = 0; j 7; j++) {
String s = "";
if (dayNo = 1 dayNo = maxDayNo)
s = String.valueOf(dayNo);
daysButton[i][j].setText(s);
dayNo++;
}
}
dayColorUpdate(false);
}
public void stateChanged(ChangeEvent e) {
JSpinner source = (JSpinner) e.getSource();
Calendar c = getCalendar();
if (source.getName().equals("Hour")) {
c.set(Calendar.HOUR_OF_DAY, getSelectedHour());
setDate(c.getTime());
return;
}
dayColorUpdate(true);
if (source.getName().equals("Year"))
c.set(Calendar.YEAR, getSelectedYear());
else
// (source.getName().equals("Month"))
c.set(Calendar.MONTH, getSelectedMonth() - 1);
setDate(c.getTime());
flushWeekAndDay();
}
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
if (source.getText().length() == 0)
return;
dayColorUpdate(true);
source.setForeground(todayBackColor);
int newDay = Integer.parseInt(source.getText());
Calendar c = getCalendar();
c.set(Calendar.DAY_OF_MONTH, newDay);
setDate(c.getTime());
}
}
}
这是一个专门的选日期的类 ,你看看完了调用就行了
用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万年历源代码是多少?
package org.java.test;
import java.util.Scanner;
public class CalendarTest{
public static void main(String[] args) {
System.out.println("欢 迎 使 用 万 年 历");
Scanner input = new Scanner(System.in);
System.out.print("\n请选择年份: ");
int year = input.nextInt();
System.out.print("\n请选择月份: ");
int month = input.nextInt();
System.out.println();
int days = 0; // 存储当月的天数
boolean isRn;
/* 判断是否是闰年 */
if (year % 4 == 0 !(year % 100 == 0) || year % 400 == 0) { // 判断是否为闰年
isRn = true; // 闰年
} else {
isRn = false;// 平年
}
/* 计算输入的年份之前的天数 */
int totalDays = 0;
for (int i = 1900; i year; i++) {
/* 判断闰年或平年,并进行天数累加 */
if (i % 4 == 0 !(i % 100 == 0) || i % 400 == 0) { // 判断是否为闰年
totalDays = totalDays + 366; // 闰年366天
} else {
totalDays = totalDays + 365; // 平年365天
}
}
/* 计算输入月份之前的天数 */
int beforeDays = 0;
for (int i = 1; i = month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 2:
if (isRn) {
days = 29;
} else {
days = 28;
}
break;
default:
days = 30;
break;
}
if (i month) {
beforeDays = beforeDays + days;
}
}
totalDays = totalDays + beforeDays; // 距离1900年1月1日的天数
/* 计算星期几 */
int firstDayOfMonth; // 存储当月第一天是星期几:星期日为0,星期一~星期六为1~6
int temp = 1 + totalDays % 7; // 从1900年1月1日推算
if (temp == 7) { // 求当月第一天
firstDayOfMonth = 0; // 周日
} else {
firstDayOfMonth = temp;
}
/* 输出日历 */
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
for (int nullNo = 0; nullNo firstDayOfMonth; nullNo++) {
System.out.print("\t"); // 输出空格
}
for (int i = 1; i = days; i++) {
System.out.print(i + "\t");
if ((totalDays + i-1) % 7 == 5) { // 如果当天为周六,输出换行
System.out.println();
}
}
}
}
这是你要的万年历吗?
急需日历记事本JAVA源代码
import java.util.Calendar;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Hashtable;
public class CalendarPad extends JFrame implements MouseListener
{
int year,month,day;
Hashtable hashtable;
File file;
JTextField showDay[];
JLabel title[];
Calendar 日历;
int 星期几;
NotePad notepad=null;
Month 负责改变月;
Year 负责改变年;
String 星期[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
JPanel leftPanel,rightPanel;
public CalendarPad(int year,int month,int day)
{
leftPanel=new JPanel();
JPanel leftCenter=new JPanel();
JPanel leftNorth=new JPanel();
leftCenter.setLayout(new GridLayout(7,7));
rightPanel=new JPanel();
this.year=year;
this.month=month;
this.day=day;
负责改变年=new Year(this);
负责改变年.setYear(year);
负责改变月=new Month(this);
负责改变月.setMonth(month);
title=new JLabel[7];
showDay=new JTextField[42];
for(int j=0;j7;j++)
{
title[j]=new JLabel();
title[j].setText(星期[j]);
title[j].setBorder(BorderFactory.createRaisedBevelBorder());
leftCenter.add(title[j]);
}
title[0].setForeground(Color.red);
title[6].setForeground(Color.blue);
for(int i=0;i42;i++)
{
showDay[i]=new JTextField();
showDay[i].addMouseListener(this);
showDay[i].setEditable(false);
leftCenter.add(showDay[i]);
}
日历=Calendar.getInstance();
Box box=Box.createHorizontalBox();
box.add(负责改变年);
box.add(负责改变月);
leftNorth.add(box);
leftPanel.setLayout(new BorderLayout());
leftPanel.add(leftNorth,BorderLayout.NORTH);
leftPanel.add(leftCenter,BorderLayout.CENTER);
leftPanel.add(new Label("请在年份输入框输入所查年份(负数表示公元前),并回车确定"),
BorderLayout.SOUTH) ;
leftPanel.validate();
Container con=getContentPane();
JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
leftPanel,rightPanel);
con.add(split,BorderLayout.CENTER);
con.validate();
hashtable=new Hashtable();
file=new File("日历记事本.txt");
if(!file.exists())
{
try{
FileOutputStream out=new FileOutputStream(file);
ObjectOutputStream objectOut=new ObjectOutputStream(out);
objectOut.writeObject(hashtable);
objectOut.close();
out.close();
}
catch(IOException e)
{
}
}
notepad=new NotePad(this);
rightPanel.add(notepad);
设置日历牌(year,month);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
setBounds(100,50,524,285);
validate();
}
public void 设置日历牌(int year,int month)
{
日历.set(year,month-1,1);
星期几=日历.get(Calendar.DAY_OF_WEEK)-1;
if(month==1||month==2||month==3||month==5||month==7
||month==8||month==10||month==12)
{
排列号码(星期几,31);
}
else if(month==4||month==6||month==9||month==11)
{
排列号码(星期几,30);
}
else if(month==2)
{
if((year%4==0year%100!=0)||(year%400==0))
{
排列号码(星期几,29);
}
else
{
排列号码(星期几,28);
}
}
}
public void 排列号码(int 星期几,int 月天数)
{
for(int i=星期几,n=1;i星期几+月天数;i++)
{
showDay[i].setText(""+n);
if(n==day)
{
showDay[i].setForeground(Color.green);
showDay[i].setFont(new Font("TimesRoman",Font.BOLD,20));
}
else
{
showDay[i].setFont(new Font("TimesRoman",Font.BOLD,12));
showDay[i].setForeground(Color.black);
}
if(i%7==6)
{
showDay[i].setForeground(Color.blue);
}
if(i%7==0)
{
showDay[i].setForeground(Color.red);
}
n++;
}
for(int i=0;i星期几;i++)
{
showDay[i].setText("");
}
for(int i=星期几+月天数;i42;i++)
{
showDay[i].setText("");
}
}
public int getYear()
{
return year;
}
public void setYear(int y)
{
year=y;
notepad.setYear(year);
}
public int getMonth()
{
return month;
}
public void setMonth(int m)
{
month=m;
notepad.setMonth(month);
}
public int getDay()
{
return day;
}
public void setDay(int d)
{
day=d;
notepad.setDay(day);
}
public Hashtable getHashtable()
{
return hashtable;
}
public File getFile()
{
return file;
}
public void mousePressed(MouseEvent e)
{
JTextField source=(JTextField)e.getSource();
try{
day=Integer.parseInt(source.getText());
notepad.setDay(day);
notepad.设置信息条(year,month,day);
notepad.设置文本区(null);
notepad.获取日志内容(year,month,day);
}
catch(Exception ee)
{
}
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public static void main(String args[])
{
Calendar calendar=Calendar.getInstance();
int y=calendar.get(Calendar.YEAR);
int m=calendar.get(Calendar.MONTH)+1;
int d=calendar.get(Calendar.DAY_OF_MONTH);
new CalendarPad(y,m,d);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Month extends Box implements ActionListener
{
int month;
JTextField showMonth=null;
JButton 下月,上月;
CalendarPad 日历;
public Month(CalendarPad 日历)
{
super(BoxLayout.X_AXIS);
this.日历=日历;
showMonth=new JTextField(2);
month=日历.getMonth();
showMonth.setEditable(false);
showMonth.setForeground(Color.blue);
showMonth.setFont(new Font("TimesRomn",Font.BOLD,16));
下月=new JButton("下月");
上月=new JButton("上月");
add(上月);
add(showMonth);
add(下月);
上月.addActionListener(this);
下月.addActionListener(this);
showMonth.setText(""+month);
}
public void setMonth(int month)
{
if(month=12month=1)
{
this.month=month;
}
else
{
this.month=1;
}
showMonth.setText(""+month);
}
public int getMonth()
{
return month;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==上月)
{
if(month=2)
{
month=month-1;
日历.setMonth(month);
日历.设置日历牌(日历.getYear(),month);
}
else if(month==1)
{
month=12;
日历.setMonth(month);
日历.设置日历牌(日历.getYear(),month);
}
showMonth.setText(""+month);
}
else if(e.getSource()==下月)
{
if(month12)
{
month=month+1;
日历.setMonth(month);
日历.设置日历牌(日历.getYear(),month);
}
else if(month==12)
{
month=1;
日历.setMonth(month);
日历.设置日历牌(日历.getYear(),month);
}
showMonth.setText(""+month);
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class NotePad extends JPanel implements ActionListener
{
JTextArea text;
JButton 保存日志,删除日志;
Hashtable table;
JLabel 信息条;
int year,month,day;
File file;
CalendarPad calendar;
public NotePad(CalendarPad calendar)
{
this.calendar=calendar;
year=calendar.getYear();
month=calendar.getMonth();
day=calendar.getDay();;
table=calendar.getHashtable();
file=calendar.getFile();
信息条=new JLabel(""+year+"年"+month+"月"+day+"日",JLabel.CENTER);
信息条.setFont(new Font("TimesRoman",Font.BOLD,16));
信息条.setForeground(Color.blue);
text=new JTextArea(10,10);
保存日志=new JButton("保存日志") ;
删除日志=new JButton("删除日志") ;
保存日志.addActionListener(this);
删除日志.addActionListener(this);
setLayout(new BorderLayout());
JPanel pSouth=new JPanel();
add(信息条,BorderLayout.NORTH);
pSouth.add(保存日志);
pSouth.add(删除日志);
add(pSouth,BorderLayout.SOUTH);
add(new JScrollPane(text),BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==保存日志)
{
保存日志(year,month,day);
}
else if(e.getSource()==删除日志)
{
删除日志(year,month,day);
}
}
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;
}
public void setDay(int day)
{
this.day=day;
}
public int getDay()
{
return day;
}
public void 设置信息条(int year,int month,int day)
{
信息条.setText(""+year+"年"+month+"月"+day+"日");
}
public void 设置文本区(String s)
{
text.setText(s);
}
public void 获取日志内容(int year,int month,int day)
{
String key=""+year+""+month+""+day;
try
{
FileInputStream inOne=new FileInputStream(file);
ObjectInputStream inTwo=new ObjectInputStream(inOne);
table=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exception ee)
{
}
if(table.containsKey(key))
{
String m=""+year+"年"+month+"月"+day+"这一天有日志记载,想看吗?";
int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
text.setText((String)table.get(key));
}
else
{
text.setText("");
}
}
else
{
text.setText("无记录");
}
}
public void 保存日志(int year,int month,int day)
{
String 日志内容=text.getText();
String key=""+year+""+month+""+day;
String m=""+year+"年"+month+"月"+day+"保存日志吗?";
int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
try
{
FileInputStream inOne=new FileInputStream(file);
ObjectInputStream inTwo=new ObjectInputStream(inOne);
table=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
table.put(key,日志内容);
FileOutputStream out=new FileOutputStream(file);
ObjectOutputStream objectOut=new ObjectOutputStream(out);
objectOut.writeObject(table);
objectOut.close();
out.close();
}
catch(Exception ee)
{
}
}
}
public void 删除日志(int year,int month,int day)
{
String key=""+year+""+month+""+day;
if(table.containsKey(key))
{
String m="删除"+year+"年"+month+"月"+day+"日的日志吗?";
int ok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
try
{
FileInputStream inOne=new FileInputStream(file);
ObjectInputStream inTwo=new ObjectInputStream(inOne);
table=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
table.remove(key);
FileOutputStream out=new FileOutputStream(file);
ObjectOutputStream objectOut=new ObjectOutputStream(out);
objectOut.writeObject(table);
objectOut.close();
out.close();
text.setText(null);
}
catch(Exception ee)
{
}
}
}
else
{
String m=""+year+"年"+month+"月"+day+"无日志记录";
JOptionPane.showMessageDialog(this,m,"提示",JOptionPane.WARNING_MESSAGE);
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Year extends Box implements ActionListener
{
int year;
JTextField showYear=null;
JButton 明年,去年;
CalendarPad 日历;
public Year(CalendarPad 日历)
{
super(BoxLayout.X_AXIS);
showYear=new JTextField(4);
showYear.setForeground(Color.blue);
showYear.setFont(new Font("TimesRomn",Font.BOLD,14));
this.日历=日历;
year=日历.getYear();
明年=new JButton("下年");
去年=new JButton("上年");
add(去年);
add(showYear);
add(明年);
showYear.addActionListener(this);
去年.addActionListener(this);
明年.addActionListener(this);
}
public void setYear(int year)
{
this.year=year;
showYear.setText(""+year);
}
public int getYear()
{
return year;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==去年)
{
year=year-1;
showYear.setText(""+year);
日历.setYear(year);
日历.设置日历牌(year,日历.getMonth());
}
else if(e.getSource()==明年)
{
year=year+1;
showYear.setText(""+year);
日历.setYear(year);
日历.设置日历牌(year,日历.getMonth());
}
else if(e.getSource()==showYear)
{
try
{
year=Integer.parseInt(showYear.getText());
showYear.setText(""+year);
日历.setYear(year);
日历.设置日历牌(year,日历.getMonth());
}
catch(NumberFormatException ee)
{
showYear.setText(""+year);
日历.setYear(year);
日历.设置日历牌(year,日历.getMonth());
}
}
}
}
希望能帮到你,以上分为4个类。。分割线以标注了
分享文章:java漂亮日历源代码 日历编程java代码
分享网址:http://scyingshan.cn/article/dodehji.html