導航:首頁 > 編程語言 > java顯示日歷

java顯示日歷

發布時間:2023-10-22 06:18:58

① 用java語言編寫一個萬年歷程序,要求只顯示陽歷、節日、還有農歷年、閏年

/**
* @(#)AidyCalender.java
*
*
* @author
* @version 1.00 2008/7/19
*/
import java.awt.*;
import java.awt.event.*;
import java.lang.StringBuffer;
import javax.swing.*;
import java.util.*;
import javax.swing.Timer;
import javax.swing.border.*;

public class AidyCalender extends JFrame implements ActionListener,ItemListener{

Date date = new Date();
private GregorianCalendar gregorianCalendar = new GregorianCalendar();
//定義中英文字元數組存儲星期信息,用於轉換顯示
private String[] stringWeekEn = new String[] { "SUN", "MON", "TUE", "WED",
"THU", "FRI", "SAT" };
private String[] stringWeekCn = new String[] { "星期日", "星期一", "星期二", "星期三",
"星期四", "星期五", "星期六" };
//定義存儲月份的信息數組,用於轉換顯示方示
private String[] stringMonthEn = new String[] { "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };
private String[] stringMonthCn = {"一月","二月","三月","四月","五月","六月",
"七月","八月","九月","十月","十一月","十二月"};

private String[] sysNowTime = new String[6];//sysNowTime 用於存儲系統時間的變數
private String[] sysRunTime = new String[6];
private JLabel []labelWeek = new JLabel[7];
private JLabel []labelDay = new JLabel[42];
private JLabel labelTime = new JLabel();

private JPanel panel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JPanel panel3 = new JPanel();
private JComboBox combo1 = new JComboBox();
private JComboBox combo2 = new JComboBox();
private JButton buttonToday = new JButton();

private Border border = BorderFactory.createRaisedBevelBorder();
private Border border1 = BorderFactory.createLineBorder(Color.cyan,3);

public AidyCalender(String title) {
super(title);

for (int y = 1900; y < 2101; y++) {
combo1.addItem(" " + new Integer(y).toString()+"年");
}
for (int m = 0;m<12;m++){
combo2.addItem(" "+stringMonthCn[m]);
}
buttonToday.setText("今 天");
setLayout(new FlowLayout());
add(panel1);
add(panel2);
add(panel3);
panel1.setLayout(new GridLayout(1,3,10,0));
panel1.add(combo1);
combo1.addItemListener(this);
panel1.add(combo2);
combo2.addItemListener(this);
panel1.add(buttonToday);
buttonToday.addActionListener(this);
labelTime.setFont(new Font("宋體",Font.PLAIN,16));
labelTime.setForeground(Color.MAGENTA);
panel1.add(labelTime);
Timer time = new Timer(1000,new TimerListener());
time.addActionListener(new TimerListener());
//time.setRepeats(true);
time.start();

//labelTime.addAncestorListener(new TimerListener());

panel2.setLayout(new GridLayout(7,7,0,10));
panel2.setBackground(Color.white);

for(int i=0;i<7;i++){
labelWeek[i] = new JLabel();
labelWeek[i].setHorizontalAlignment(0);
if(i==0||i==6){
labelWeek[i].setBackground(Color.blue);
labelWeek[i].setForeground(Color.RED);
labelWeek[i].setFont(new Font("黑體",Font.BOLD,14));
}

else{
labelWeek[i].setForeground(Color.BLACK);
labelWeek[i].setFont(new Font("新宋體",Font.PLAIN,14));
}

labelWeek[i].setText(stringWeekCn[i]);
panel2.add(labelWeek[i]);

}
for(int i= 0;i<42;i++){
labelDay[i] = new JLabel();
labelDay[i].setHorizontalAlignment(0);
labelDay[i].setText("");
panel2.add(labelDay[i]);
}

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

setSize(300,300);
setBounds(250, 200, 400, 360);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

getSysDate();
setNowDate();
}

public void actionPerformed(ActionEvent ae){
if(ae.getSource()==buttonToday){
setNowDate();
}

}
public void itemStateChanged(ItemEvent aa){
setChangeDate();

}

public int turnWeek(String week){
int i;
for(i=0;i<7;i++)
if(week.equalsIgnoreCase(stringWeekEn[i]))
break;
return i;
}

public int turnMonth(String month){
/**
*int turnMonth(String month)
*@month 系統日期中的月,諸如Jan\Feb
*@return int
*返回一個整數值,用於尋找stringMonthCn[]數組中對應的中文月份
*/
int i;
for(i=0;i<12;i++)
if(month.equalsIgnoreCase(stringMonthEn[i]))
break;
return i;
}
/**
*setNowDate()
*設置當前系統日期
*/
public void setNowDate(){

setSysDate(getNowYear(),getNowMonth());
getSysRunDate();
setDateNull();
combo1.setSelectedIndex(getShowYear() - 1900);
combo2.setSelectedIndex(getShowMonth());
setDays(getMonthDays(getNowYear(),getNowMonth()),getInitWeek(sysRunTime[0]),getNowDay());
//labelTime.setText(sysNowTime[3]);
//labelTime.setHorizontalAlignment(0);
}

/**Integer getShowYear()
*獲取組合框中應該顯示的年份
*/

public void setSysDate(int year,int month){
gregorianCalendar.set(year,month,1);
}
public void setDateNull(){
for(int i=0;i<42;i++){
labelDay[i].setText("");
}
}
public void setChangeDate(){
setSysDate(getComboYear(),getComboMonth());
getSysRunDate();
setDateNull();
setDays(getMonthDays(getComboYear(),getComboMonth()),getInitWeek(sysRunTime[0]),-1);

}
public int getMonthDays(int year, int month) {
/**
*返回所選年月的天數,因為數組中的數值從0開始,所以3\5\8\10分別代表4\6\9\11幾個小月.
*而1代表2月,經過是否為閏年判斷,選擇返回28或29天.
*其餘月份為大月,返回31天.
**/
switch (month) {
case 3:
case 5:
case 8:
case 10:
return 30;//小月返回30天
case 1:
if (gregorianCalendar.isLeapYear(year)) {
//isLeapYear(year)確定當前紀元中的指定年份是否為閏年。
return 29;
} else {
return 28;
}//閏年的二月返回29天,平年返回28天
default:
return 31;
//大月返回31天
}
}
/**
*int getComboYear()
*獲取組合框中的年份
*/
public void getSysDate(){
date = gregorianCalendar.getTime();
sysNowTime = (date.toString()).split(" ");
}
public void getSysRunDate(){
date = gregorianCalendar.getTime();
sysRunTime = (date.toString()).split(" ");
}
public int getComboYear(){
return combo1.getSelectedIndex()+1900;
}

/**
*int getComboMonth()
*獲取月組合框中的整數值,
*/
public int getComboMonth(){
return combo2.getSelectedIndex();
}

public int getInitWeek(String initWeek){
/**
*getWeekNow(String initWeek)
*@para nowWeek 系統日期中的星期
*返回當月中的1號是從星期幾開始
*/
int nowWeek = 0 ;
for(int i = 0;i<7;i++){
if(initWeek.equalsIgnoreCase(stringWeekEn[i])){

nowWeek = i;
break;
}
}
return nowWeek;
}
public int getNowYear(){
return Integer.parseInt(sysNowTime[5]);
}
public int getNowMonth(){
int nowMonth=0;
for(int i=0;i<12;i++){
if(sysNowTime[1].equalsIgnoreCase(stringMonthEn[i]));
nowMonth=i;
break;
}
return nowMonth;
}
public int getNowDay(){
return Integer.parseInt(sysNowTime[2]);

}
public Integer getShowYear(){
return Integer.parseInt(sysNowTime[5]);
}

public Integer getShowMonth(){
/**
*Integer getShowMonth()
*獲取在組給框中顯示的中文格式月份:如七月\八月等
*/
return turnMonth(sysNowTime[1]);
}

public void setDays(int monthDays,int initWeek,int day){
/**
*void setDays(int monthDays,int initWeek,int day)
*@para monthDays 本月天數
*@para initWeek 初始星期
*@para day 今天日
*設置月歷
*/
setDateNull();
for(int i=initWeek;i<initWeek+monthDays+1;i++){
if((i-initWeek+1)==day){
labelDay[i].setBorder(border1);
labelDay[i].setForeground(Color.BLUE);
labelDay[i].setFont(new Font("黑體",Font.BOLD,20));
}else if((i%7==0)||(i%7==6))
labelDay[i].setForeground(Color.RED);
else{
labelDay[i].setForeground(Color.BLACK);
}
labelDay[i].setText(String.valueOf(i-initWeek+1));
}
for(int i=initWeek+monthDays;i<42;i++)
labelDay[i].setText("");
}

class TimerListener implements ActionListener{
//AdapterDemo var=new AdapterDemo("萬年歷程序--Aidy");

public void actionPerformed(ActionEvent e) {

GregorianCalendar g = new GregorianCalendar();
String clock = new String((g.getTime().toString().split(" "))[3]);
labelTime.setText(clock);
}

}
public static void main(String args[])
{
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){
throw new RuntimeException(e);
}
AidyCalender var=new AidyCalender("萬年歷程序--Aidy");

}

}

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/aidydream/archive/2008/07/19/2678664.aspx

② 用java做一個日歷顯示數據功能,在頁面上顯示日期數據。

MainFrame.java是顯示日歷程序,Clock.java是日歷計算程序(可以不要)。

編譯後運行MainFrame這個類即可。

swing窗口顯示萬年歷,jdk1.4以上環境編譯運行。

packageorg.test;
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.GridLayout;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.sql.Date;
importjava.util.Calendar;
importjavax.swing.JComboBox;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
{
=-306484324260972141L;
JPanelpanel=newJPanel(newBorderLayout());
JPanelpanel1=newJPanel();
JPanelpanel2=newJPanel(newGridLayout(7,7));
JPanelpanel3=newJPanel();
JLabel[]label=newJLabel[49];
JLabely_label=newJLabel("年份");
JLabelm_label=newJLabel("月份");
JComboBoxcom1=newJComboBox();
JComboBoxcom2=newJComboBox();
intre_year,re_month,x_size,y_size;
Stringyear_num;
Calendarnow=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(inti=0;i<49;i++){
label[i]=newJLabel("",JLabel.CENTER);//將顯示的字元設置為居中
panel2.add(label[i]);
}
panel3.add(newClock(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(newClockAction());
com2.addActionListener(newClockAction());
setContentPane(panel);
setVisible(true);
setResizable(false);
}

{
publicvoidactionPerformed(ActionEventarg0){
intc_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
}
}

publicvoidInit(){
intyear,month_num,first_day_num;
Stringlog[]={"日","一","二","三","四","五","六"};
for(inti=0;i<7;i++){
label[i].setText(log[i]);
}

for(inti=0;i<49;i=i+7){
label[i].setForeground(Color.red);//將星期日的日期設置為紅色
}

for(inti=6;i<49;i=i+7){
label[i].setForeground(Color.green);//將星期六的日期設置為綠色
}

for(inti=1;i<10000;i++){
com1.addItem(""+i);
}

for(inti=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);
}

publicintuse(intreyear,intremonth){
intweek_num;
now.set(reyear,remonth,1);//設置時間為所要查詢的年月的第一天
week_num=(int)(now.get(Calendar.DAY_OF_WEEK));//得到第一天的星期
returnweek_num;
}

@SuppressWarnings("deprecation")
publicvoidResetday(intweek_log,intyear_log,intmonth_log){
intmonth_day_score;//存儲月份的天數
intcount;
month_day_score=0;
count=1;
Datedate=newDate(year_log,month_log+1,1);//now
Calendarcal=Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH,-1);//前個月
month_day_score=cal.getActualMaximum(Calendar.DAY_OF_MONTH);//最後一天
for(inti=7;i<49;i++){//初始化標簽
label[i].setText("");
}
week_log=week_log+6;//將星期數加6,使顯示正確
month_day_score=month_day_score+week_log;
for(inti=week_log;i<month_day_score;i++,count++){
label[i].setText(count+"");
}
}

publicstaticvoidmain(String[]args){
JFrame.(true);
newMainFrame();
}
}
packageorg.test;
importjava.awt.Color;
importjava.util.Calendar;
importjava.awt.Canvas;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.text.SimpleDateFormat;
{
=3660124045489727166L;
MainFramemf;
Threadt;
Stringtime;

publicClock(MainFramemf){
this.mf=mf;
setSize(280,40);
setBackground(Color.white);
t=newThread(this);//實例化線程
t.start();
//調用線程
}

publicvoidrun(){
while(true){
try{
Thread.sleep(1000);//休眠1秒鍾
}catch(InterruptedExceptione){
System.out.println("異常");
}
this.repaint(100);
}
}

publicvoidpaint(Graphicsg){
Fontf=newFont("宋體",Font.BOLD,16);
SimpleDateFormatSDF=newSimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化時間顯示類型
Calendarnow=Calendar.getInstance();
time=SDF.format(now.getTime());//得到當前日期和時間
g.setFont(f);
g.setColor(Color.orange);
g.drawString(time,45,25);
}
}
閱讀全文

與java顯示日歷相關的資料

熱點內容
銀行人員如何解壓 瀏覽:827
newfile命令快捷鍵 瀏覽:567
阿里雲物理伺服器 瀏覽:953
靈狐視頻app哪個好 瀏覽:257
大廠退役程序員自述 瀏覽:252
linux命令watch 瀏覽:889
加密幣哪些平台不撤出中國 瀏覽:553
max加線命令 瀏覽:424
app胖瘦模式哪個好用 瀏覽:724
可以下載源碼的軟體 瀏覽:487
程序員寫一天代碼累嗎 瀏覽:628
ie文件夾禁止訪問 瀏覽:545
百川互聯網程序員 瀏覽:785
linuxpython解釋器 瀏覽:669
興安得力軟體加密狗 瀏覽:494
智能網路攝像頭加密 瀏覽:575
軟體畢業程序員培訓 瀏覽:654
安卓陀螺儀低怎麼辦 瀏覽:248
一級建造師復習題集pdf 瀏覽:905
法理學pdf海默 瀏覽:394