在万年历中加上一个时钟,不知道怎么加,加到哪,求指导
第一个包第一个类
package aa;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Lunar {
private int year;
private int month;
private int day;
private boolean leap;
final static String chineseNumber[] = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"};
static SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
final static long[] lunarInfo = new long[]
{0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977,
0x04970, 0x0a4b0, 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, 0x0a950, 0x0b557,
0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950, 0x06aa0,
0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,
0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6,
0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570,
0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0,
0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5,
0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,
0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,
0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45,
0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0};
//====== 传回农历 y年的总天数
final private static int yearDays(int y) {
int i, sum = 348;
for (i = 0x8000; i > 0x8; i >>= 1) {
if ((lunarInfo[y - 1900] & i) != 0) sum += 1;
}
return (sum + leapDays(y));
}
//====== 传回农历 y年闰月的天数
final private static int leapDays(int y) {
if (leapMonth(y) != 0) {
if ((lunarInfo[y - 1900] & 0x10000) != 0)
return 30;
else
return 29;
} else
return 0;
}
//====== 传回农历 y年闰哪个月 1-12 , 没闰传回 0
final private static int leapMonth(int y) {
return (int) (lunarInfo[y - 1900] & 0xf);
}
//====== 传回农历 y年m月的总天数
final private static int monthDays(int y, int m) {
if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0)
return 29;
else
return 30;
}
//====== 传回农历 y年的生肖
final public String animalsYear() {
final String[] Animals = new String[]{"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
return Animals[(year - 4) % 12];
}
//====== 传入 月日的offset 传回干支, 0=甲子
final private static String cyclicalm(int num) {
final String[] Gan = new String[]{"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
final String[] Zhi = new String[]{"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
return (Gan[num % 10] + Zhi[num % 12]);
}
//====== 传入 offset 传回干支, 0=甲子
final public String cyclical() {
int num = year - 1900 + 36;
return (cyclicalm(num));
}
public static String getChinaDayString(int day) {
String chineseTen[] = {"初", "十", "廿", "卅"};
int n = day % 10 == 0 ? 9 : day % 10 - 1;
if (day > 30)
return "";
if (day == 10)
return "初十";
else
return chineseTen[day / 10] + chineseNumber[n];
}
/** *//**
* 传出y年m月d日对应的农历.
* yearCyl3:农历年与1864的相差数 ?
* monCyl4:从1900年1月31日以来,闰月数
* dayCyl5:与1900年1月31日相差的天数,再加40 ?
* @param cal
* @return
*/
public String getLunarDate(int year_log, int month_log, int day_log) {
//@SuppressWarnings("unused")
@SuppressWarnings("unused")
int yearCyl, monCyl, dayCyl;
int leapMonth = 0;
String nowadays;
Date baseDate = null;
Date nowaday=null;
try {
baseDate = chineseDateFormat.parse("1900年1月31日");
} catch (ParseException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
nowadays=year_log+"年"+month_log+"月"+day_log+"日";
try {
nowaday = chineseDateFormat.parse(nowadays);
} catch (ParseException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
//求出和1900年1月31日相差的天数
int offset = (int) ((nowaday.getTime() - baseDate.getTime()) / 86400000L);
dayCyl = offset + 40;
monCyl = 14;
//用offset减去每农历年的天数
// 计算当天是农历第几天
//i最终结果是农历的年份
//offset是当年的第几天
int iYear, daysOfYear = 0;
for (iYear = 1900; iYear < 10000 && offset > 0; iYear++) {
daysOfYear = yearDays(iYear);
offset -= daysOfYear;
monCyl += 12;
}
if (offset < 0) {
offset += daysOfYear;
iYear--;
monCyl -= 12;
}
//农历年份
year = iYear;
yearCyl = iYear - 1864;
leapMonth = leapMonth(iYear); //闰哪个月,1-12
leap = false;
//用当年的天数offset,逐个减去每月(农历)的天数,求出当天是本月的第几天
int iMonth, daysOfMonth = 0;
for (iMonth = 1; iMonth < 13 && offset > 0; iMonth++) {
//闰月
if (leapMonth > 0 && iMonth == (leapMonth + 1) && !leap) {
--iMonth;
leap = true;
daysOfMonth = leapDays(year);
} else
daysOfMonth = monthDays(year, iMonth);
offset -= daysOfMonth;
//解除闰月
if (leap && iMonth == (leapMonth + 1)) leap = false;
if (!leap) monCyl++;
}
//offset为0时,并且刚才计算的月份是闰月,要校正
if (offset == 0 && leapMonth > 0 && iMonth == leapMonth + 1) {
if (leap) {
leap = false;
} else {
leap = true;
--iMonth;
--monCyl;
}
}
//offset小于0时,也要校正
if (offset < 0) {
offset += daysOfMonth;
--iMonth;
--monCyl;
}
month = iMonth;
day = offset + 1;
if(((month)==1)&&day==1){
return "春节";
}
else if(((month)==1)&&day==15){
return "元宵";
}
else if(((month)==5)&&day==5)
return "端午";
else if(((month)==8)&&day==15)
return "中秋";
else if(day==1)
return chineseNumber[month - 1] + "月";
else
return getChinaDayString(day);
}
public String toString() {
if(chineseNumber[month - 1]=="一"&&getChinaDayString(day)=="初一")
return "农历"+year + "年";
else if(getChinaDayString(day)=="初一")
return chineseNumber[month - 1] + "月";
else
return getChinaDayString(day);
//return year + "年" + (leap ? "闰" : "") + chineseNumber[month - 1] + "月" + getChinaDayString(day);
}
}
第二个类
package aa;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class national {
public national() {
ComboBoxFrame frame = new ComboBoxFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class ComboBoxFrame extends JFrame {
public static final int DEFAULT_WIDTH = 430;//设置窗口大小
public static final int DEFAULT_HEIGHT = 200;
public ComboBoxFrame() {
setTitle("世界时间");//标题
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// add panel to frame
ComboBoxPanel panel = new ComboBoxPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
Thread t = new Thread(panel);//线程
t.start();//线程启动
}
}
@SuppressWarnings("serial")
class ComboBoxPanel extends JPanel implements Runnable {
JLabel label1 = null;//定义各组件
JLabel label2 = null;
JLabel label3 = null;
JLabel label4 = null;
JLabel label5 = null;
@SuppressWarnings("unchecked")
JComboBox combo1 = null;
@SuppressWarnings("unchecked")
JComboBox combo2 = null;
@SuppressWarnings("unchecked")
JComboBox combo3 = null;
//默认时间,北京、纽约、东京
private String timeZone1 = "Etc/GMT-8";//北京时间
private String timeZone2 = "America/New_York";//纽约时间
private String timeZone3 = "Asia/Tokyo";//东京时间
@SuppressWarnings("unchecked")
public ComboBoxPanel() {
label1 = new JLabel();
label2 = new JLabel();
label3 = new JLabel();
label4 = new JLabel("下面的时间默认为北京,纽约,东京的时间,下拉选择其他地方时间");//添加标签
label5 = new JLabel("同一个世界,同一个梦想",JLabel.CENTER);
label1.setFont(new Font("Dialog", Font.BOLD, 12));//设置字体
label2.setFont(new Font("Dialog", Font.BOLD, 12));
label3.setFont(new Font("Dialog", Font.BOLD, 12));
combo1 = new JComboBox(TimeZone.getAvailableIDs());//下拉组件
combo2 = new JComboBox(TimeZone.getAvailableIDs());
combo3 = new JComboBox(TimeZone.getAvailableIDs());
Clock();
add(label4);
add(label1);
add(combo1);
add(label2);
add(combo2);
add(label3);
add(combo3);
add(label5);
JComboBoxAction combo11Action = new JComboBoxAction(1);
JComboBoxAction combo12Action = new JComboBoxAction(2);
JComboBoxAction combo13Action = new JComboBoxAction(3);
combo1.addActionListener(combo11Action);//监听下拉组件
combo2.addActionListener(combo12Action);
combo3.addActionListener(combo13Action);
}
private class JComboBoxAction implements ActionListener {//实现下拉组件监听
int Type = 0;
public JComboBoxAction(int type) {
Type = type;
}
public void actionPerformed(ActionEvent event) {
if(Type == 1){
timeZone1 = (String)combo1.getSelectedItem();
} else if(Type == 2){
timeZone2 = (String)combo2.getSelectedItem();
} else if(Type == 3){
timeZone3 = (String)combo3.getSelectedItem();
}
}
}
private void Clock() {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置时间输出格式
sd.setTimeZone(TimeZone.getTimeZone(timeZone1));//初始化标签
String strDate = sd.format(new Date());
label1.setText(strDate);
sd.setTimeZone(TimeZone.getTimeZone(timeZone2));
strDate = sd.format(new Date());
label2.setText(strDate);
sd.setTimeZone(TimeZone.getTimeZone(timeZone3));
strDate = sd.format(new Date());
label3.setText(strDate);
}
public void run() {//实现方法
while (true) {
Clock();//调用函数
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
}
}
第三个类
package aa;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.*;
import
import
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
@SuppressWarnings("serial")
public class SetClock extends JFrame implements Runnable {
JLabel ri, shi, fen, miao, dangqian, guanji;
JButton queding, dakai;
JTextField music, RI, SHI, FEN, MIAO;
int h = 0, f = 0, m = 0, r = 0;
boolean fo = false, foo = false;
public AudioClip soumd1;
int riqi, shizhong, fenzhong, miaozhong;
public SetClock() {
Container c = getContentPane();
c.setLayout(new GridLayout(4, 1));
JPanel jp = new JPanel();
dangqian = new JLabel();
jp.add(dangqian);
c.add(jp);
JPanel jp3 = new JPanel();
guanji = new JLabel("离闹铃时间:0日0时0分0秒");
jp3.add(guanji);
c.add(jp3);
JPanel jp1 = new JPanel();
music = new JTextField(20);
dakai = new JButton("选择闹铃音乐");
jp1.add(music);
jp1.add(dakai);
c.add(jp1);
ri = new JLabel("日");
RI = new JTextField(4);
shi = new JLabel("时");
SHI = new JTextField(4);
fen = new JLabel("分");
FEN = new JTextField(4);
miao = new JLabel("秒");
MIAO = new JTextField(4);
JPanel jp2 = new JPanel();
jp2.add(ri);
jp2.add(RI);
jp2.add(shi);
jp2.add(SHI);
jp2.add(fen);
jp2.add(FEN);
jp2.add(miao);
jp2.add(MIAO);
queding = new JButton("确定");
jp2.add(queding);
c.add(jp2);
setSize(400, 160);
setVisible(true);
dakai.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fileChooser = new JFileChooser(); // 实例化文件选择器
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 设置文件选择模式,此处为文件和目录均可
fileChooser.setCurrentDirectory(new File(".")); // 设置文件选择器当前目录
fileChooser .setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File file) { // 可接受的文件类型
String name = file.getName().toLowerCase();
return name.endsWith(".wav")|| name.endsWith(".au")|| file.isDirectory();
}
public String getDescription() { // 文件描述
return "音乐文件(*.wav,*.au)";
}
});
if (fileChooser.showOpenDialog(SetClock.this) == JFileChooser.APPROVE_OPTION) { // 弹出文件选择器,并判断是否点击了打开按钮
String fileName = fileChooser.getSelectedFile().getAbsolutePath(); // 得到选择文件或目录的绝对路径
music.setText(fileName);
}
}
});
queding.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (queding.getText().equals("确定")) {
try
{
r = Integer.parseInt(RI.getText());
h = Integer.parseInt(SHI.getText());
f = Integer.parseInt(FEN.getText());
m = Integer.parseInt(MIAO.getText());
if (1 <= r && r<= 31 && 0 <= h && h <= 23 && 0 <= f && f <= 59 && 0 <= m && m <= 59)
{
Date now = new Date();
SimpleDateFormat ri = new SimpleDateFormat("dd");
if (RI.getText() == null || RI.getText().equals(""))
{
r = Integer.parseInt(ri.format(now));
RI.setText(ri.format(now));
}
else
r = Integer.parseInt(RI.getText());
fo = true;
}
else
JOptionPane.showMessageDialog(null, "输入时间错误");
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null," 请输入正确的时间");
}
}
else
{
try
{
fo = false;
RI.setEditable(true);
SHI.setEditable(true);
FEN.setEditable(true);
MIAO.setEditable(true);
queding.setText("确定");
soumd1.stop();
}
catch(Exception E)
{
JOptionPane.showMessageDialog(null," 已关闭!");
}
}
}
});
}
public void Set()
{
//SetClock s = new SetClock();
Thread t1 = new Thread(this);
t1.start();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@SuppressWarnings("deprecation")
public void run() {
while(true){
Date now = new Date();
dangqian.setText("当前时间: " + now.toString());
if (foo)
{
if (h == 0||h - shizhong<0)
{
h += 24;
r = r - 1;
}
if (f == 0||f - fenzhong<0)
{
f += 60;
h -= 1;
}
if (m == 0||m - miaozhong<0)
{
m += 60;
f -= 1;
}
guanji.setText("离闹铃时间: " + (r - riqi) + "日" + (h - shizhong)+ "时" + (f - fenzhong) + "分" + (m - miaozhong-1) + "秒");
if(r<riqi)
{
foo=false;
guanji.setText("离闹铃时间:0日0时0分0秒");
}
else if(r<=riqi&&h<shizhong)
{
foo=false;
guanji.setText("离闹铃时间:0日0时0分0秒");
}
else if(r<=riqi&&h<=shizhong&&f<fenzhong)
{
foo=false;
guanji.setText("离闹铃时间:0日0时0分0秒");
}
else if(r<=riqi&&h<=shizhong&&f<=fenzhong&&m<miaozhong)
{
foo=false;
guanji.setText("离闹铃时间:0日0时0分0秒");
}
}
if (fo)
{
foo = true;
RI.setEditable(false);
SHI.setEditable(false);
FEN.setEditable(false);
MIAO.setEditable(false);
queding.setText("关闭");
SimpleDateFormat ri = new SimpleDateFormat("dd"); // 封装为了获取日期
SimpleDateFormat shi = new SimpleDateFormat("HH"); // 封装为了获取小时
SimpleDateFormat fen = new SimpleDateFormat("mm"); // 封装为了获取分钟
SimpleDateFormat miao = new SimpleDateFormat("ss"); // 封装为了获取秒钟
riqi = Integer.parseInt(ri.format(now)); //获取日期
shizhong = Integer.parseInt(shi.format(now)); // 获取小时
fenzhong = Integer.parseInt(fen.format(now)); // 获取分钟
miaozhong = Integer.parseInt(miao.format(now)); // 获取秒钟
if (riqi == r && shizhong == h && fenzhong == f && miaozhong == m) // 判断条件
{
try
{
soumd1 = Applet.newAudioClip(new File(music.getText())
.toURL()); // 播放音乐
soumd1.loop(); // 我设置的是循环播放..这样不起床都不行..
fo = false;
foo = false;
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
}
}
try
{
Thread.sleep(1000);
}
catch (InterruptedException ie) {
}
}
}
}
第二个包运行程序
package pack1;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Calendar;
import java.util.Date;
import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import aa.Lunar;
public class MainFrame extends JFrame implements ItemListener//建立主界面
{
private ImageIcon background;
Image picture = null;
JPanel panel=new JPanel(new BorderLayout());//建立中层容器并布局
JPanel panel1=new JPanel();
JPanel panel2=new JPanel(new GridLayout(8,7));
JPanel panel3=new JPanel(new GridLayout(5,1));
JPanel panel4=new JPanel();
JLabel[] label=new JLabel[49];//标签
JLabel y_label=new JLabel("年");
JLabel m_label=new JLabel("月");
JLabel chen =new JLabel("时间总是在不经意间溜走,我们还得向前走",JLabel.CENTER);
JLabel xu =new JLabel("希望在回首时候,不带任何遗憾!",JLabel.CENTER);
JLabel wang =new JLabel("新年快乐~~~!",JLabel.CENTER);
JComboBox com1=new JComboBox();//下拉窗口
JComboBox com2=new JComboBox();
JButton but1=new JButton("闹钟"); //按钮
JButton but3=new JButton("时钟"); //按钮
JButton but2=new JButton("世界时间");
Calendar now=Calendar.getInstance();
public MainFrame ()//设置主界面,并添加各组件
{
super("筱筱万年历");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.add(but1);
panel1.add(but3);
panel1.add(y_label);
panel1.add(com1);
panel1.add(m_label);
panel1.add(com2);
panel1.add(but2);
panel3.add(chen);
panel3.add(xu);
panel3.add(wang);
for(int i=0;i<49;i++)//日期标签
{
label[i]=new JLabel(" ",JLabel.CENTER);
panel2.add(label[i]);
}
panel.add(panel1,BorderLayout.NORTH);//中层容器布局
panel.add(panel2,BorderLayout.CENTER);
panel.add(panel3,BorderLayout.SOUTH);
panel1.setBackground(new Color(0, 0, 0, 0));
panel2.setBackground(new Color(0, 0, 0, 0));
panel3.setBackground(new Color(0, 0, 0, 0));
setContentPane(panel);
setVisible(true);
init();
IS();
com1.addItemListener(this);//对下拉组件监听
com2.addItemListener(this);
but1.addActionListener(new ActionListener() {//按钮动作监听
public void actionPerformed(ActionEvent e) {
new aa.SetClock(); //闹钟类
}
});
but2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new aa.national();//世界时间类
}
});
but3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new shizhong.Clock();
}
});
}
public void IS()//设置日期标签函数
{
background = new ImageIcon("8.jpg");//背景图片
JLabel a = new JLabel(background);//把背景图片显示在一个标签里面
//把标签的大小位置设置为图片刚好填充整个面板
a.setBounds(0,0,background.getIconWidth(),background.getIconHeight());//把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
panel = (JPanel)this.getContentPane();
panel.setOpaque(false);//内容窗格默认的布局管理器为BorderLayout
this.getLayeredPane().setLayout(null);//把背景图片添加到分层窗格的最底层作为背景
this.getLayeredPane().add(a,new Integer(Integer.MIN_VALUE));
panel.setLayout(new FlowLayout());
}
public void init()//设置日期标签函数
{
int year,month,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(Color.blue);
}
for(int i=6;i<49;i=i+7)
{
label[i].setForeground(Color.blue);//将星期六设置为红色
}
for(int i=1800;i<=2100;i++)//将年份的下拉设置为1800-2100年
{
com1.addItem(""+i);
}
for(int i=1;i<13;i++)//讲月份下拉设置为12月
{
com2.addItem(""+i);
}
first_day_num=(int)(now.get(Calendar.DAY_OF_WEEK));//获得日期
month=(int)(now.get(Calendar.MONTH));//获得月份
year=(int)(now.get(Calendar.YEAR));//获得年份
com1.setSelectedIndex(year-1800);//将年份设置为当前年份
com2.setSelectedIndex(month);//将月份设置为当前月
Resetday(first_day_num,year,month);//调用Resetday函数
}
public int use(int year2,int month2)//use函数
{
int first_day;
now.set(year2, month2,1);
first_day=(int)(now.get(Calendar.DAY_OF_WEEK));
return first_day;
}
public void stateChanged()
{
int year3,month3,week3;
year3=Integer.parseInt(com1.getSelectedItem().toString());
month3=Integer.parseInt(com2.getSelectedItem().toString())-1;
week3=use(year3,month3);
Resetday(week3,year3,month3);//调用函数
}
@SuppressWarnings({ "unused", "deprecation" })
public void Resetday(int week_log, int year_log, int month_log) {//Resetday函数
int month_day_score; // 存储月份的天数
int count;
Lunar lunar;//定义lunar
int month_day;
String[] LunarDate=new String[49];//农历标签
month_day_score = 0;
count = 1;
for (int i = 0; i < 49; i ++) {//将农历跟公历一起显示
for(int j=0;j<49;j=j+7)
{
if(i!=j&&i!=j+6)
label[i].setForeground(Color.black);
}
for(int j=0;i<49;i=i+7)
{
label[i].setForeground(Color.blue);
}
for(int j=6;i<49;i=i+7)
{
label[i].setForeground(Color.blue);
}
}
Date date = new Date(year_log, month_log + 1, 1); // now MONTH是从0开始的, 对于一月第几天来说,DAY_OF_MONTH第一天就是1. 对于一年第几个月来说,MONTH一月份是0,二月份是1...
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, -1); // 前个月
month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天
month_day=month_day_score;
for (int i = 7; i < 49; i++) { // 初始化标签
label[i].setText("");
}
week_log = week_log + 6; // 将星期数加6,使显示正确
month_day_score = month_day_score + week_log;
lunar=new aa.Lunar();//调用Lunar类
for(int i=0;i<month_day;i++)
{
LunarDate[i]=lunar.getLunarDate( year_log, month_log+1, i+1);//农历的实现
}
for (int i = week_log; i < month_day_score; i++, count++) {//一些假期
if(month_log==9&&count==1)
{
label[i].setText(count +"国庆" );
label[i].setForeground(Color.red);
}
else if(month_log==0&&count==1)
{
label[i].setText(count +"元旦" );
label[i].setForeground(Color.red);
}
else if(month_log==11&&count==24)
{
label[i].setText(count +"平安夜" );
label[i].setForeground(Color.red);
}
else if(month_log==11&&count==25)
{
label[i].setText(count +"圣诞" );
label[i].setForeground(Color.red);
}
else if(month_log==1&&count==14)
{
label[i].setText(count +"情人节" );
label[i].setForeground(Color.red);
}
else if(month_log==4&&count==1)
{
label[i].setText(count +"劳动节" );
label[i].setForeground(Color.red);
}
else if(LunarDate[i-week_log].equals("春节")||LunarDate[i-week_log].equals("元宵")||LunarDate[i-week_log].equals("端午")||LunarDate[i-week_log].equals("中秋"))
{
label[i].setText("\n"+count +LunarDate[i-week_log] );
label[i].setForeground(Color.red);
}
else
label[i].setText(count +LunarDate[i-week_log] );
}
}
public void itemStateChanged(ItemEvent e)
{
stateChanged();//调用函数
}
public static void main(String [] args)//主函数
{
new MainFrame();
}
}