日历程序java代码 java编程实现日历记事本

求Java 日历的小程序的源代码也不知道你具体需求是什么,以前改过一个日历程序,一共四个java类,放在同一个包里 。经测试可以运行 。
//Start.java
import java.awt.*;
import javax.swing.*;
class Start{
public static void main(String [] args){
DateFrame frame=new DateFrame();
frame.setLocationRelativeTo(frame);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
//DateInfo.java
import java.util.*;
public class DateInfo{
private int mYear, mMonth;
private int mDayOfMonth, mFristWeek;
public DateInfo(int year, int month) throws DateException{
mYear = year;
if (month0 || month12){
throw (new DateException());
}
mMonth = month;
mDayOfMonth = getDayOfMonth(mYear, mMonth);
mFristWeek = getFristWeek(mYear, mMonth);
}
private int getDayOfMonth(int year, int month){
int[][] ary = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
return (ary[isLeapYear(year)][month]);
}
private int isLeapYear(int year){
if (year % 4 == 0year % 100 != 0 ||year % 400 == 0){
return (1);
}
else{
return (0);
}
}
private int getFristWeek(int year, int month){
java.util.Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, 1);
return (cal.get(Calendar.DAY_OF_WEEK) - 1);
}
public String toString(){
String str;
str = "\t\t"mYear"年"mMonth"月\n";
str= "日\t一\t二\t三\t四\t五\t六\n";
int i;
for (i = 1; i = mFristWeek; i){
str= " \t";
}
for (int j = 1; j = mDayOfMonth; j, i){
str=j "\t";
if (i % 7 == 0){
str= "\n";
}
}
return (str);
}
}
//DateFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;
class DateFrame extends JFrame implements Runnable{
Calendar date=Calendar.getInstance();
String[] str={"1","2","3","4","5","6","7","8","9","10","11","12"};
JLabel lblYear=new JLabel("年 ");
JLabel lblMonth=new JLabel("月 ");
JLabel lblDate=new JLabel("现在的时间是:");
JLabel lblShowDate=new JLabel();
// javax.swing.JTextField trxt =new JTextField(10);
// trxt.setHorizontalAlignment(JTextField.RIGHT);//设置文本从右边输入
JComboBox cboMonth=new JComboBox(str);
JComboBox cboYear=new JComboBox();
JTextArea txaShow=new JTextArea();
JPanel pnlNorth=new JPanel();
JPanel pnlSOUTH=new JPanel();
JButton btnShow=new JButton("显示");
JButton btnClose=new JButton("关闭");
JScrollPane jsp=new JScrollPane(txaShow);
Container c=this.getContentPane();
public DateFrame(){
Thread thread=new Thread(this);
thread.start();
this.setTitle("玩玩日历拉!!!");
this.setSize(300,260);
for (int i = 1990; i=2025; i) {
cboYear.addItem("" i);
}
cboYear.setSelectedItem("" (date.get(Calendar.YEAR)));
cboMonth.setSelectedItem("" (date.get(Calendar.MONTH) 1));
pnlNorth.add(cboYear);
txaShow.setTabSize(4);//设置tab键的距离
txaShow.setForeground(Color.GREEN);
pnlNorth.add(lblYear);
pnlNorth.add(cboMonth);
pnlNorth.add(lblMonth);
pnlNorth.add(lblDate);
pnlNorth.add(lblShowDate);
c.add(pnlNorth,BorderLayout.NORTH);
c.add(jsp);
pnlSOUTH.add(btnShow);
pnlSOUTH.add(btnClose);
c.add(pnlSOUTH,BorderLayout.SOUTH);
btnShow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int year=Integer.parseInt((String)cboYear.getSelectedItem());
int month=Integer.parseInt((String)cboMonth.getSelectedItem());
try {
DateInfo date=new DateInfo(year,month);
txaShow.setText("" date);
}
catch (DateException ex) {
ex.printStackTrace();
}
}
});
btnClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
}
public void run(){
try {
while(true){
Thread.sleep(1000);
int hour=date.get(Calendar.HOUR);
int minute=date.get(Calendar.MINUTE);
int second=date.get(Calendar.SECOND);
String str=hour ":" minute ":" second;
lblShowDate.setText(str);
//this.repaint();
}
【日历程序java代码 java编程实现日历记事本】}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
//DateException.java
public class DateException extends Exception{
public DateException(){
super("日期数据不合法.");
}
}
显示系统当前日历和查询日历,java代码,亲们,MainFrame.java是显示日历程序,Clock.java是日历计算程序(可以不要) 。
编译后运行MainFrame这个类即可 。
swing窗口显示万年历 , jdk1.4以上环境编译运行 。
package
org.test;import
java.awt.BorderLayout;import
java.awt.Color;import
java
怎么用java写日历以下是两个类日历程序java代码,请楼主分别存成两个java文件:
其中
MainFrame.java是显示日历程序日历程序java代码,Clock.java是日历计算程序 。编译后运行MainFrame这个类即可 。
1.MainFrame.java
---
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 = 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 = 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; i49; 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; // 得到当前月份日历程序java代码,并减1,计算机中的月为0-11
c_week = use(c_year, c_month); // 调用函数use日历程序java代码,得到星期几
Resetday(c_week, c_year, c_month); // 调用函数Resetday
}
}
public void Init() {
int year, month_num, first_day_num;
String log[] = { "日", "一", "二", "三", "四", "五", "六" };
for (int i = 0; i7; i) {
label[i].setText(log[i]);
}
for (int i = 0; i49; i = i7) {
label[i].setForeground(Color.red); // 将星期日的日期设置为红色
}
for (int i = 6; i49; i = i7) {
label[i].setForeground(Color.green);// 将星期六的日期设置为绿色
}
for (int i = 1; i10000; i) {
com1.addItem(""i);
}
for (int i = 1; i13; 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_log1, 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; i49; i) { // 初始化标签
label[i].setText("");
}
week_log = week_log6; // 将星期数加6,使显示正确
month_day_score = month_day_scoreweek_log;
for (int i = week_log; imonth_day_score; i, count) {
label[i].setText(count"");
}
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
new MainFrame();
}
}
2.Clock.java
-----
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;
//显示时间的类:Clock
/** *//**
* Clock.java
* Summary 数字时间显示
* Created on
* @author
* remark
*/
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Calendar;
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代码和java编程实现日历记事本的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读