这是我刚做出来的,时间紧迫,做得有些粗糙,但是你要的功能基本实现了!import javaawt;
import javatext;
import javaxswing;
import javaawtevent;
import javautil;
//import javautilTimer;
public class DataSimple implements ItemListener
{
JFrame mainFrame;
JLabel yearLabel,monthLabel,showTextLabel,timeLabel;
JTextArea showArea;
JComboBox yearBox,monthBox;
JPanel panel_top,panel_down;
Container con;
//JTable myTable;
//JScrollPane JSpane;
String year[]=new String[9000];
String month[]=new String[12];
Date today;
GregorianCalendar cal;
//Timer myTimer;
//TimerTask myTask;
int totalDays,startDay,i;
String selectYear,selectMonth;
Object [][] day=new Object[4][6];
static final String head[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
SimpleDateFormat timeFormat=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
public DataSimple()
{
mainFrame=new JFrame("模拟万年日历");
con=mainFramegetContentPane();
yearLabel=new JLabel("选择年份:");
monthLabel=new JLabel("选择月份:");
for(int i=1900;i<10000;i++)
{
try
{
//year[i-1900]=(new Integer(i))toString();
year[i-1900]=StringvalueOf(i);
}
catch(Exception e)
{
JOptionPaneshowMessageDialog(mainFrame,"出现异常");
}
}
yearBox=new JComboBox(year);
yearBoxsetSelectedItem("2010");
yearBoxaddItemListener(this);
for(int i=0;i<12;i++)
{
month[i]=(new Integer(i))toString();
}
monthBox=new JComboBox(month);
monthBoxsetSelectedItem("1");
monthBoxaddItemListener(this);
panel_top=new JPanel();
panel_topsetLayout(new FlowLayout());
panel_topadd(yearLabel);
panel_topadd(yearBox);
panel_topadd(monthLabel);
panel_topadd(monthBox);
conadd(panel_top,BorderLayoutNORTH);
showArea=new JTextArea();
showAreasetEditable(false);
showDate(2010,1);
//myTable=new JTable(day,head);
conadd(showArea,BorderLayoutCENTER);
showTextLabel=new JLabel("现在时间是:");
today=new Date();
timeLabel=new JLabel(timeFormatformat(today));
panel_down=new JPanel();
panel_downsetLayout(new FlowLayout());
panel_downadd(showTextLabel);
panel_downadd(timeLabel);
conadd(panel_down,BorderLayoutSOUTH);
//conadd(showArea,BorderLayoutCENTER);
mainFramesetSize(400,300);
mainFramesetVisible(true);
mainFramesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
public void showDate(int year,int month)
{
showAreasetText("");
cal=new GregorianCalendar(year,month-1,1);
totalDays=calgetActualMaximum(CalendarDAY_OF_MONTH);
startDay=calget(CalendarDAY_OF_WEEK)-1;
for(i=0;i<headlength;i++)
showAreaappend(head[i]+" ");
showAreaappend("\n");
//输出第一天之前的空格,每个输出项8个空格
for(i=0;i<startDay;i++)
{
//day[0][i]=new Integer(0);
showAreaappend(" ");
}
//依次输出每一天,每一个输出项占8个字符宽度
for(int days=1;days<=totalDays;days++)
{ /for(int j=0;j<5;j++)
{
for(int z=0;z<7;z++)
{
if(!day[j][z]equals(0))
{
day[j][z]=new Integer(days);
}/
if(days<10)
{
showAreaappend(" "+days+" ");
}
else if(days>=10)
{
showAreaappend(" "+days+" ");
}
i++;
if(i==7)
{
showAreaappend("\n");
i=0;
}
/}
}/
}
}
public void itemStateChanged(ItemEvent e)
{
if(egetStateChange()==ItemEventSELECTED)
{
selectYear=(String)(yearBoxgetSelectedItem());
selectMonth=(String)(monthBoxgetSelectedItem());
showDate(IntegerparseInt(selectYear),IntegerparseInt(selectMonth));
}
}
public static void main(String argv[])
{
new DataSimple();
}
}运行截图:
我这里两个类,你跑起来看看,一个是计算农历的,一个是拿来显示的。
import javautil;
/
农历计算
@author xysource
/
public class LauarUtil
{
private static String[] daySTrPreArray = { "初", "十", "廿", "卅", " " };
/
月份
/
private static String[] dayStrArray = { "", "一", "二", "三", "四", "五", "六",
"七", "八", "九", "十", "十一", "十二" };
// private static String[] weekStrArray = { "", "日", "一", "二", "三", "四",
// "五", "六" };
final static long[] lunarInfo = new long[] { 0x04bd8, 0x04ae0, 0570,
0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
0x04ae0, 05b6, 04d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0,
0da2, 0x095b0, 0x14977, 0x04970, 04b0, 0x0b4b5, 0x06a50,
0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566,
0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0,
0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4,
0x025d0, 0x092d0, 0x0d2b2, 0950, 0x0b557, 0x06ca0, 0x0b550,
0x15355, 0x04da0, 05d0, 0x14573, 0x052d0, 09a8, 0x0e950,
0x06aa0, 0ea6, 0b50, 0x04b60, 0ae4, 0570, 0x05260,
0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0,
04d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6,
0x095b0, 0x049b0, 0974, 04b0, 0x0b27a, 0x06a50, 0x06d40,
0f46, 0b60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x074a3,
0x0ea50, 0x06b58, 0x055c0, 0b60, 0x096d5, 0x092e0, 0x0c960,
0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0bb7, 0x025d0,
0x092d0, 0x0cab5, 0950, 0x0b4a0, 0x0baa4, 0d50, 0x055d9,
0x04ba0, 05b0, 0x15176, 0x052b0, 0930, 0x07954, 0x06aa0,
0d50, 0x05b52, 0x04b60, 06e6, 04e0, 0x0d260, 0x0ea65,
0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 04d0,
0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2,
0x049b0, 0577, 04b0, 0a50, 0x1b255, 0x06d20, 0da0 };
final public static int lYearDays(int y)// ====== 传回农历 y年的总天数
{
int i, sum = 348;
for (i = 0x8000; i > 0x8; i >>= 1)
{
if ((lunarInfo[y - 1900] & i) != 0)
sum += 1;
}
return (sum + leapDays(y));
}
final public static int leapDays(int y)// ====== 传回农历 y年闰月的天数
{
if (leapMonth(y) != 0)
{
if ((lunarInfo[y - 1900] & 0x10000) != 0)
return 30;
else
return 29;
}
else
return 0;
}
final public static int leapMonth(int y)// ====== 传回农历 y年闰哪个月 1-12 , 没闰传回 0
{
return (int) (lunarInfo[y - 1900] & 0xf);
}
final public static int monthDays(int y, int m)// ====== 传回农历 y年m月的总天数
{
if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0)
return 29;
else
return 30;
}
final public static String AnimalsYear(int y)// ====== 传回农历 y年的生肖
{
final String[] Animals = new String[] { "鼠", "牛", "虎", "兔", "龙", "蛇",
"马", "羊", "猴", "鸡", "狗", "猪" };
return Animals[(y - 4) % 12];
}
final public static String cyclicalm(int num)// ====== 传入 月日的offset 传回干支,
// 0=甲子
{
final String[] Gan = new String[] { "甲", "乙", "丙", "丁", "戊", "己", "庚",
"辛", "壬", "癸" };
final String[] Zhi = new String[] { "子", "丑", "寅", "卯", "辰", "巳", "午",
"未", "申", "酉", "戌", "亥" };
return (Gan[num % 10] + Zhi[num % 12]);
}
final public static String cyclical(int y)// ====== 传入 offset 传回干支, 0=甲子
{
int num = y - 1900 + 36;
return (cyclicalm(num));
}
final public long[] Lunar(int y, int m)// 传出农历year0 month1 day2
// yearCyl3
// monCyl4
// dayCyl5 isLeap6
{
final int[] year20 = new int[] { 1, 4, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1 };
final int[] year19 = new int[] { 0, 3, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 };
final int[] year2000 = new int[] { 0, 3, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1 };
long[] nongDate = new long[7];
int i = 0, temp = 0, leap = 0;
Date baseDate = new Date(1900, 1, 31);
Date objDate = new Date(y, m, 1);
long offset = (objDategetTime() - baseDategetTime()) / 86400000L;
if (y < 2000)
offset += year19[m - 1];
if (y > 2000)
offset += year20[m - 1];
if (y == 2000)
offset += year2000[m - 1];
nongDate[5] = offset + 40;
nongDate[4] = 14;
for (i = 1900; i < 2050 && offset > 0; i++)
{
temp = lYearDays(i);
offset -= temp;
nongDate[4] += 12;
}
if (offset < 0)
{
offset += temp;
i--;
nongDate[4] -= 12;
}
nongDate[0] = i;
nongDate[3] = i - 1864;
leap = leapMonth(i); // 闰哪个月
nongDate[6] = 0;
for (i = 1; i < 13 && offset > 0; i++)
{
// 闰月
if (leap > 0 && i == (leap + 1) && nongDate[6] == 0)
{
--i;
nongDate[6] = 1;
temp = leapDays((int) nongDate[0]);
}
else
{
temp = monthDays((int) nongDate[0], i);
}
// 解除闰月
if (nongDate[6] == 1 && i == (leap + 1))
nongDate[6] = 0;
offset -= temp;
if (nongDate[6] == 0)
nongDate[4]++;
}
if (offset == 0 && leap > 0 && i == leap + 1)
{
if (nongDate[6] == 1)
{
nongDate[6] = 0;
}
else
{
nongDate[6] = 1;
--i;
--nongDate[4];
}
}
if (offset < 0)
{
offset += temp;
--i;
--nongDate[4];
}
nongDate[1] = i;
nongDate[2] = offset + 1;
return nongDate;
}
final public static long[] calElement(int y, int m, int d)
// 传出y年m月d日对应的农历year0 month1 day2 yearCyl3 monCyl4 dayCyl5 isLeap6
{
long[] nongDate = new long[7];
int i = 0, temp = 0, leap = 0;
Date baseDate = new Date(0, 0, 31);
Date objDate = new Date(y - 1900, m - 1, d);
long offset = (objDategetTime() - baseDategetTime()) / 86400000L;
nongDate[5] = offset + 40;
nongDate[4] = 14;
for (i = 1900; i < 2050 && offset > 0; i++)
{
temp = lYearDays(i);
offset -= temp;
nongDate[4] += 12;
}
if (offset < 0)
{
offset += temp;
i--;
nongDate[4] -= 12;
}
nongDate[0] = i;
nongDate[3] = i - 1864;
leap = leapMonth(i); // 闰哪个月
nongDate[6] = 0;
for (i = 1; i < 13 && offset > 0; i++)
{
// 闰月
if (leap > 0 && i == (leap + 1) && nongDate[6] == 0)
{
--i;
nongDate[6] = 1;
temp = leapDays((int) nongDate[0]);
}
else
{
temp = monthDays((int) nongDate[0], i);
}
// 解除闰月
if (nongDate[6] == 1 && i == (leap + 1))
nongDate[6] = 0;
offset -= temp;
if (nongDate[6] == 0)
nongDate[4]++;
}
if (offset == 0 && leap > 0 && i == leap + 1)
{
if (nongDate[6] == 1)
{
nongDate[6] = 0;
}
else
{
nongDate[6] = 1;
--i;
--nongDate[4];
}
}
if (offset < 0)
{
offset += temp;
--i;
--nongDate[4];
}
nongDate[1] = i;
nongDate[2] = offset + 1;
return nongDate;
}
public static String getchina(int day)
{
if (day == 10)
return "初十";
if (day == 20)
return "二十";
if (day == 30)
return "三十";
String a = daySTrPreArray[(int) ((day) / 10)];
//Systemoutprintln(a);
a += dayStrArray[(int) (day % 10)];
return a;
}
public static String getLauar(Calendar cld)
{
int year = cldget(CalendarYEAR);
int month = cldget(CalendarMONTH) + 1;
int day = cldget(CalendarDAY_OF_MONTH);
long[] lauarStrArray = calElement(year, month, day);
String monthStr = dayStrArray[(int) (lauarStrArray[1])];
return monthStr + "月" + getchina((int) (lauarStrArray[2]));//AnimalsYear(1989)+"年 农历" +
}
public static void main(String[] args)
{
Calendar cld = CalendargetInstance();
// for (int i = 0; i < 145; i++) {
Systemoutprintln(getLauar(cld));
// cldadd(CalendarDAY_OF_MONTH, 1);
// }
}
}
Java-时钟万年历
clockjava代码如下:
/
Clockjava
Summary 数字时间显示
Created on 2005-8-14
@author 高
remark 如有改动请发一份代码给我,邮箱gkgklovelove@eyoucom
/
package Clock;
import javaxswing;
import javaawt;
import javaawtevent;
import javatext;
import javautil;
class Clock extends Canvas implements Runnable{
MainFrame mf;
Thread t;
String time;
Clock(MainFrame mf){
thismf=mf;
setSize(400,40);
setBackground(Colorwhite);
t=new Thread(this); //实例化线程
tstart(); //调用线程
}
public void run(){
while(true){
try{
tsleep(1000); //休眠1秒钟
}catch(InterruptedException e){
Systemoutprintln("异常");
}
thisrepaint(100);
}
}
public void paint(Graphics g){
Font f=new Font("宋体",FontBOLD,16);
SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化时间显示类型
Calendar now=CalendargetInstance();
time=SDFformat(nowgetTime()); //得到当前日期和时间
gsetFont(f);
gsetColor(Colororange);
gdrawString(time,100,25);
}
MainFrame.java 的代码如下:
/
MainFramejava
Summary 万年历主类
Created on 2005-8-14
@author 高
remark 如有改动请发一份代码给我,邮箱gkgklovelove@eyoucom
/
package Clock;
import javaxswing;
import javaawt;
import javaawtevent;
import javatext;
import javautil;
class MainFrame extends JFrame{
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();
JButton button=new JButton("查看");
int re_year,re_month;
int x_size,y_size;
String year_num;
Calendar now=CalendargetInstance(); //实例化Calendar
MainFrame(){
super("万年历");
setSize(300,350);
x_size=(int)(ToolkitgetDefaultToolkit()getScreenSize()getWidth());
y_size=(int)(ToolkitgetDefaultToolkit()getScreenSize()getHeight());
setLocation((x_size-300)/2,(y_size-350)/2);
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
panel1add(y_label);
panel1add(com1);
panel1add(m_label);
panel1add(com2);
panel1add(button);
for(int i=0;i<49;i++){
label[i]=new JLabel("",JLabelCENTER);//将显示的字符设置为居中
panel2add(label[i]);
}
panel3add(new Clock(this));
paneladd(panel1,BorderLayoutNORTH);
paneladd(panel2,BorderLayoutCENTER);
paneladd(panel3,BorderLayoutSOUTH);
panelsetBackground(Colorwhite);
panel1setBackground(Colorwhite);
panel2setBackground(Colorwhite);
panel3setBackground(Colorwhite);
Init();
buttonaddActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int c_year,c_month,c_week;
c_year=IntegerparseInt(com1getSelectedItem()toString()); //得到当前所选年份
c_month=IntegerparseInt(com2getSelectedItem()toString())-1; //得到当前月份,并减1,计算机中的月为0-11
c_week=use(c_year,c_month); //调用函数use,得到星期几
Resetday(c_week,c_year,c_month); //调用函数Resetday
}});
setContentPane(panel);
setVisible(true);
setResizable(false);
}
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(Colorred); //将星期日的日期设置为红色
}
for(int i=6;i<49;i=i+7){
label[i]setForeground(Colorgreen);//将星期六的日期设置为绿色
}
for(int i=1;i<10000;i++){
com1addItem(""+i);
}
for(int i=1;i<13;i++){
com2addItem(""+i);
}
month_num=(int)(nowget(CalendarMONTH)); //得到当前时间的月份
year=(int)(nowget(CalendarYEAR)); //得到当前时间的年份
com1setSelectedIndex(year-1); //设置下拉列表显示为当前年
com2setSelectedIndex(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;
nowset(reyear,remonth,1); //设置时间为所要查询的年月的第一天
week_num= (int)(nowget(CalendarDAY_OF_WEEK));//得到第一天的星期
return week_num;
}
public void Resetday(int week_log,int year_log,int month_log){
int month_score_log; //判断是否是闰年的标记
int month_day_score; //存储月份的天数
int count;
month_score_log=0;
month_day_score=0;
count=1;
if(year_log%4==0&&year_log%100!=0||year_log%400==0){//判断是否为闰年
month_score_log=1;
}
month_log=month_log+1; //将传来的月份数加1
switch(month_log){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
month_day_score=31;
break;
case 4:
case 6:
case 9:
case 11:
month_day_score=30;
break;
case 2:
if(month_score_log==1){
month_day_score=29;
}
else{
month_day_score=28;
}
break;
}
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){
JFramesetDefaultLookAndFeelDecorated(true);
MainFrame start=new MainFrame();
}
}
以下是用java swing编写的日历,很好用,在我所做的系统里就能够正常的使用
接下来 是具体代码:
package Demo;
import javaawtBorderLayout;
import javaawtColor;
import javaawtDimension;
import javaawtFlowLayout;
import javaawtFont;
import javaawtFrame;
import javaawtGridLayout;
import javaawtPoint;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaawteventMouseAdapter;
import javaawteventMouseEvent;
import javatextParseException;
import javatextSimpleDateFormat;
import javautilCalendar;
import javautilDate;
import javaxswingJButton;
import javaxswingJDialog;
import javaxswingJFrame;
import javaxswingJFormattedTextField;
import javaxswingJLabel;
import javaxswingJPanel;
import javaxswingJSpinner;
import javaxswingSpinnerNumberModel;
import javaxswingSwingConstants;
import javaxswingSwingUtilities;
import javaxswingborderLineBorder;
import javaxswingeventChangeEvent;
import javaxswingeventChangeListener;
public class DateChooser extends JPanel implements ActionListener,
ChangeListener {
/
/
private static final long serialVersionUID = 1L;
int startYear = 1980;
int lastYear = 2050;
int width = 270;
int height = 200;
Color backGroundColor = Colorgray;
Color palletTableColor = Colorwhite;
Color todayBackColor = Colororange;
Color weekFontColor = Colorblue;
Color dateFontColor = Colorblack;
Color weekendFontColor = Colorred;
Color controlLineColor = Colorpink;
Color controlTextColor = Colorwhite;
Color rbFontColor = Colorwhite;
Color rbBorderColor = Colorred;
Color rbButtonColor = Colorpink;
Color rbBtFontColor = Colorred;
JDialog dialog;
JSpinner yearSpin;
JSpinner monthSpin;
JSpinner hourSpin;
JSpinner minuteSpin;
JButton[][] daysButton = new JButton[6][7];
JFormattedTextField jFormattedTextField;
Calendar c = getCalendar();
DateChooser(JFormattedTextField jftf) {
jFormattedTextField = jftf;
setLayout(new BorderLayout());
setBorder(new LineBorder(backGroundColor, 2));
setBackground(backGroundColor);
JPanel topYearAndMonth = createYearAndMonthPanal();
add(topYearAndMonth, BorderLayoutNORTH);
JPanel centerWeekAndDay = createWeekAndDayPanal();
add(centerWeekAndDay, BorderLayoutCENTER);
}
private JPanel createYearAndMonthPanal() {
int currentYear = cget(CalendarYEAR);
int currentMonth = cget(CalendarMONTH) + 1;
int currentHour = cget(CalendarHOUR_OF_DAY);
int currentMinute = cget(CalendarMINUTE);
JPanel result = new JPanel();
resultsetLayout(new FlowLayout());
resultsetBackground(controlLineColor);
yearSpin = new JSpinner(new SpinnerNumberModel(currentYear, startYear,
lastYear, 1));
yearSpinsetPreferredSize(new Dimension(48, 20));
yearSpinsetName("Year");
yearSpinsetEditor(new JSpinnerNumberEditor(yearSpin, "####"));
yearSpinaddChangeListener(this);
resultadd(yearSpin);
JLabel yearLabel = new JLabel("年");
yearLabelsetForeground(controlTextColor);
resultadd(yearLabel);
monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1, 12, 1));
monthSpinsetPreferredSize(new Dimension(35, 20));
monthSpinsetName("Month");
monthSpinaddChangeListener(this);
resultadd(monthSpin);
JLabel monthLabel = new JLabel("月");
monthLabelsetForeground(controlTextColor);
resultadd(monthLabel);
hourSpin = new JSpinner(new SpinnerNumberModel(currentHour, 0, 23, 1));
hourSpinsetPreferredSize(new Dimension(35, 20));
hourSpinsetName("Hour");
hourSpinaddChangeListener(this);
resultadd(hourSpin);
JLabel hourLabel = new JLabel("时");
hourLabelsetForeground(controlTextColor);
resultadd(hourLabel);
minuteSpin = new JSpinner(new SpinnerNumberModel(currentMinute, 0, 59,
1));
minuteSpinsetPreferredSize(new Dimension(35, 20));
minuteSpinsetName("Minute");
minuteSpinaddChangeListener(this);
resultadd(minuteSpin);
JLabel minuteLabel = new JLabel("分");
minuteLabelsetForeground(controlTextColor);
resultadd(minuteLabel);
return result;
}
private JPanel createWeekAndDayPanal() {
String colname[] = { "日", "一", "二", "三", "四", "五", "六" };
JPanel result = new JPanel();
resultsetFont(new Font("宋体", FontPLAIN, 12));
resultsetLayout(new GridLayout(7, 7));
resultsetBackground(Colorwhite);
JLabel cell;
for (int i = 0; i < 7; i++) {
cell = new JLabel(colname[i]);
cellsetHorizontalAlignment(JLabelCENTER);
if (i == 0 || i == 6)
cellsetForeground(weekendFontColor);
else
cellsetForeground(weekFontColor);
resultadd(cell);
}
int actionCommandId = 0;
for (int i = 0; i < 6; i++)
for (int j = 0; j < 7; j++) {
JButton numberButton = new JButton();
numberButtonsetBorder(null);
numberButtonsetHorizontalAlignment(SwingConstantsCENTER);
numberButtonsetActionCommand(StringvalueOf(actionCommandId));
numberButtonaddActionListener(this);
numberButtonsetBackground(palletTableColor);
numberButtonsetForeground(dateFontColor);
if (j == 0 || j == 6)
numberButtonsetForeground(weekendFontColor);
else
numberButtonsetForeground(dateFontColor);
daysButton[i][j] = numberButton;
numberButtonaddMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (egetClickCount() == 2) {
closeAndSetDate();
}
}
});
resultadd(numberButton);
actionCommandId++;
}
return result;
}
private JDialog createDialog(Frame owner) {
JDialog result = new JDialog(owner, "日期时间选择", true);
resultsetDefaultCloseOperation(JDialogHIDE_ON_CLOSE);
resultgetContentPane()add(this, BorderLayoutCENTER);
resultpack();
resultsetSize(width, height);
return result;
}
public void showDateChooser(Point position) {
Object tmpobj=SwingUtilitiesgetWindowAncestor(jFormattedTextField);
if(tmpobjgetClass()isInstance(new JDialog())||tmpobjgetClass()getSuperclass()isInstance(new JDialog()))
{
JDialog ownerdialog = (JDialog) SwingUtilities
getWindowAncestor(jFormattedTextField);
Frame owner = (Frame) SwingUtilitiesgetWindowAncestor(ownerdialog);
if (dialog == null || dialoggetOwner() != owner) {
dialog = createDialog(owner);
}
dialogsetLocation(getAppropriateLocation(owner, position));
}
else if(tmpobjgetClass()isInstance(new JFrame())||tmpobjgetClass()getSuperclass()isInstance(new JFrame()))
{
JFrame ownerFrame = (JFrame) SwingUtilities
getWindowAncestor(jFormattedTextField);
if (dialog == null || dialoggetOwner() != ownerFrame) {
dialog = createDialog(ownerFrame);
}
dialogsetLocation(getAppropriateLocation(ownerFrame, position));
}
flushWeekAndDay();
dialogsetVisible(true);
}
Point getAppropriateLocation(Frame owner, Point position) {
Point result = new Point(position);
Point p = ownergetLocation();
int offsetX = (positionx + width) - (px + ownergetWidth());
int offsetY = (positiony + height) - (py + ownergetHeight());
if (offsetX > 0) {
resultx -= offsetX;
}
if (offsetY > 0) {
resulty -= offsetY;
}
return result;
}
public void closeAndSetDate() {
setDate(cgetTime());
dialogdispose();
}
private Calendar getCalendar() {
Calendar result = CalendargetInstance();
resultsetTime(getDate());
return result;
}
private int getSelectedYear() {
return ((Integer) yearSpingetValue())intValue();
}
private int getSelectedMonth() {
return ((Integer) monthSpingetValue())intValue();
}
private int getSelectedHour() {
return ((Integer) hourSpingetValue())intValue();
}
private int getSelectedMinute() {
return ((Integer) minuteSpingetValue())intValue();
}
private void dayColorUpdate(boolean isOldDay) {
int day = cget(CalendarDAY_OF_MONTH);
cset(CalendarDAY_OF_MONTH, 1);
int actionCommandId = day - 2 + cget(CalendarDAY_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() {
cset(CalendarDAY_OF_MONTH, 1);
int maxDayNo = cgetActualMaximum(CalendarDAY_OF_MONTH);
int dayNo = 2 - cget(CalendarDAY_OF_WEEK);
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
String s = "";
if (dayNo >= 1 && dayNo <= maxDayNo)
s = StringvalueOf(dayNo);
daysButton[i][j]setText(s);
dayNo++;
}
}
dayColorUpdate(false);
}
public void stateChanged(ChangeEvent e) {
JSpinner source = (JSpinner) egetSource();
if (sourcegetName()equals("Minute")) {
cset(CalendarMINUTE, getSelectedMinute());
return;
}
if (sourcegetName()equals("Hour")) {
cset(CalendarHOUR_OF_DAY, getSelectedHour());
return;
}
dayColorUpdate(true);
if (sourcegetName()equals("Year")) {
cset(CalendarYEAR, getSelectedYear());
}
if (sourcegetName()equals("Month")) {
cset(CalendarMONTH, getSelectedMonth() - 1);
}
flushWeekAndDay();
}
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) egetSource();
if (sourcegetText()length() == 0)
return;
dayColorUpdate(true);
sourcesetForeground(todayBackColor);
int newDay = IntegerparseInt(sourcegetText());
cset(CalendarDAY_OF_MONTH, newDay);
}
public void setDate(Date date) {
jFormattedTextFieldsetText(getDefaultDateFormat()format(date));
}
public Date getDate() {
try {
String dateString = jFormattedTextFieldgetText();
return getDefaultDateFormat()parse(dateString);
} catch (ParseException e) {
return getNowDate();
} catch (Exception ee) {
return getNowDate();
}
}
private static Date getNowDate() {
return CalendargetInstance()getTime();
}
private static SimpleDateFormat getDefaultDateFormat() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm");
}
}
测试程序:
package Demo;
import javaawtPoint;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingJButton;
import javaxswingJFormattedTextField;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJPanel;
public class DemoForDateChooser extends JFrame {
private JFormattedTextField formattedTextField;
/
Launch the application
@param args
/
public static void main(String args[]) {
try {
DemoForDateChooser frame = new DemoForDateChooser();
framesetVisible(true);
} catch (Exception e) {
eprintStackTrace();
}
}
/
Create the frame
/
public DemoForDateChooser() {
super();
setTitle("日期选择框");
getContentPane()setLayout(null);
setBounds(100, 100, 383, 137);
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
final JPanel panel = new JPanel();
panelsetLayout(null);
panelsetBounds(0, 0, 375, 107);
getContentPane()add(panel);
formattedTextField = new JFormattedTextField();
formattedTextFieldsetBounds(68, 48, 175, 23);
final JButton button = new JButton();
buttonaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
DateChooser mDateChooser=new DateChooser(formattedTextField);
Point p = buttongetLocationOnScreen();
py = py + 30;
mDateChoosershowDateChooser(p);
formattedTextFieldrequestFocusInWindow();
}
});
buttonsetText("选择日期");
buttonsetBounds(249, 47, 99, 23);
paneladd(button);
final JLabel label = new JLabel();
labelsetText("日期:");
labelsetBounds(10, 47, 71, 23);
paneladd(label);
paneladd(formattedTextField);
//
}
}
package clock;
/ //
MainFramejava
Summary 万年历主类
Created on
@author
remark
/
import javaawtBorderLayout;
import javaawtColor;
import javaawtGridLayout;
import javaawtToolkit;
import javaawteventActionEvent;
import javaawteventActionListener;
import javasqlDate;
import javautilCalendar;
import javaxswingJComboBox;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJPanel;
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 = CalendargetInstance(); // 实例化Calendar
MainFrame() {
super("万年历");
setSize(300, 350);
x_size = (int) (ToolkitgetDefaultToolkit()getScreenSize()getWidth());
y_size = (int) (ToolkitgetDefaultToolkit()getScreenSize()getHeight());
setLocation((x_size - 300) / 2, (y_size - 350) / 2);
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
panel1add(y_label);
panel1add(com1);
panel1add(m_label);
panel1add(com2);
for (int i = 0; i < 49; i++) {
label[i] = new JLabel("", JLabelCENTER);// 将显示的字符设置为居中
panel2add(label[i]);
}
panel3add(new Clock(this));
paneladd(panel1, BorderLayoutNORTH);
paneladd(panel2, BorderLayoutCENTER);
paneladd(panel3, BorderLayoutSOUTH);
panelsetBackground(Colorwhite);
panel1setBackground(Colorwhite);
panel2setBackground(Colorwhite);
panel3setBackground(Colorwhite);
Init();
com1addActionListener(new ClockAction());
com2addActionListener(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 = IntegerparseInt(com1getSelectedItem()toString()); // 得到当前所选年份
c_month = IntegerparseInt(com2getSelectedItem()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(Colorred); // 将星期日的日期设置为红色
}
for (int i = 6; i < 49; i = i + 7) {
label[i]setForeground(Colorgreen);// 将星期六的日期设置为绿色
}
for (int i = 1; i < 10000; i++) {
com1addItem("" + i);
}
for (int i = 1; i < 13; i++) {
com2addItem("" + i);
}
month_num = (int) (nowget(CalendarMONTH)); // 得到当前时间的月份
year = (int) (nowget(CalendarYEAR)); // 得到当前时间的年份
com1setSelectedIndex(year - 1); // 设置下拉列表显示为当前年
com2setSelectedIndex(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;
nowset(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天
week_num = (int) (nowget(CalendarDAY_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 = CalendargetInstance();
calsetTime(date);
caladd(CalendarMONTH, -1); // 前个月
month_day_score = calgetActualMaximum(CalendarDAY_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) {
JFramesetDefaultLookAndFeelDecorated(true);
new MainFrame();
}
}
以上就是关于使用JAVA编程万年历要求:使用图形用户界面;实现日期与星期的查询。全部的内容,包括:使用JAVA编程万年历要求:使用图形用户界面;实现日期与星期的查询。、万年历JAVA程序上机、需要一个java程序,最好是万年历的,要在500行左右的等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)