效果图如下:

 import java.awt.*;
import java.awt.event.*;
import java.util.*; import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*; public class MyCalendar extends JApplet
{
//定义星期天到星期六
public static final String WEEK_SUN = "周日";
public static final String WEEK_MON = "周一";
public static final String WEEK_TUE = "周二";
public static final String WEEK_WED = "周三";
public static final String WEEK_THU = "周四";
public static final String WEEK_FRI = "周五";
public static final String WEEK_SAT = "周六";
//设置背景颜色
public static final Color background = Color.white;
//设置前景颜色
public static final Color foreground = Color.black;
//设置题头星期的背景颜色和前景颜色
public static final Color headerBackground = Color.blue;
public static final Color headerForeground = Color.white;
//设置被选中的日期的背景颜色和前景颜色
public static final Color selectedBackground = Color.red;
public static final Color selectedForeground = Color.white; private JPanel cPane;
private JLabel yearsLabel;//年
private JSpinner yearsSpinner;//年调控
private JLabel monthsLabel;//月
private JComboBox monthsComboBox;//月下拉框
private JTable daysTable;//用来显示日期的table
private AbstractTableModel daysModel;
private Calendar calendar;
//构造方法初始化panel public MyCalendar()
{
cPane = (JPanel) getContentPane();
}
//初始化,对所有的空间进行布局
public void init()
{ cPane.setLayout(new BorderLayout()); //使用Border布局管理器
calendar = Calendar.getInstance(); //获取当前时间
calendar = Calendar.getInstance();
yearsLabel = new JLabel("Year: ");
yearsSpinner = new JSpinner();
yearsSpinner.setEditor(new JSpinner.NumberEditor(yearsSpinner,"0000"));
yearsSpinner.setValue(new Integer(calendar.get(Calendar.YEAR)));
//增加监听 监听年份的改变
yearsSpinner.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent changeEvent)
{
int day = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.YEAR,((Integer)yearsSpinner.getValue()).intValue());
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, day>maxDay?maxDay:day);
updateView();
}
}); JPanel yearMonthPanel = new JPanel();
cPane.add(yearMonthPanel,BorderLayout.NORTH);
yearMonthPanel.setLayout(new BorderLayout());
yearMonthPanel.add(new JPanel(),BorderLayout.CENTER);
JPanel yearPanel = new JPanel();
yearMonthPanel.add(yearPanel,BorderLayout.WEST);
yearPanel.setLayout(new BorderLayout());
yearPanel.add(yearsLabel,BorderLayout.WEST);
yearPanel.add(yearsSpinner,BorderLayout.CENTER); monthsLabel = new JLabel("Month: ");
//向月份下拉框中增加内容
monthsComboBox = new JComboBox();
for(int i=1; i<=12; i++)
{
monthsComboBox.addItem(new Integer(i));
}
monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
//监听月份的改变
monthsComboBox.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent actionEvevt) {
// TODO Auto-generated method stub
int day = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH,1);
calendar.set(Calendar.MONTH,monthsComboBox.getSelectedIndex());
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH,day>maxDay?maxDay:day);
updateView();
}
}); JPanel monthPanel = new JPanel();
yearMonthPanel.add(monthPanel,BorderLayout.EAST);
monthPanel.setLayout(new BorderLayout());
monthPanel.add(monthsLabel,BorderLayout.WEST);
monthPanel.add(monthsComboBox,BorderLayout.CENTER); daysModel = new AbstractTableModel() { @Override
public Object getValueAt(int row, int column) {
// TODO Auto-generated method stub
if(row==0)
{
return getHeader(column);
}
row--;
Calendar calendar = (Calendar)MyCalendar.this.calendar.clone();
calendar.set(Calendar.DAY_OF_MONTH,1);
int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK)-1; //每个月第一天从哪一列开始
int index = row*7 + column;
int dayIndex = index - moreDayCount + 1;
if(index<moreDayCount||dayIndex>dayCount)
{
return null;
}
else
{
return new Integer(dayIndex);
}
}
//设置行7
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return 7;
}
//设置列7
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return 7;
}
};
daysTable = new CalendarTable(daysModel,calendar);
//设置每个cell可以被选中
daysTable.setCellSelectionEnabled(true);
daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
daysTable.setDefaultRenderer(daysTable.getColumnClass(0),new TableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table,Object value,
boolean isSelected,boolean hasFocus,int row,int column) {
String text = (value == null )? "" : value.toString();
JLabel cell = new JLabel(text);
cell.setOpaque(true);
if(row == 0)
{
cell.setForeground(headerForeground);
cell.setBackground(headerBackground);
}
else
{
if(isSelected)
{
cell.setForeground(selectedForeground);
cell.setBackground(selectedBackground);
}
else
{
cell.setForeground(foreground);
cell.setBackground(background);
}
}
return cell;
}
});
updateView();
cPane.add(daysTable,BorderLayout.CENTER);
} public static String getHeader(int index)
{
switch(index)
{
case 0:
return WEEK_SUN;
case 1:
return WEEK_MON;
case 2:
return WEEK_TUE;
case 3:
return WEEK_WED;
case 4:
return WEEK_THU;
case 5:
return WEEK_FRI;
case 6:
return WEEK_SAT;
default:
return null;
}
} public void updateView()
{
daysModel.fireTableDataChanged();
daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH),
calendar.get(Calendar.WEEK_OF_MONTH));
daysTable.setColumnSelectionInterval(calendar.get(Calendar.DAY_OF_WEEK)-1,
calendar.get(Calendar.DAY_OF_WEEK)-1);
}
//设置日历的table
public static class CalendarTable extends JTable
{
private Calendar calendar;
public CalendarTable(TableModel model,Calendar calendar)
{
super(model);
this.calendar = calendar;
}
public void changeSelection(int row,int column,boolean toggle,boolean extend)
{
super.changeSelection(row, column, toggle, extend);
if(row == 0)
{
return;
}
Object obj = getValueAt(row,column);
if(obj != null)
{
calendar.set(Calendar.DAY_OF_MONTH,((Integer)obj).intValue());
}
}
}
//让applet作为一个可执行的程序来运行
public static void main(String[] args)
{
JFrame frame = new JFrame("ShuBing日历");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyCalendar myCalendar = new MyCalendar();
myCalendar.init();
frame.getContentPane().add(myCalendar);
frame.setSize(300,200);
frame.setVisible(true);
}
}

JAVA日历的更多相关文章

  1. java日历显示年份、月份

    import java.util.Scanner;class CalendarMain{     //主函数入口    public static void main(String[] args)   ...

  2. java 日历代码实现

    System.out.println("请输入日期(按照格式:2030-3-10):"); //在控制台输入 //String str="2016-9-26"; ...

  3. java日历程序版本

    //MainFrame.java package com.huowolf.myCalender; import java.awt.BorderLayout; import java.awt.Color ...

  4. java日历类Calendar简单使用

    import java.util.Calendar; import java.util.TimeZone; public class Test1 { public static void main(S ...

  5. java 日历计算农历和节假日的工具类

    背景 业务需求需要后端提供这样的接口,网上找了很多java代码例子,虽然功能实现了 但是不完善,特别是节日那一块儿.然后百度发现有这样的插件,但是信息也是java后端提供的非js 然后在开源js插件找 ...

  6. java日历类(calendar),可输出年月日等等,以及和Date相互转化

    日历创建对象: Calendar类为抽象类,不可实例化 方式一: 父类引用指向类对象 Calendar  cal = new GregorianCalendar(); 方式二: Calendar ca ...

  7. java 日历类Calendar用法

    如何获取昨天?取昨天的日期,本想的截出来日期减一就好了.又一想不对,如果今天是一号怎么办? 现有两个办法 1: Date as = new Date(new Date().getTime()-24*6 ...

  8. 我编辑的JAVA日历程序

    class calendar { public static void main(String[]args) { int yearIn ; yearIn = Integer.parseInt(args ...

  9. JAVA上百实例源码以及开源项目

    简介 笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级.中级.高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情.执着,对IT的憧憬. ...

随机推荐

  1. [C#源码]VS各版本转换器(支持VS2012,VS2013)

    项目名称:[C#源码]VS各版本转换器(支持VS2012,VS2013) 下载内容: (C#源码)VS各版本转换器 实现功能: 支持vs2003-vs2013的各版本转换,由高到低,由低到高都支持. ...

  2. Android上实现仿IOS弹性ScrollView

    [转]http://www.tuicool.com/articles/ummIJb

  3. c/c++中使用指针需要注意的问题

    一.使用指针的时候需要注意几点: 分配空间 初始化 释放 二.常见的错误有几种: 1)内存分配未成功,却使用了它      编程新手常犯这种错误,因为他们没有意识到内存分配会不成功.常用解决办法是,使 ...

  4. 如何关闭UINavigationController 向右滑动 返回上一层视图

    说明一下: 我的nav 设置的rootview 是 tabbarcontroller,登录界面是push进去的,所以,在登录界面,如果靠近最左边 向右滑动 会出现 tabbarcontroller的视 ...

  5. 应用之星推出“图文app”制作工具,并附上教程

    应用之星已推出的"图文"app制作工具,是高速制作图文电子书,图文杂志等一切有关图文资料的app生成工具,以下跟大家介绍"图文"制作教程,简单快捷,大致分三大步 ...

  6. 写给新入IT的新人们

    IT=挨踢,这是IT人的自嘲,进入IT行业是有四五年了,也算得上是一个"老人"了吧,见了不少新人,面试了不少新人,也带了一些新人,多多少少还是有点发言权的. 关于书本 新人们经常会 ...

  7. hdu 1348 Wall(凸包模板题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 Wall Time Limit: 2000/1000 MS (Java/Others)    M ...

  8. nopCommerce的源代码结构和架构

    下载地址 nopcommerce的源代码很容易拿到,它是开源的,所以你可以直接到网上下载.下载地址:http://www.nopcommerce.com/downloads.aspx 因为nop是国外 ...

  9. How to allow/block PING on Linux server – IPTables rules for icmp---reference

    BY ADMIN - APRIL, 9TH 2014 The ‘PING’, it’s a command-line tool to check a host is reachable or not. ...

  10. VirtualBox中centos网络配置

    VirtualBox图形界面下有四种网络接入方式,它们分别是: 1.NAT 网络地址转换模式(NAT,Network Address Translation) 2.Bridged Adapter 桥接 ...