站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题一个日历程序

一个日历程序

摘要: 一个日历程序

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle">
<table width="678" border="0"> <tr> <td width="313">程序运行图:

</td> <td width="355"> </td> </tr> </table>
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Calendar;
import java.util.Date;

/**
* 日历类, 提供日历功能
*/
public class MonthSelector extends JPanel implements ActionListener, MouseMotionListener, MouseListener
{
final private String TITLES[] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
final private int ROW_HEIGHT = 20;
final private int ROW_WIDTH = 40;

private JTable table; // 表格
private JLabel title; // 标题
private JButton nextMonth; // 下个月份的按钮
private JButton preMonth; // 上个月的按钮
private JPanel top; // 顶部容器

private int year; // 当前显示的年份
private int month; // 当前显示的月份
private int day; // 当前选择的日期
private int week; // 当前选择的星期
private int selectedX = -1;
private int selectedY = -1;
private int clickX = -1;
private int clickY = -1;

public MonthSelector(){
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());

year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH)+1;
day = cal.get(Calendar.DAY_OF_MONTH)-1;
week = cal.get(Calendar.DAY_OF_WEEK)-1;

top = new JPanel(new FlowLayout(FlowLayout.CENTER));
table = new JTable(7,7);

table.setPreferredSize(new Dimension(ROW_WIDTH * 7, ROW_HEIGHT * 7));
table.setRowHeight(ROW_HEIGHT);
for(int i=0; i table.setValueAt(TITLES[i],0,i);

title = new JLabel();
nextMonth = new JButton(">>");
preMonth = new JButton("<<");
top.add(preMonth);
top.add(title);
top.add(nextMonth);
setTitle();
table.setEnabled(false);
// 设置按钮
setLayout(new BorderLayout());

add(top, BorderLayout.NORTH);
add(table, BorderLayout.CENTER);

preMonth.addActionListener(this);
nextMonth.addActionListener(this);

showMonth(); // 设置显示当前月份
table.setCellSelectionEnabled(true);

table.addMouseMotionListener(this);
table.addMouseListener(this);
}

/**
* 取得指定年份和月份的日期数
* @return
*/
public int getDaysOfMonth(int year, int month)
{
int Days[]= {31,28,31,30,31,30,31,31,30,31,30,31};
// 判断是否闰年

if( year % 400 == 0 || (year % 4 ==0 && year % 100 != 0)) // 说明是闰年
Days[1] = 29;

return Days[month-1];
}

/**
* 取得指定月份的头一天的星期几
* @param month
* @return
*/
public int getWeekOfFirstDay(int year, int month){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DAY_OF_MONTH, 1);

int result = cal.get(Calendar.DAY_OF_WEEK) - 1;
return result ;
}

/**
* 设置显示的标题
*/
private void setTitle(){
String m = String.valueOf(month);
if(m.length() ==1) m = "0"+m;
title.setText(String.valueOf(year)+"年"+m+"月");
}

/**
* 显示月份
*/
private void showMonth(){
for(int x=0; x<7; x++)
for(int y=1; y<7; y++) table.setValueAt("",y, x);

int y = 1;
int x = getWeekOfFirstDay(year, month);
int maxDay = getDaysOfMonth(year,month);

for(int i=0; i table.setValueAt(new Integer(i+1),y,x);
x ++;
if(x>6){
x=0;
y++;
}
}
repaint();
}

/**
* 处理按钮
* @param e
*/
public void actionPerformed(ActionEvent e){
if(e.getSource().equals(nextMonth)){
month++;
if(month>12){
month = 1;
year++;
}
}

if(e.getSource().equals(preMonth)){
month--;
if(month<1){
month = 12;
year --;
}
}

setTitle();
showMonth();
}

/**
* 绘制
* @param g
*/
public void paint(Graphics g){
super.paint(g);

// repaint Table First LINE
g.fillRect(0,top.getHeight(), getWidth()-1, ROW_HEIGHT -1);
g.setColor(Color.white);
for(int i=0; i<7; i++)
g.drawString((String)(table.getModel().getValueAt(0,i)),i*ROW_WIDTH, top.getHeight()+ ROW_HEIGHT - 3);

Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
int line = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH) + 1;
int x = week * table.getWidth() / 7;
int y = top.getHeight() + ROW_HEIGHT * line ;
g.setColor(Color.black);
g.drawRect(x+1,y+1, ROW_WIDTH-4, ROW_HEIGHT-4);
if(selectedX >= 0 && selectedY>= 0 && ! table.getModel().getValueAt(selectedY,selectedX).equals("")){
g.setColor(Color.blue);
g.drawRect(selectedX * ROW_WIDTH+1, (selectedY + 2) * ROW_HEIGHT -1 ,ROW_WIDTH-4,ROW_HEIGHT-4);
}

if(clickY >= 0 && clickX >= 0 & ! table.getModel().getValueAt(clickY,clickX).equals(""))
{
g.setColor(Color.green);
g.drawRect(clickX * ROW_WIDTH+1, (clickY + 2) * ROW_HEIGHT -1 ,ROW_WIDTH-4,ROW_HEIGHT-4);
}
}

public void mouseDragged(MouseEvent e){}

public void mouseMoved(MouseEvent e){
if(e.getX()>0 && e.getY() ROW_HEIGHT && e.getY()< ROW_HEIGHT * 7 )
{
selectedX = e.getX() / ROW_WIDTH;
selectedY = e.getY() / ROW_HEIGHT;

repaint();
}
}

public void mouseClicked(MouseEvent e) {}

public void mousePressed(MouseEvent e){
if(e.getX()>0 && e.getY() ROW_HEIGHT && e.getY()< ROW_HEIGHT * 7 )
{
clickX = e.getX() / ROW_WIDTH;
clickY = e.getY() / ROW_HEIGHT;

repaint();
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}

/* --=[ 测试代码 ]=-- */
public static void main(String[] args){
JFrame frm = new JFrame();
frm.getContentPane().setLayout(new BorderLayout());
frm.getContentPane().add(new MonthSelector());
frm.pack();
frm.show();
}
}


</td> </tr> <tr>


↑返回目录
前一篇: 字符串替换
后一篇: Swing 入门教程