万年历java源代码 java万年历实验原理
求一个java swing带界面的万年历代码
按照兆穗高你的要求编写的Java swing 带界面的万年历代码如下
成都创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站制作、做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的万秀网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
//日历
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class CCI extends JFrame implements ActionListener{
JButton jb1=new JButton("");
JButton jb2=new JButton("");
JButton jb3=new JButton("");
JButton jb4=new JButton("");
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();
JPanel jp4=new JPanel();
JLabel jl1=new JLabel();
JLabel jl2=new JLabel();
JLabel[]jl=new JLabel[49];
String []week={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
Calendar c=Calendar.getInstance();
int year,month,day;
int nowyear,nowmonth,nowday;
CCI(){
super("简单日历");
nowyear=c.get(Calendar.YEAR);
nowmonth=c.get(Calendar.MONTH)+1;
nowday=c.get(Calendar.DAY_OF_MONTH);
year=nowyear;
month=nowmonth;
day=nowday;
String s=year+"年"+month+"月";
jl1.setForeground(Color.RED);
jl1.setFont(new Font(null,Font.BOLD,20));
jl1.setText(s);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
族野 jp1.add(jb1);jp1.add(jb2);jp1.add(jl1);jp1.add(jb3);jp1.add(jb4);
jp2.setLayout(null);
createMonthPanel();
jp2.add(jp3);
jl2.setFont(new Font(null,Font.BOLD,20));
jl2.setText("今天是"+nowyear+"年"+nowmonth+"月"+nowday+"日");
jp4.add(jl2);
add(jp1,BorderLayout.NORTH);
add(jp2,BorderLayout.CENTER);
族尺 add(jp4,BorderLayout.SOUTH);
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==jb1){
year=year-1;
String s=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb2){
if(month==1){
year=year-1;
month=12;
}else{
month=month-1;
}
String s=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb3){
if(month==12){
year=year+1;
month=1;
}else{
month=month+1;
}
String s=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb4){
year=year+1;
String s=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
}
public static void main(String[] args) {
new CCI();
}
public int getMonthDays(int year, int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 2:
if ((year%4==0year%100!=0)||year%400==0) {
return 29;
} else {
return 28;
}
default:
return 30;
}
}
public void createMonthPanel(){
c.set(year, month-1, getMonthDays(year,month));
int weekOfMonth=c.get(Calendar.WEEK_OF_MONTH);
if(weekOfMonth==6){
jp3.setLayout(new GridLayout(7,7));
jp3.setBounds(50, 20, 420, 350);
}else{
jp3.setLayout(new GridLayout(6,7));
jp3.setBounds(50, 20, 420, 300);
}
jp3.setBorder(BorderFactory.createEtchedBorder());
for(int i=0;i7;i++){
jl[i]=new JLabel(week[i],JLabel.CENTER);
jl[i].setFont(new Font(null,Font.BOLD,20));
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
c.set(year, month-1, 1);
int emptyFirst=c.get(Calendar.DAY_OF_WEEK)-1;
int daysOfMonth=getMonthDays(year,month);
for(int i=6+emptyFirst;i=7;i--){
int intyear=year;
int intmonth=month;
if(intmonth==1){
intyear=intyear-1;
intmonth=12;
}else{
intmonth=intmonth-1;
}
int intdays=getMonthDays(intyear,intmonth);
jl[i]=new JLabel((intdays+7-i)+"",JLabel.CENTER);
jl[i].setFont(new Font(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
for(int i=7+emptyFirst;idaysOfMonth+7+emptyFirst;i++){
jl[i]=new JLabel((i-7-emptyFirst+1)+"",JLabel.CENTER);
jl[i].setFont(new Font(null,Font.BOLD,20));
if((i+1)%7==0 || (i+1)%7==1){
jl[i].setForeground(Color.RED);
}else if((i-7-emptyFirst+1)==nowdaymonth==nowmonthyear==nowyear)
jl[i].setForeground(Color.BLUE);
else
jl[i].setForeground(Color.BLACK);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
if(weekOfMonth==6)
for(int i=48;i=daysOfMonth+emptyFirst+7;i--){
jl[i]=new JLabel((49-i)+"",JLabel.CENTER);
jl[i].setFont(new Font(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
else
for(int i=41;i=daysOfMonth+emptyFirst+7;i--){
jl[i]=new JLabel((42-i)+"",JLabel.CENTER);
jl[i].setFont(new Font(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
}
}
我有万年历和时钟的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;m12;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;i7;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;i42;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;i7;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;i12;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;i42;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;i7;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;i12;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;iinitWeek+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;i42;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博客,转载请标明出处:
怎么用java for循环打印万年历基础源代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class CalenderTrain extends JFrame implements ActionListener {
JComboBox Month = new JComboBox(); //月份下拉列表框
JComboBox Year = new JComboBox(); //年份羡磨高下拉列表框
JLabel Year_l = new JLabel("Year::"); //定义标签
JLabel Month_l = new JLabel("Month::"); //定义标签
Date now_date = new Date(); //获取今天的日期
JButton[] button_day = new JButton[49]; //定义一个数组用来存放日期
JButton button_ok = new JButton("Enter"); //现实选择日期
JButton button_today = new JButton("Today"); //显示今天按钮
int now_year = now_date.getYear() + 1900; //获取年份值
int now_month = now_date.getMonth(); //获取月份值(当前月份-1)
String year_int = null; //存放年份
int month_int; //存放月份
JPanel pane_ym = new JPanel(); //放置下拉列表框和控制按钮面板
JPanel pane_day = new JPanel(); //放置日期面板
JPanel pane_parent = new JPanel(); //放兄尺置以上两个面板
//定义方法绘制面板
public CalenderTrain() {
super("Calender!"); //设定面板得title
//---以下几行使得关闭面板时退出程序
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClose(WindowEvent e) {
System.exit(0);
}
});
//游判---
setResizable(false); //面板的大小不能变化
//设定年月
/*年份的区间是当前年份的过去10年到当前年份的未来20年
* 月份正常1??12月
*/
for (int i = now_year - 10; i = now_year + 20; i++) {
Year.addItem(i + "");
}
for (int i = 1; i 13; i++) {
Month.addItem(i + "");
}
Year.setSelectedIndex(10);//设定年份下拉列表为当前年份
pane_ym.add(Year_l);//添加年份标签
pane_ym.add(Year);//添加年份下拉列表框
Month.setSelectedIndex(now_month);//设定月份下拉列表为当前月份
pane_ym.add(Month_l);//添加月份标签
pane_ym.add(Month);//添加月份下拉列表框
pane_ym.add(button_ok);//添加确定按钮
pane_ym.add(button_today);//添加“今天”按钮
button_ok.addActionListener(this);//确定按钮添加监听事件
button_today.addActionListener(this);//“今天”按钮添加监听事件
//年月设定结束
//初始化日期按钮并绘制
pane_day.setLayout(new GridLayout(7, 7, 10, 10));
for (int i = 0; i 49; i++) {
button_day[i] = new JButton(" ");
pane_day.add(button_day[i]);
}
this.setDay();//调用setDay()方法
pane_parent.setLayout(new BorderLayout());//设定布局管理器
setContentPane(pane_day);
setContentPane(pane_ym);
pane_parent.add(pane_day, BorderLayout.SOUTH);
pane_parent.add(pane_ym, BorderLayout.NORTH);
setContentPane(pane_parent);
pack();
show();
}
void setDay() {
year_int = Year.getSelectedItem().toString();
month_int = Month.getSelectedIndex();
int year_sel = Integer.parseInt(year_int) - 1900;//获得年份值
Date dt = new Date(year_sel, month_int, 1);//构造一个日期
GregorianCalendar cal = new GregorianCalendar();//创建一个Calendar实例
cal.setTime(dt);
String week[] = { "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" };
int day = 0;//day中存放某个月份的天数
int day_week = 0;//用来存放某个月的第一天是星期几的数值
//--将星期添加到前7个按钮中
for (int i = 0; i 7; i++) {
button_day[i].setText(week[i]);
}
//--
/*判断是几月份,根据它来设定day的值
* 其中二月份要判断是否是闰年
*/
if (month_int == 0
|| month_int == 2
|| month_int == 4
|| month_int == 6
|| month_int == 7
|| month_int == 9
|| month_int == 11) {
day = 31;
} else if (
month_int == 3
|| month_int == 5
|| month_int == 8
|| month_int == 10) {
day = 30;
} else {
if (cal.isLeapYear(year_sel)) {
day = 29;
} else {
day = 28;
}
}
day_week = 7 + dt.getDay();
int count = 1;
/*绘制按钮
* 在这里我们首先要根据选定的月份的第一天是星期几来确定我们绘制按钮的起始位置
* 其中day_week就是我们要绘制的起始位置
* 对于那些没有数值可以显示的按钮要置空
*/
for (int i = day_week; i day_week + day; count++, i++) {
if (i % 7 == 0
|| i == 13
|| i == 20
|| i == 27
|| i == 48
|| i == 34
|| i == 41) {
button_day[i].setForeground(Color.RED);
button_day[i].setText(count + "");
} else {
button_day[i].setText(count + "");
}
}
//--对于没有日期数值显示的按钮进行置空处理
if (day_week == 0) {
for (int i = day; i 49; i++) {
button_day[i].setText(" ");
}
} else {
//第一天前面的按钮置空
for (int i = 7; i day_week; i++) {
button_day[i].setText(" ");
}//最后一天后面的按钮置空
for (int i = day_week + day; i 49; i++) {
button_day[i].setText(" ");
}
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button_ok) {
this.setDay();//如果点击确定按钮就调用setDay()重新方法绘制按钮
} else if (e.getSource() == button_today) {
new CalenderTrain();//如果点击今天按钮,得到今天的日期
}
}
public static void main(String[] args) {
CalenderTrain ct = new CalenderTrain();
}
}
网站题目:万年历java源代码 java万年历实验原理
URL分享:http://scyanting.com/article/ddpgoph.html