也不知道你具體需求是什么,以前改過(guò)一個(gè)日歷程序,一共四個(gè)java類,放在同一個(gè)包里。經(jīng)測(cè)試可以運(yùn)行。
10年的齊河網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。網(wǎng)絡(luò)營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整齊河建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“齊河網(wǎng)站設(shè)計(jì)”,“齊河網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
//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 += "日\(chéng)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("現(xiàn)在的時(shí)間是:");
JLabel lblShowDate=new JLabel();
// javax.swing.JTextField trxt =new JTextField(10);
// trxt.setHorizontalAlignment(JTextField.RIGHT); //設(shè)置文本從右邊輸入
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("關(guān)閉");
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); //設(shè)置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("日期數(shù)據(jù)不合法.");
}
}
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:在原有基礎(chǔ)上修改了以下內(nèi)容:
* 1. 將容器由Frame改為了Dialog,以便在基于對(duì)話框的程序中也能夠使用
* 2. 將最小日期由1980改為了1950,考慮到目前球員的出生日期可能早于1980年
* 3. 將初始顯示格式設(shè)置為 yyyy年MM月dd日 格式,原有的小時(shí)去掉了,不適合于出生日期字段
*/
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());
}
// 覆蓋父類的方法使之無(wú)效
//public void addActionListener(ActionListener listener) {
//}
private class DateChooser extends JPanel implements ActionListener,
ChangeListener {
int startYear = 1950; // 默認(rèn)【最小】顯示年份
int lastYear = 2050; // 默認(rèn)【最大】顯示年份
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; // 控制條標(biāo)簽文字色
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("時(shí)");
hourLabel.setForeground(controlTextColor);
result.add(hourLabel);
return result;
}
private JPanel createWeekAndDayPanal() {
String colname[] = { "日", "一", "二", "三", "四", "五", "六" };
JPanel result = new JPanel();
// 設(shè)置固定字體,以免調(diào)用環(huán)境改變影響界面美觀
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, "日期時(shí)間選擇", 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());
}
}
}
這是一個(gè)專門(mén)的選日期的類 ,你看看完了調(diào)用就行了
import java.util.Scanner; public class Wan{ public static void main(String[] args){ Scanner name = new Scanner(System.in); System.out.print("請(qǐng)輸入要查詢的年份:"); int year = name.nextInt(); System.out.print("請(qǐng)輸入該年的月份"); int month = name.nextInt(); } //累加 該年至輸入的月份 天數(shù) //比如 輸入2009年的 3月分 // 那就累加 2009年的1月至 3月1號(hào)的總天數(shù) 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; } //最后一個(gè)月份不要累加 因?yàn)槲覀冎皇且愕皆撛碌囊惶?hào)就可以了 if(i month){ sumDay += day; } } //累加 2000年到該年的一月一號(hào)天數(shù) for(int i = 2000;iyear;i++){ if( i % 4 == 0 || i %400== 0 i % 100 != 0){ sumDay += 366; }else{ sumDay += 365; } } //求該月一號(hào)為星期幾 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(); } } } } 給點(diǎn)分哈 寫(xiě)得好累
package org.java.test;
import java.util.Scanner;
public class CalendarTest{
public static void main(String[] args) {
System.out.println("歡 迎 使 用 萬(wàn) 年 歷");
Scanner input = new Scanner(System.in);
System.out.print("\n請(qǐng)選擇年份: ");
int year = input.nextInt();
System.out.print("\n請(qǐng)選擇月份: ");
int month = input.nextInt();
System.out.println();
int days = 0; // 存儲(chǔ)當(dāng)月的天數(shù)
boolean isRn;
/* 判斷是否是閏年 */
if (year % 4 == 0 !(year % 100 == 0) || year % 400 == 0) { // 判斷是否為閏年
isRn = true; // 閏年
} else {
isRn = false;// 平年
}
/* 計(jì)算輸入的年份之前的天數(shù) */
int totalDays = 0;
for (int i = 1900; i year; i++) {
/* 判斷閏年或平年,并進(jìn)行天數(shù)累加 */
if (i % 4 == 0 !(i % 100 == 0) || i % 400 == 0) { // 判斷是否為閏年
totalDays = totalDays + 366; // 閏年366天
} else {
totalDays = totalDays + 365; // 平年365天
}
}
/* 計(jì)算輸入月份之前的天數(shù) */
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日的天數(shù)
/* 計(jì)算星期幾 */
int firstDayOfMonth; // 存儲(chǔ)當(dāng)月第一天是星期幾:星期日為0,星期一~星期六為1~6
int temp = 1 + totalDays % 7; // 從1900年1月1日推算
if (temp == 7) { // 求當(dāng)月第一天
firstDayOfMonth = 0; // 周日
} else {
firstDayOfMonth = temp;
}
/* 輸出日歷 */
System.out.println("星期日\(chéng)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) { // 如果當(dāng)天為周六,輸出換行
System.out.println();
}
}
}
}
這是你要的萬(wàn)年歷嗎?
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 負(fù)責(zé)改變?cè)?
Year 負(fù)責(zé)改變年;
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;
負(fù)責(zé)改變年=new Year(this);
負(fù)責(zé)改變年.setYear(year);
負(fù)責(zé)改變?cè)?new Month(this);
負(fù)責(zé)改變?cè)?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(負(fù)責(zé)改變年);
box.add(負(fù)責(zé)改變?cè)?;
leftNorth.add(box);
leftPanel.setLayout(new BorderLayout());
leftPanel.add(leftNorth,BorderLayout.NORTH);
leftPanel.add(leftCenter,BorderLayout.CENTER);
leftPanel.add(new Label("請(qǐng)?jiān)谀攴葺斎肟蜉斎胨槟攴?負(fù)數(shù)表示公元前),并回車確定"),
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);
設(shè)置日歷牌(year,month);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
setBounds(100,50,524,285);
validate();
}
public void 設(shè)置日歷牌(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)
{
排列號(hào)碼(星期幾,31);
}
else if(month==4||month==6||month==9||month==11)
{
排列號(hào)碼(星期幾,30);
}
else if(month==2)
{
if((year%4==0year%100!=0)||(year%400==0))
{
排列號(hào)碼(星期幾,29);
}
else
{
排列號(hào)碼(星期幾,28);
}
}
}
public void 排列號(hào)碼(int 星期幾,int 月天數(shù))
{
for(int i=星期幾,n=1;i星期幾+月天數(shù);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=星期幾+月天數(shù);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.設(shè)置信息條(year,month,day);
notepad.設(shè)置文本區(qū)(null);
notepad.獲取日志內(nèi)容(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);
日歷.設(shè)置日歷牌(日歷.getYear(),month);
}
else if(month==1)
{
month=12;
日歷.setMonth(month);
日歷.設(shè)置日歷牌(日歷.getYear(),month);
}
showMonth.setText(""+month);
}
else if(e.getSource()==下月)
{
if(month12)
{
month=month+1;
日歷.setMonth(month);
日歷.設(shè)置日歷牌(日歷.getYear(),month);
}
else if(month==12)
{
month=1;
日歷.setMonth(month);
日歷.設(shè)置日歷牌(日歷.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 設(shè)置信息條(int year,int month,int day)
{
信息條.setText(""+year+"年"+month+"月"+day+"日");
}
public void 設(shè)置文本區(qū)(String s)
{
text.setText(s);
}
public void 獲取日志內(nèi)容(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,"詢問(wèn)",JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
text.setText((String)table.get(key));
}
else
{
text.setText("");
}
}
else
{
text.setText("無(wú)記錄");
}
}
public void 保存日志(int year,int month,int day)
{
String 日志內(nèi)容=text.getText();
String key=""+year+""+month+""+day;
String m=""+year+"年"+month+"月"+day+"保存日志嗎?";
int ok=JOptionPane.showConfirmDialog(this,m,"詢問(wèn)",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,日志內(nèi)容);
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,"詢問(wèn)",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+"無(wú)日志記錄";
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);
日歷.設(shè)置日歷牌(year,日歷.getMonth());
}
else if(e.getSource()==明年)
{
year=year+1;
showYear.setText(""+year);
日歷.setYear(year);
日歷.設(shè)置日歷牌(year,日歷.getMonth());
}
else if(e.getSource()==showYear)
{
try
{
year=Integer.parseInt(showYear.getText());
showYear.setText(""+year);
日歷.setYear(year);
日歷.設(shè)置日歷牌(year,日歷.getMonth());
}
catch(NumberFormatException ee)
{
showYear.setText(""+year);
日歷.setYear(year);
日歷.設(shè)置日歷牌(year,日歷.getMonth());
}
}
}
}
希望能幫到你,以上分為4個(gè)類。。分割線以標(biāo)注了