最近忽然对Java萌生了极大的兴趣,之前有选修过Java课程但是没怎么认真学,但近日不知因何迷恋上了Java,因此我趁热打铁,天天钻研Java,编写程序,下面是我学习过程中自己写一个练习作,实现了一个带界面的日历,在此与广大程序爱好者分享一下,希望大家多多指教:
程序界面:
http://s12/middle/6759f4614850e63cba02b&690
//DataTime.java
import java.util.*;
public class DateTime
{
Calendar date=Calendar.getInstance();
isLeapYear leapYear=new isLeapYear();
int year,month,dayofweek,februaryDays;
String monthDays[];
DateTime(int year,int month)
{
this.year=year;
this.month=month;
date.set(year,month-1,1);
dayofweek=date.get(Calendar.DAY_OF_WEEK)-1;
if(leapYear.isLeapYear(year))
februaryDays=28;
else
februaryDays=29;
}
public String[] getMonthDays()
{
if(month==2)
monthDays=new
String[dayofweek+februaryDays];
else
if(month==4||month==6||month==9||month==11)
monthDays=new
String[dayofweek+30];
else
monthDays=new
String[dayofweek+31];
for(int
i=0;i<dayofweek;i++)
monthDays[i]="**";
for(int
i=dayofweek,n=1;i<monthDays.length;i++)
{
if(n<=9)
monthDays[i]="
"+String.valueOf(n);
else
monthDays[i]=String.valueOf(n);
n++;
}
return monthDays;
}
}
//WindowCalendar.java 此文件为主程序
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
//import java.util.*;
class Win extends Frame implements
ItemListener,MouseListener,Runnable
{
int monthes,year,day;
Thread threadTime;
DateTime dateTime;
NowDate nowDate=new NowDate();
SureExit sureExit;
Choice choiceMonth;
List
yearList;
TextArea text;
TextField textTime,textNowDate;
Panel
panelDate,panelNorth,panelTime,panelNowDate;
Label label=new
Label("^_^**********日期**********^_^");
Label labelTime=new Label("时间:");
Label labelNowDate=new Label("当前日期:");
String
month[]={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
String
monthDays[];
String nowMonthDays[];
Win(String s)
{
super(s);
threadTime=new
Thread(this);
choiceMonth=new Choice();
yearList=new
List(1,false);
text=new TextArea(7,22);
textTime=new
TextField(15);
textNowDate=new
TextField(12);
panelDate=new Panel();
panelNorth=new Panel();
panelTime=new Panel();
panelNowDate=new Panel();
sureExit=new
SureExit(this,"确定对话框",false);
for(int i=0;i<12;i++)
choiceMonth.add(""+month[i]);
for(int
i=1900;i<=2100;i++)
yearList.add(""+i);
panelDate.add(label);
panelNowDate.add(labelNowDate);
panelNowDate.add(textNowDate);
add(panelDate);
panelNorth.add(yearList);
panelNorth.add(choiceMonth);
add(panelNorth);
add(text);
add(panelNowDate);
add(labelTime);
add(textTime);
choiceMonth.addItemListener(this);
yearList.addItemListener(this);
textTime.addMouseListener(this);
addWindowListener(new
WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
sureExit.setVisible(true);
}
});
Font font=new Font("宋体",Font.PLAIN,15);
text.setFont(font);
text.setBackground(Color.cyan);
text.setForeground(Color.red);
Font f=new
Font("黑体",Font.BOLD,15);
label.setFont(f);
labelTime.setFont(f);
labelNowDate.setFont(f);
labelNowDate.setForeground(Color.blue);
label.setForeground(Color.blue);
labelTime.setForeground(Color.blue);
labelTime.setBackground(Color.orange);
labelNowDate.setBackground(Color.orange);
label.setBackground(Color.orange);
text.setEditable(false);
textTime.setEditable(false);
textTime.setForeground(Color.red);
textTime.setFont(f);
textTime.setBackground(Color.green);
textNowDate.setEditable(false);
textNowDate.setForeground(Color.red);
textNowDate.setBackground(Color.green);
textNowDate.setFont(f);
choiceMonth.select(nowDate.getMonth()-1);
int
y=nowDate.getYear()-1900;
yearList.select(y);
day=nowDate.getDay();
int m=nowDate.getMonth();
y=y+1900;
textNowDate.setText(""+y+"年"+m+"月"+day+"日");
setMonthDays();
setLayout(new
FlowLayout());
setBounds(200,150,250,330);
setVisible(true);
validate();
threadTime.start();
}
public void run()
{
while(true)
{
java.util.Date
date=new java.util.Date();
Dates
dates=new Dates();
String
time=date.toString().substring(11,19);
textNowDate.setText(dates.year+"年"+dates.month+"月"+dates.day+"日");
textTime.setText(time+"
星期"+dates.week);
try
{
threadTime.sleep(1000);
}
catch
(InterruptedException e)
{
}
}
}
public void mouseEntered(MouseEvent e)
{
TextField text1=(TextField)e.getSource();
text1.setBackground(Color.cyan);
}
public void mouseExited(MouseEvent e)
{
TextField textField=(TextField)e.getSource();
textField.setBackground(Color.green);
}
public void mouseReleased(MouseEvent
e){}
public void mousePressed(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void setMonthDays()
{
text.setText("日 一
二 三 四 五 六");
nowMonthDays=nowDate.getMonthDays();
for(int
i=0;i<nowMonthDays.length;i++)
{
if(i%7==0)
text.append("\n");
text.append(""+nowMonthDays[i]+"
");
}
}
public void itemStateChanged(ItemEvent
e)
{
monthes=choiceMonth.getSelectedIndex()+1;
year=1900+yearList.getSelectedIndex();
dateTime=new
DateTime(year,monthes);
monthDays=dateTime.getMonthDays();
text.setText("日 一
二 三 四 五 六");
for(int
i=0;i<monthDays.length;i++)
{
if(i%7==0)
text.append("\n");
text.append(""+monthDays[i]+"
");
}
}
}
class WindowCalendar
{
public static void main(String[] args)
{
Win win=new Win("日期和时间");
}
}
//isLeapYear.java
public class isLeapYear
{
boolean isLeapYear(int year)
{
if(year%4==0&&year%100!=0||year%400==0)
return
true;
else
return
false;
}
}
//NowDate.java
import java.util.*;
public class NowDate
{
Calendar nowDate=Calendar.getInstance();
int
day,month,year,dayofweek,februaryDays,hour,minute,second;
isLeapYear leapYear=new isLeapYear();
String monthDays[];
NowDate()
{
month=nowDate.get(Calendar.MONTH)+1;
year=nowDate.get(Calendar.YEAR);
day=nowDate.get(Calendar.DAY_OF_MONTH);
hour=nowDate.get(Calendar.HOUR_OF_DAY);
minute=nowDate.get(Calendar.MINUTE);
second=nowDate.get(Calendar.SECOND);
nowDate.set(year,month-1,1);
dayofweek=nowDate.get(Calendar.DAY_OF_WEEK)-1;
}
public String[] getMonthDays()
{
if(leapYear.isLeapYear(year))
februaryDays=28;
else
februaryDays=29;
if(month==2)
monthDays=new
String[28+dayofweek];
else
if(month==4||month==6||month==9||month==11)
monthDays=new
String[30+dayofweek];
else
monthDays=new
String[31+dayofweek];
for(int i=0;i<dayofweek;i++)
monthDays[i]="**";
for(int
i=dayofweek,n=1;i<monthDays.length;i++)
{
if(i<=9)
monthDays[i]="
"+String.valueOf(n);
else
monthDays[i]=String.valueOf(n);
n++;
}
return monthDays;
}
public int getMonth()
{
return month;
}
public int getYear()
{
return year;
}
public int getDay()
{
return day;
}
public int getHour()
{
return hour;
}
public int getMinute()
{
return minute;
}
public int getSecond()
{
return second;
}
}
//SureExit.java
import java.awt.*;
import java.awt.event.*;
public class SureExit extends Dialog implements
ActionListener
{
static final int YES=1,NO=0;
int message=-1;
Button buttonYes,buttonNo;
Panel panelButton,panelLabel;
Label label=new
Label("确定退出?",Label.CENTER);
SureExit(Frame f,String s,boolean b)
{
super(f,s,b);
buttonYes=new Button("Yes");
buttonNo=new
Button("No");
panelButton=new Panel();
panelLabel=new Panel();
panelLabel.add(label);
panelButton.add(buttonYes);
panelButton.add(buttonNo);
add(panelLabel);
add(panelButton);
buttonYes.addActionListener(this);
buttonNo.addActionListener(this);
addWindowListener(new
WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
setVisible(false);
}
});
setLocation(250,200);
setSize(140,120);
setLayout(new
FlowLayout());
}
public void actionPerformed(ActionEvent
e)
{
if(e.getSource()==buttonYes)
{
message=YES;
System.exit(0);
setVisible(false);
}
else
if(e.getSource()==buttonNo)
{
message=NO;
setVisible(false);
}
}
public int getMessage()
{
return message;
}
}
//Dates.java
import java.util.*;
class Dates
{
String year,month,day,week;
Dates()
{
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
year=String.valueOf(calendar.get(Calendar.YEAR));
month=String.valueOf(calendar.get(Calendar.MONTH)+1);
day=String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
week=String.valueOf(calendar.get(Calendar.DAY_OF_WEEK)-1);
}
}
加载中,请稍候......