这个日历是改出来的。。。
JPanel版的
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import xserver.inventory.res.DateTools;
public class CalendarControl extends JPanel implements ActionListener
{
public static void main(String[] av)
{
JFrame f = new JFrame("Cal");
Container c = f.getContentPane();
c.setLayout(new FlowLayout());
c.add(new CalendarControl());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
private JButton b0;
protected JButton btnDays[][];
private JComboBox monthChoice = new JComboBox();
private JComboBox yearChoice = new JComboBox();
protected int yy, mm, dd;
protected int firstDayWeek = 0;
private int activeDay = -1;
private Calendar calendar = new GregorianCalendar();
protected final int thisYear = calendar.get(Calendar.YEAR);
protected final int thisMonth = calendar.get(Calendar.MONTH);// 从0开始
CalendarControl()
{
super();
setYYMMDD(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar
.get(Calendar.DAY_OF_MONTH));
buildGUI();
recompute();
}
CalendarControl(int year, int month, int today)
{
super();
setYYMMDD(year, month, today);
buildGUI();
recompute();
}
private void buildGUI()
{
getAccessibleContext().setAccessibleDescription("Calendar not accessible yet. Sorry!");
setBorder(BorderFactory.createEtchedBorder());
setLayout(new BorderLayout());
yearsPanel();
daysPanel();
}
private void setYYMMDD(int year, int month, int today)
{
yy = year;
mm = month;
dd = today;
}
public String getDateString()
{
return yy + "-" + mm + "-" + dd;
}
private void yearChoice()
{
for (int i = yy - 5; i < yy + 20; i++)
yearChoice.addItem(Integer.toString(i));
yearChoice.setSelectedItem(Integer.toString(yy));
yearChoice.addActionListener(this);
}
private void monthChoice()
{
String[] months = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };
for (int i = 0; i < months.length; i++)
monthChoice.addItem(months[i]);
monthChoice.setSelectedItem(months[mm]);
yearChoice.addActionListener(this);
monthChoice.getAccessibleContext().setAccessibleName("Months");
monthChoice.getAccessibleContext().setAccessibleDescription("Choose a month of the year");
}
private void yearsPanel()
{
JPanel ymPanel = new JPanel();// 年月面板
yearChoice();
monthChoice();
ymPanel.add(yearChoice);
ymPanel.add(monthChoice);
add(BorderLayout.CENTER, ymPanel);
monthChoice.addActionListener(this);
}
public void actionPerformed(ActionEvent e) // 监听年月
{
JComboBox comtobox = (JComboBox) e.getSource();
if (comtobox == yearChoice)
{
int i = yearChoice.getSelectedIndex();
if (i >= 0)
{
yy = Integer.parseInt(yearChoice.getSelectedItem().toString());
recompute();
System.out.println("xxx");
}
return;
}
if (comtobox == monthChoice)
{
int i = monthChoice.getSelectedIndex();
if (i >= 0)
{
mm = i;// 月分下标由0开始
recompute();
}
return;
}
}
private void daysPanel()
{
JPanel daysPanel = new JPanel();// 日期面板
daysPanel.setLayout(new GridLayout(7, 7));
btnDays = new JButton[6][7]; // first row is days
daysPanel.add(b0 = new JButton("日"));
daysPanel.add(new JButton("一"));
daysPanel.add(new JButton("二"));
daysPanel.add(new JButton("三"));
daysPanel.add(new JButton("四"));
daysPanel.add(new JButton("五"));
daysPanel.add(new JButton("六"));
ActionListener dateSetter = new ActionListener()// 内部类,监听
{
public void actionPerformed(ActionEvent e)
{
String day = e.getActionCommand();
if (!day.equals(""))
{
setDayActive(Integer.parseInt(day));// 设置选中的高亮
dd = Integer.parseInt(day);
System.out.println(getDateString());
}
}
};
for (int i = 0; i < 6; i++)
for (int j = 0; j < 7; j++)
{
daysPanel.add(btnDays[i][j] = new JButton(""));
btnDays[i][j].addActionListener(dateSetter);
}
add(BorderLayout.SOUTH, daysPanel);
}
public final static int MONTH_DAYS[] = { // 平年2月为28天
31, 28, 31, 30, /* jan feb mar apr */
31, 30, 31, 31, /* may jun jul aug */
30, 31, 30, 31 /* sep oct nov dec */
};
public int getOneDayWeek(int yy, int mm, int dd)
{
Calendar calendar = new GregorianCalendar(yy, mm, dd);
return calendar.get(Calendar.DAY_OF_WEEK) - 1;
}
/** Compute which days to put where, in the Cal panel */
protected void recompute()
{
if (mm < 0 || mm > 11)
{
throw new IllegalArgumentException("Month " + mm + " bad, must be 0-11");
}
clearDayActive();
calendar = new GregorianCalendar(yy, mm, dd);
firstDayWeek = DateTools.getMohtnFirstDayWeek(yy, mm);
for (int i = 0; i < firstDayWeek; i++)// 一个月的一号之前的那几个空天
{
btnDays[0][i].setText("");
}
int daysCount = MONTH_DAYS[mm];
if (DateTools.isLeap(calendar.get(Calendar.YEAR)) && mm == 1) ++daysCount; // 闰年二月多一天
for (int i = 1; i <= daysCount; i++)// 画一个月
{
JButton b = btnDays[(firstDayWeek + i - 1) / 7][(firstDayWeek + i - 1) % 7];
b.setText(Integer.toString(i));
System.out.println(i);
}
for (int i = firstDayWeek + daysCount; i < 6 * 7; i++)// 剩余空格
{
btnDays[(i) / 7][(i) % 7].setText("");
}
if (thisYear == yy && mm == thisMonth) setDayActive(dd); // shade the box for today
repaint();
}
public void setDayActive(int newDay)// 设置高亮
{
clearDayActive();
if (newDay <= 0) dd = new GregorianCalendar().get(Calendar.DAY_OF_MONTH);
else dd = newDay;
Component square = btnDays[(firstDayWeek + newDay - 1) / 7][(firstDayWeek + newDay - 1) % 7];
square.setBackground(Color.red);
square.repaint();
activeDay = newDay;
}
private void clearDayActive() // 取消高亮
{
JButton b;
if (activeDay > 0)
{
b = btnDays[(firstDayWeek + activeDay - 1) / 7][(firstDayWeek + activeDay - 1) % 7];
b.setBackground(b0.getBackground());
b.repaint();
activeDay = -1;
}
}
}
JDialog版
package xserver.inventory.res;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CalendarDialog extends JDialog implements ActionListener
{
private JButton b0;
private JButton btnDays[][];
private JComboBox monthChoice = new JComboBox();
private JComboBox yearChoice = new JComboBox();
private int yy, mm, dd;
private int firstDayWeek = 0;
private int activeDay = -1;
private Calendar calendar = new GregorianCalendar();
private final int thisYear = calendar.get(Calendar.YEAR);
private final int thisMonth = calendar.get(Calendar.MONTH);// 从0开始
public CalendarDialog(JFrame parent)
{
super(parent, "日历", true);
setYYMMDD(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar
.get(Calendar.DAY_OF_MONTH));
buildGUI();
recompute();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int high = (int) screenSize.getHeight()/ 4;
int width = (int) screenSize.getWidth()/ 2;
setLocation(width, high);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}
private void buildGUI()
{
getAccessibleContext().setAccessibleDescription("Calendar not accessible yet. Sorry!");
setLayout(new BorderLayout());
yearsPanel();
daysPanel();
}
private void setYYMMDD(int year, int month, int today)
{
yy = year;
mm = month;
dd = today;
}
public String getDateString()
{
return yy + "-" + (mm + 1) + "-" + dd;
}
private void yearChoice()
{
for (int i = yy - 5; i < yy + 20; i++)
yearChoice.addItem(Integer.toString(i));
yearChoice.setSelectedItem(Integer.toString(yy));
yearChoice.addActionListener(this);
}
private void monthChoice()
{
String[] months = { "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月" };
for (int i = 0; i < months.length; i++)
monthChoice.addItem(months[i]);
monthChoice.setSelectedItem(months[mm]);
yearChoice.addActionListener(this);
monthChoice.getAccessibleContext().setAccessibleName("Months");
monthChoice.getAccessibleContext().setAccessibleDescription("Choose a month of the year");
}
private void yearsPanel()
{
JPanel ymPanel = new JPanel();// 年月面板
yearChoice();
monthChoice();
ymPanel.add(yearChoice);
ymPanel.add(monthChoice);
add(BorderLayout.CENTER, ymPanel);
monthChoice.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
try
{
JComboBox comtobox = (JComboBox) e.getSource();
if (comtobox == yearChoice)
{
int i = yearChoice.getSelectedIndex();
if (i >= 0)
{
yy = Integer.parseInt(yearChoice.getSelectedItem().toString());
recompute();
}
return;
}
if (comtobox == monthChoice)
{
int i = monthChoice.getSelectedIndex();
if (i >= 0)
{
mm = i;// 月分下标由0开始
recompute();
}
return;
}
}
catch (ClassCastException cce)
{
}
String day = e.getActionCommand();
if (!day.equals(""))
{
setDayActive(Integer.parseInt(day));// 设置选中的高亮
dd = Integer.parseInt(day);
setVisible(false);
System.out.println(this.getDateString());
}
}
private void daysPanel()
{
JPanel daysPanel = new JPanel();// 日期面板
daysPanel.setLayout(new GridLayout(7, 7));
btnDays = new JButton[6][7]; // first row is days
daysPanel.add(b0 = new JButton("日"));
daysPanel.add(new JButton("一"));
daysPanel.add(new JButton("二"));
daysPanel.add(new JButton("三"));
daysPanel.add(new JButton("四"));
daysPanel.add(new JButton("五"));
daysPanel.add(new JButton("六"));
for (int i = 0; i < 6; i++)
for (int j = 0; j < 7; j++)
{
daysPanel.add(btnDays[i][j] = new JButton(""));
btnDays[i][j].addActionListener(this);
}
add(BorderLayout.SOUTH, daysPanel);
}
private final static int MONTH_DAYS[] = { // 平年2月为28天
31, 28, 31, 30, /* jan feb mar apr */
31, 30, 31, 31, /* may jun jul aug */
30, 31, 30, 31 /* sep oct nov dec */
};
public int getOneDayWeek(int yy, int mm, int dd)
{
Calendar calendar = new GregorianCalendar(yy, mm, dd);
return calendar.get(Calendar.DAY_OF_WEEK) - 1;
}
/** Compute which days to put where, in the Cal panel */
private void recompute()
{
if (mm < 0 || mm > 11)
{
throw new IllegalArgumentException("Month " + mm + " bad, must be 0-11");
}
clearDayActive();
calendar = new GregorianCalendar(yy, mm, dd);
firstDayWeek = DateTools.getMohtnFirstDayWeek(yy, mm);
for (int i = 0; i < firstDayWeek; i++)// 一个月的一号之前的那几个空天
{
btnDays[0][i].setText("");
}
int daysCount = MONTH_DAYS[mm];
if (DateTools.isLeap(calendar.get(Calendar.YEAR)) && mm == 1) ++daysCount; // 闰年二月多一天
for (int i = 1; i <= daysCount; i++)// 画一个月
{
JButton b = btnDays[(firstDayWeek + i - 1) / 7][(firstDayWeek + i - 1) % 7];
b.setText(Integer.toString(i));
}
for (int i = firstDayWeek + daysCount; i < 6 * 7; i++)// 剩余空格
{
btnDays[(i) / 7][(i) % 7].setText("");
}
if (thisYear == yy && mm == thisMonth) setDayActive(dd); // shade the box for today
repaint();
}
private void setDayActive(int newDay)// 设置高亮
{
clearDayActive();
if (newDay <= 0) dd = new GregorianCalendar().get(Calendar.DAY_OF_MONTH);
else dd = newDay;
Component square = btnDays[(firstDayWeek + newDay - 1) / 7][(firstDayWeek + newDay - 1) % 7];
square.setBackground(Color.red);
square.repaint();
activeDay = newDay;
}
private void clearDayActive() // 取消高亮
{
JButton b;
if (activeDay > 0)
{
b = btnDays[(firstDayWeek + activeDay - 1) / 7][(firstDayWeek + activeDay - 1) % 7];
b.setBackground(b0.getBackground());
b.repaint();
activeDay = -1;
}
}
}