效果图如下:
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日历的更多相关文章
-
java日历显示年份、月份
import java.util.Scanner;class CalendarMain{ //主函数入口 public static void main(String[] args) ...
-
java 日历代码实现
System.out.println("请输入日期(按照格式:2030-3-10):"); //在控制台输入 //String str="2016-9-26"; ...
-
java日历程序版本
//MainFrame.java package com.huowolf.myCalender; import java.awt.BorderLayout; import java.awt.Color ...
-
java日历类Calendar简单使用
import java.util.Calendar; import java.util.TimeZone; public class Test1 { public static void main(S ...
-
java 日历计算农历和节假日的工具类
背景 业务需求需要后端提供这样的接口,网上找了很多java代码例子,虽然功能实现了 但是不完善,特别是节日那一块儿.然后百度发现有这样的插件,但是信息也是java后端提供的非js 然后在开源js插件找 ...
-
java日历类(calendar),可输出年月日等等,以及和Date相互转化
日历创建对象: Calendar类为抽象类,不可实例化 方式一: 父类引用指向类对象 Calendar cal = new GregorianCalendar(); 方式二: Calendar ca ...
-
java 日历类Calendar用法
如何获取昨天?取昨天的日期,本想的截出来日期减一就好了.又一想不对,如果今天是一号怎么办? 现有两个办法 1: Date as = new Date(new Date().getTime()-24*6 ...
-
我编辑的JAVA日历程序
class calendar { public static void main(String[]args) { int yearIn ; yearIn = Integer.parseInt(args ...
-
JAVA上百实例源码以及开源项目
简介 笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级.中级.高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情.执着,对IT的憧憬. ...
随机推荐
-
Android驱动开发前的准备(四)
源代码的下载和编译 4.1 下载.编译和测试Android源代码 4.2下载和编译linux内核源代码 4.1.1 配置Android源代码下载环境 (1) 创建一个用于存放下载脚本文件的目录 # m ...
-
winform设置button的边框颜色,或取消边框颜色,不显示边框
// winform设置边框颜色不像webform那么简单,可以通过设置FlatAppearance,也可以通过重绘实现. 一.设置按钮本身属性 buttonBubufx.FlatStyle = Fl ...
-
Linux下端口被占用解决
有时候关闭软件后,后台进程死掉,导致端口被占用.下面以JBoss端口8083被占用为例,列出详细解决过程. 解决方法: 1.查找被占用的端口 netstat -tln netstat -tln | ...
-
ActiveMQ持久化消息
ActiveMQ的另一个问题就是只要是软件就有可能挂掉,挂掉不可怕,怕的是挂掉之后把信息给丢了,所以本节分析一下几种持久化方式: 一.持久化为文件 ActiveMQ默认就支持这种方式,只要在发消息时设 ...
-
Struts+jdbc+分页 实例
根据项目里分页实例,带有注解. package org.tarena.netctoss.dao.impl; import java.sql.Connection; import java.sql.Pr ...
-
python学习之路网络编程篇(第四篇)- 续
Memcache简介 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速 ...
-
Mint wine
卸载 http://blog.csdn.net/ouyangying123/article/details/459126051.终端中执行sudo apt-get remove wine1.3(win ...
-
20165327 2017-2018-2 《Java程序设计》第2周学习总结
20165327 2017-2018-2 <Java程序设计>第2周学习总结 内容:教材第2.3章 内容小结: (一)标识符由字母.下划线.美元符号和数字组成, 并且第一个字符不能是数字字 ...
-
codeM 2018 资格赛
比赛链接:https://www.nowcoder.com/activity/2018codem/index?from=meituan 1.下单 给定若干商品,可以选择打折.满减两种方式. #incl ...
-
python模块--hashlib
用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法 import hashlib m = hashlib.m ...