用java代码做一个日历 java编写一个日历
用java做一个日历显示数据功能,在页面上显示日期数据。
MainFrame.java是显示日历程序,Clock.java是日历计算程序(可以不孝历要)。
创新互联是一家集网站建设、成都网站制作、网站页面设计、网站优化SEO优化为一体的专业网站制作公司,已为成都等多地近百家企业提供网站建设服务。追求良好的浏览体验,以探求精品塑造与理念升华,设计最适合用户的网站页面。 合作只是第一步,服务才是根本,我们始终坚持讲诚信,负责任的原则,为您进行细心、贴心、认真的服务,与众多客户在蓬勃发展的市场环境中,互促共生。
编译后运行MainFrame这个类即可。
swing窗口显示万年历,jdk1.4以上环境编译运行。
package org.test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.Calendar;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainFrame extends JFrame {
private static final long serialVersionUID = -306484324260972141L;
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, x_size, y_size;
String year_num;
Calendar now = 卖腔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 (int i = 0; i 49; i++) {
巧配搜label[i] = new JLabel("", JLabel.CENTER);// 将显示的字符设置为居中
panel2.add(label[i]);
}
panel3.add(new Clock(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(new ClockAction());
com2.addActionListener(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 = 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
}
}
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(Color.red); // 将星期日的日期设置为红色
}
for (int i = 6; i 49; i = i + 7) {
label[i].setForeground(Color.green);// 将星期六的日期设置为绿色
}
for (int i = 1; i 10000; i++) {
com1.addItem("" + i);
}
for (int i = 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);
}
public int use(int reyear, int remonth) {
int week_num;
now.set(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天
week_num = (int) (now.get(Calendar.DAY_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 = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, -1); // 前个月
month_day_score = cal.getActualMaximum(Calendar.DAY_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) {
JFrame.setDefaultLookAndFeelDecorated(true);
new MainFrame();
}
}
package org.test;
import java.awt.Color;
import java.util.Calendar;
import java.awt.Canvas;
import java.awt.Font;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
public class Clock extends Canvas implements Runnable{
private static final long serialVersionUID = 3660124045489727166L;
MainFrame mf;
Thread t;
String time;
public Clock(MainFrame mf){
this.mf=mf;
setSize(280,40);
setBackground(Color.white);
t=new Thread(this);//实例化线程
t.start();
//调用线程
}
public void run(){
while(true){
try{
Thread.sleep(1000);//休眠1秒钟
}catch(InterruptedException e){
System.out.println("异常");
}
this.repaint(100);
}
}
public void paint(Graphics g){
Font f=new Font("宋体",Font.BOLD,16);
SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化时间显示类型
Calendar now=Calendar.getInstance();
time=SDF.format(now.getTime()); //得到当前日期和时间
g.setFont(f);
g.setColor(Color.orange);
g.drawString(time,45,25);
}
}
如何用java做日历
看看下面代码:
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import javax.swing.JOptionPane;
class CalendarBean
{
String day[];
int year=2011,month=0;
public void setYear(int year)
{ this.year=year;
}
public int getYear()
{ return year;
}
public void setMonth(int month)
{ this.month=month;
}
public int getMonth()
{ return month;
}
public String[] getCalendar()
{ String a[]=new String[42];
Calendar 日历=Calendar.getInstance();
日历.set(year,month-1,1);
int 星期几=日历.get(Calendar.DAY_OF_WEEK)-1;
int day=0;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{ day=31;
}
if(month==4||month==6||month==9||month==11)
{ day=30;
}
if(month==2)
{ if(((year%4==0)(year%100!=0))||(year%400==0))
{ day=29;
}
else
{ day=28;
}
}
for(int i=星期几,n=1;i星期几+day;i++)
{
a[i]=String.valueOf(n) ;
n++;
}
return a;
}
}
class CalendarFrame extends Frame implements ActionListener
{
Label labelDay[]=new Label[42];
Button titleName[]=new Button[7];
String name[]={"日","一","二","三", "四","五","六"};
TextField text1,text2;Button nextMonth,previousMonth,Enter;Label lab1,lab2,lab3;
int year=2012,month=5;
CalendarBean calendar;
Label showMessage=new Label("",Label.CENTER);
public CalendarFrame()
{ Panel pCenter=new Panel();
pCenter.setLayout(new GridLayout(7,7));
for(int i=0;i7;i++)
{ titleName[i]=new Button(name[i]);
pCenter.add(titleName[i]);
}
for(int i=0;i42;i++)
{
labelDay[i]=new Label("",Label.CENTER);
pCenter.add(labelDay[i]);
}
calendar=new CalendarBean();
calendar.setYear(year);
calendar.setMonth(month);
String day[]=calendar.getCalendar();
for(int i=0;i42;i++)
{ labelDay[i].setText(day[i]);
}
lab1=new Label("请输入日期");
lab2=new Label("年份");
lab3=new Label("月份");
Enter=new Button("确定");
text1=new TextField(10);
text2=new TextField(5);
nextMonth=new Button("下月");
previousMonth=new Button("上月");
Enter.addActionListener(this);
nextMonth.addActionListener(this);
previousMonth.addActionListener(this);
Panel pNorth=new Panel(),
pSouth=new Panel();
pNorth.add( lab1);
pNorth.add(lab2);
pNorth.add( text1);
pNorth.add(lab3);
pNorth.add(text2);
pNorth.add(Enter);
pNorth.add(previousMonth);
pNorth.add(nextMonth);
pSouth.add(showMessage);
showMessage.setText("日历:"+calendar.getYear()+"年"+ calendar.getMonth()+"月" );
ScrollPane scrollPane=new ScrollPane();
scrollPane.add(pCenter);
add(scrollPane,BorderLayout.CENTER);
add(pNorth ,BorderLayout.NORTH);
add(pSouth ,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==nextMonth)
{ month=month+1;
if(month12)
month=1;
calendar.setMonth(month);
String day[]=calendar.getCalendar();
for(int i=0;i42;i++)
{ labelDay[i].setText(day[i]);
}
}
else if(e.getSource()==previousMonth)
{ month=month-1;
if(month1)
month=12;
calendar.setMonth(month);
String day[]=calendar.getCalendar();
for(int i=0;i42;i++)
{ labelDay[i].setText(day[i]);
}
}else {
String yea=text1.getText();
String mon=text2.getText();
try{
year=Integer.parseInt(yea);
month=Integer.parseInt(mon);
if(month12||month1||year1){
JOptionPane.showMessageDialog(null, "请输入正确月份或月份");
return;
}
else{
calendar.setYear(year);
calendar.setMonth(month);
}
String day[]=calendar.getCalendar();
for(int i=0;i42;i++)
{
labelDay[i].setText(day[i]);
}
}catch(NumberFormatException ee){
JOptionPane.showMessageDialog(null, "请输入正确的年份及月份");
}
}
showMessage.setText("日历:"+calendar.getYear()+"年"+calendar.getMonth()+"月" );
}
}
public class CalendarMainClass
{ public static void main(String args[])
{ CalendarFrame frame=new CalendarFrame();
frame.setTitle("日历");
frame.setBounds(300,200,500,300);
frame.setVisible(true);
frame.validate();
frame.addWindowListener(new java.awt.event.WindowAdapter()
{ public void windowClosing(java.awt.event.WindowEvent e)
{ System.exit(0);
}
}
);
}
}
有问题就追问。满意请采纳。
java编写日历
我给你贴上我在java核心技术中看到的代码吧,当然没有输入年份和月份,是按照当前时间创建的,写有我写的注释,应该能看的懂
/*
* 2012年5月13日10:37:58
* 日历程序
* Function:
* 显示当前月份的日历
* 总结
* 1. 0-11分别代表1-12月
* 1-7分别代表周日-周六
* 2. 使用GregorianCalendar对象的get方法(参数)获取月,日,年等信息
* 3.
*/
import java.text.DateFormatSymbols;
import java.util.*;
public class CalendarTest {
public static void main(String[] args) {
//construct d as current date构造一个日期
GregorianCalendar d = new GregorianCalendar();
//获取今天是这个月的第几天
int today = d.get(Calendar.DAY_OF_MONTH); //Calendar.DAY_OF_MONTH作为参数调用,得到今天是这个月的第几天
int month = d.get(Calendar.MONTH); //月份
d.set(Calendar.DAY_OF_MONTH, 1); //设置d的日期是本月的1号
int weekDay = d.get(Calendar.DAY_OF_WEEK); //获取当天位于本星期的第几天,也就确定了星期几,值的范围是1-7
int firstDayOfWeek = d.getFirstDayOfWeek(); //获取一星期的第早坦一天,我们得到的是Calendar.SUNDAY,因为我们一星期的第一天是周日
int indent = 0; //为了定位本月第一天,定义索引
while (weekDay != firstDayOfWeek) {
//注意,月份用0-11代表1-12月,为了清晰起见,使用常量代替,下面宽迟获取月份得到的实际是当前月-1的值,所以我们要加1
//System.out.printf("当前星期的第%d天,位于当月的第%d天, 现在是%d月\n",
// weekDay, d.get(Calendar.DAY_OF_MONTH), d.get(Calendar.MONTH)+1); //Test Code
indent++;//陆巧桐缩进个数+1
d.add(Calendar.DAY_OF_MONTH, -1);//当前天数-1,如果现在是1号,则执行本条代码后,时间变为上一个月最后一天
weekDay = d.get(Calendar.DAY_OF_WEEK); //重新获取当天位于本星期的第几天
}
String[] weekDayNames = new DateFormatSymbols().getShortWeekdays();//获取简短形式的星期字符串数组
//System.out.println(weekDayNames.length);getShortWeekdays()得到的数组的长度是8,下标为0的是没有值1为星期日...7为星期六
//注释代码1
//Java核心技术的代码
/*
do {
//System.out.printf("%4s", weekDayNames[weekDay]); //经过上面定义索引,weekDay代表的是本星期日
d.add(Calendar.DAY_OF_MONTH, 1); //天数加1
weekDay = d.get(Calendar.DAY_OF_WEEK); //重新获得weekDay的值
} while (weekDay != firstDayOfWeek); //当循环完一个星期后,这里判断不成立,退出循环
*/
//我写的代码,替换上面注释代码1
for (int i=1; iweekDayNames.length; i++)//打印星期标题
System.out.printf("%3s\t", weekDayNames[i]);//引号内是一个全角的空格,因为是中文版,不是书上英文环境,中文和空格对于不上,这里我们用\t解决
//System.out.printf("%3s ", weekDayNames[i]); //方式2
System.out.println();//换行
for (int i=1; i=indent; i++)//确定一星期的一天位置,利用上面indent
System.out.printf("\t");//如用方式2,则这里内容是四个全角空格
//实现输出日期
d.set(Calendar.MONTH, month);
d.set(Calendar.DAY_OF_MONTH, 1);
do {
//print day
int day = d.get(Calendar.DAY_OF_MONTH);
System.out.printf("%3d", day);
if (day == today)
System.out.print("*");
System.out.print("\t");
d.add(Calendar.DATE, 1);//天数加1
weekDay = d.get(Calendar.DAY_OF_WEEK);//刷新weekDay
if (weekDay == firstDayOfWeek) //如果这天等于星期天则换行
System.out.println();
} while (d.get(Calendar.MONTH) == month);
}
}
网页标题:用java代码做一个日历 java编写一个日历
网站URL:http://azwzsj.com/article/ddpdjhd.html