锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
//1.AbsoluteConstraints.java
import java.awt.Dimension;
import java.awt.Point;
public class AbsoluteConstraints
聽聽聽 implements java.io.Serializable {
聽 static final long serialVersionUID = 5261460716622152494L;
聽 public int x;
聽 public int y;
聽 public int width = -1;
聽 public int height = -1;
聽 public AbsoluteConstraints(Point pos) {
聽聽聽 this(pos.x, pos.y);
聽 }
聽 public AbsoluteConstraints(int x, int y) {
聽聽聽 this.x = x;
聽聽聽 this.y = y;
聽 }
聽 public AbsoluteConstraints(Point pos, Dimension size) {
聽聽聽 this.x = pos.x;
聽聽聽 this.y = pos.y;
聽聽聽 if (size != null) {
聽聽聽聽聽 this.width = size.width;
聽聽聽聽聽 this.height = size.height;
聽聽聽 }
聽 }
聽 public AbsoluteConstraints(int x, int y, int width, int height) {
聽聽聽 this.x = x;
聽聽聽 this.y = y;
聽聽聽 this.width = width;
聽聽聽 this.height = height;
聽 }
聽 public int getX() {
聽聽聽 return x;
聽 }
聽 public int getY() {
聽聽聽 return y;
聽 }
聽 public int getWidth() {
聽聽聽 return width;
聽 }
聽 public int getHeight() {
聽聽聽 return height;
聽 }
聽 public String toString() {
聽聽聽 return super.toString() + " [x=" + x + ", y=" + y + ", width=" + width +
聽聽聽聽聽聽聽 ", height=" + height + "]";
聽 }
}
//2.import java.awt.*;
public class AbsoluteLayout
聽聽聽 implements LayoutManager2, java.io.Serializable {
聽 static final long
聽聽聽聽聽 serialVersionUID = -1919857869177070440L;
聽 protected java.util.Hashtable constraints = new java.util.Hashtable();
聽 public
聽聽聽聽聽 void addLayoutComponent(String name, Component comp) {
聽聽聽 throw new IllegalArgumentException();
聽 }
聽 public void
聽聽聽聽聽 removeLayoutComponent(Component comp) {
聽聽聽 constraints.remove(comp);
聽 }
聽 public Dimension preferredLayoutSize
聽聽聽聽聽 (Container parent) {
聽聽聽 int maxWidth = 0;
聽聽聽 int maxHeight = 0;
聽聽聽 for (java.util.Enumeration e =
聽聽聽聽聽聽聽聽 constraints.keys(); e.hasMoreElements(); ) {
聽聽聽聽聽 Component comp = (Component) e.nextElement();
聽聽聽聽聽 AbsoluteConstraints
聽聽聽聽聽聽聽聽聽 ac = (AbsoluteConstraints) constraints.get(comp);
聽聽聽聽聽 Dimension size = comp.getPreferredSize();
聽聽聽聽聽 int width =
聽聽聽聽聽聽聽聽聽 ac.getWidth();
聽聽聽聽聽 if (width == -1)
聽聽聽聽聽聽聽 width = size.width;
聽聽聽聽聽 int height = ac.getHeight();
聽聽聽聽聽 if (height == -1)
聽聽聽聽聽聽聽 height = size.height;
聽聽聽聽聽 if (ac.x + width > maxWidth)
聽聽聽聽聽聽聽 maxWidth = ac.x + width;
聽聽聽聽聽 if (ac.y + height >
聽聽聽聽聽聽聽聽聽 maxHeight)
聽聽聽聽聽聽聽 maxHeight = ac.y + height;
聽聽聽 }
聽聽聽 return new Dimension(maxWidth, maxHeight);
聽 }
聽 public
聽聽聽聽聽 Dimension minimumLayoutSize(Container parent) {
聽聽聽 int maxWidth = 0;
聽聽聽 int maxHeight = 0;
聽聽聽 for
聽聽聽聽聽聽聽 (java.util.Enumeration e = constraints.keys(); e.hasMoreElements(); ) {
聽聽聽聽聽 Component comp = (Component) e.nextElement();
聽聽聽聽聽 AbsoluteConstraints ac = (AbsoluteConstraints) constraints.get(comp);
聽聽聽聽聽 Dimension size = comp.getMinimumSize();
聽聽聽聽聽 int width = ac.getWidth();
聽聽聽聽聽 if (width == -1)
聽聽聽聽聽聽聽 width = size.width;
聽聽聽聽聽 int height = ac.getHeight();
聽聽聽聽聽 if (height == -1)
聽聽聽聽聽聽聽 height = size.height;
聽聽聽聽聽 if (ac.x + width > maxWidth)
聽聽聽聽聽聽聽 maxWidth = ac.x + width;
聽聽聽聽聽 if
聽聽聽聽聽聽聽聽聽 (ac.y + height > maxHeight)
聽聽聽聽聽聽聽 maxHeight = ac.y + height;
聽聽聽 }
聽聽聽 return new Dimension(maxWidth, maxHeight);
聽 }
聽 public void layoutContainer(Container parent) {
聽聽聽 for (java.util.Enumeration e = constraints.keys();
聽聽聽聽聽聽聽聽 e.hasMoreElements(); ) {
聽聽聽聽聽 Component comp = (Component) e.nextElement();
聽聽聽聽聽 AbsoluteConstraints ac =
聽聽聽聽聽聽聽聽聽 (AbsoluteConstraints) constraints.get(comp);
聽聽聽聽聽 Dimension size = comp.getPreferredSize();
聽聽聽聽聽 int width = ac.getWidth();
聽聽聽聽聽 if (width == -1)
聽聽聽聽聽聽聽 width = size.width;
聽聽聽聽聽 int height = ac.getHeight();
聽聽聽聽聽 if (height == -1)
聽聽聽聽聽聽聽 height = size.height;
聽聽聽聽聽 comp.setBounds(ac.x, ac.y, width, height);
聽聽聽 }
聽 }
聽 public void addLayoutComponent
聽聽聽聽聽 (Component comp, Object constr) {
聽聽聽 if (! (constr instanceof AbsoluteConstraints))
聽聽聽聽聽 throw new
聽聽聽聽聽聽聽聽聽 IllegalArgumentException();
聽聽聽 constraints.put(comp, constr);
聽 }
聽 public Dimension maximumLayoutSize(Container
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 target) {
聽聽聽 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
聽 }
聽 public float getLayoutAlignmentX
聽聽聽聽聽 (Container target) {
聽聽聽 return 0;
聽 }
聽 public float getLayoutAlignmentY(Container target) {
聽聽聽 return 0;
聽 }
聽 public void invalidateLayout(Container target) {}
}
// 3DateField.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.awt.*;
public final class DateField
聽聽聽 extends Panel {
聽 private static final long serialVersionUID = 1L;
聽 private final TextField dateText = new TextField(12);
聽 private final Button dropdownButton = new Button();
聽 private DatePicker dp;
聽 private Dialog dlg;
聽 Point origin = new Point();
聽 final class Listener
聽聽聽聽聽 extends ComponentAdapter {
聽聽聽 public void componentHidden(final ComponentEvent evt) {
聽聽聽聽聽 final Date dt = ( (DatePicker) evt.getSource()).getDate();
聽聽聽聽聽 if (null != dt)
聽聽聽聽聽聽聽 dateText.setText(dateToString(dt));
聽聽聽聽聽 dlg.dispose();
聽聽聽 }
聽 }
聽 public DateField() {
聽聽聽 super();
聽聽聽 init();
聽 }
聽 public DateField(final Date initialDate) {
聽聽聽 super();
聽聽聽 init();
聽聽聽 dateText.setText(dateToString(initialDate));
聽 }
聽 public Date getDate() {
聽聽聽 return stringToDate(dateText.getText());
聽 }
聽 public void setDate(Date date) {
聽聽聽 String v = dateToString(date);
聽聽聽 if (v == null) {
聽聽聽聽聽 v = "";
聽聽聽 }
聽聽聽 dateText.setText(v);
聽 }
聽 private void init() {
聽聽聽 setLayout(new BorderLayout());
聽聽聽 dateText.setText("");
聽聽聽 dateText.setEditable(false);
聽聽聽 dateText.setBackground(new Color(255, 255, 255));
聽聽聽 add(dateText, BorderLayout.CENTER);
聽聽聽 dropdownButton.setLabel("閫夋嫨");
聽聽聽 dropdownButton.setBackground(Color.yellow);
聽聽聽 dropdownButton.addActionListener(new ActionListener() {
聽聽聽聽聽 public void actionPerformed(final ActionEvent evt) {
聽聽聽聽聽聽聽 onButtonClick(evt);
聽聽聽聽聽 }
聽聽聽 });
聽聽聽 add(dropdownButton, BorderLayout.EAST);
聽 }
聽 private void onButtonClick(final java.awt.event.ActionEvent evt) {
聽聽聽 if ("".equals(dateText.getText()))
聽聽聽聽聽 dp = new DatePicker();
聽聽聽 else
聽聽聽聽聽 dp = new DatePicker(stringToDate(dateText.getText()));
聽聽聽 dp.addComponentListener(new Listener());
聽聽聽 final Point p = dateText.getLocationOnScreen();
聽聽聽 p.setLocation(p.getX(), p.getY() - 1 + dateText.getSize().getHeight());
聽聽聽 dlg = new Dialog(new Frame(), true);
聽聽聽 dlg.addMouseListener(new MouseAdapter() {
聽聽聽聽聽 public void mousePressed(MouseEvent e) {
聽聽聽聽聽聽聽 origin.x = e.getX();
聽聽聽聽聽聽聽 origin.y = e.getY();
聽聽聽聽聽 }
聽聽聽 });
聽聽聽 dlg.addMouseMotionListener(new MouseMotionAdapter() {
聽聽聽聽聽 public void mouseDragged(MouseEvent e) {
聽聽聽聽聽聽聽 Point p = dlg.getLocation();
聽聽聽聽聽聽聽 dlg.setLocation(p.x + e.getX() - origin.x, p.y + e.getY() - origin.y);
聽聽聽聽聽 }
聽聽聽 });
聽聽聽 dlg.setLocation(p);
聽聽聽 dlg.setResizable(false);
聽聽聽 dlg.setUndecorated(true);
聽聽聽 dlg.add(dp);
聽聽聽 dlg.pack();
聽聽聽 dlg.setVisible(true);
聽 }
聽 private static String dateToString(final Date dt) {
聽聽聽 if (null != dt)
聽聽聽聽聽 return DateFormat.getDateInstance(DateFormat.LONG).format(dt);
聽聽聽 return null;
聽 }
聽 private static Date stringToDate(final String s) {
聽聽聽 try {
聽聽聽聽聽 return DateFormat.getDateInstance(DateFormat.LONG).parse(s);
聽聽聽 }
聽聽聽 catch (ParseException e) {
聽聽聽聽聽 return null;
聽聽聽 }
聽 }
聽 public static void main(String[] args) {
聽聽聽 Dialog dlg = new Dialog(new Frame(), true);
聽聽聽 DateField df = new DateField();
聽聽 //dlg.getContentPane().add(df);
聽聽 dlg.add(df);
聽聽聽 dlg.pack();
聽聽聽 dlg.setVisible(true);
聽聽聽 System.out.println(df.getDate().toString());
聽聽聽 System.exit(0);
聽 }
}
//4.DatePicker.java
import java.awt.*;
import java.awt.event.*;
import java.util.GregorianCalendar;
import java.util.Date;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.FieldPosition;
/*
import javax.swing.*;
import javax.swing.plaf.BorderUIResource;
*/
public final class DatePicker
聽聽聽 extends Panel {
聽 private static final long serialVersionUID = 1L;
聽 private static final int startX = 10;
聽 private static final int startY = 60;
聽 private static final Font smallFont = new Font("Dialog", Font.PLAIN, 10);
聽 private static final Font largeFont = new Font("Dialog", Font.PLAIN, 12);
聽 private static final Insets insets = new Insets(2, 2, 2, 2);
聽 private static final Color highlight = Color.YELLOW;//new Color(255, 255, 204);
聽 private static final Color white = new Color(255, 255, 255);
聽 private static final Color gray = new Color(204, 204, 204);
聽 private Component selectedDay = null;
聽 private GregorianCalendar selectedDate = null;
聽 private GregorianCalendar originalDate = null;
聽 private boolean hideOnSelect = true;
聽 private final Button backButton = new Button();
聽 private final Label monthAndYear = new Label();
聽 private final Button forwardButton = new Button();
聽 private final Label[] dayHeadings = new Label[] {
聽聽聽聽聽 new Label("鏃?),
聽聽聽聽聽 new Label("涓"),
聽聽聽聽聽 new Label("浜?),
聽聽聽聽聽 new Label("涓?),
聽聽聽聽聽 new Label("鍥?),
聽聽聽聽聽 new Label("浜?),
聽聽聽聽聽 new Label("鍏?)};
聽 private final Label[][] daysInMonth = new Label[][] {
聽聽聽聽聽 {
聽聽聽聽聽 new Label(), new Label(),
聽聽聽聽聽 new Label(), new Label(),
聽聽聽聽聽 new Label(), new Label(),
聽聽聽聽聽 new Label()}
聽聽聽聽聽 , {
聽聽聽聽聽 new Label(),
聽聽聽聽聽 new Label(), new Label(),
聽聽聽聽聽 new Label(), new Label(),
聽聽聽聽聽 new Label(), new Label()}
聽聽聽聽聽 , {
聽聽聽聽聽 new Label(), new Label(),
聽聽聽聽聽 new Label(), new Label(),
聽聽聽聽聽 new Label(), new Label(),
聽聽聽聽聽 new Label()}
聽聽聽聽聽 , {
聽聽聽聽聽 new Label(),
聽聽聽聽聽 new Label(), new Label(),
聽聽聽聽聽 new Label(), new Label(),
聽聽聽聽聽 new Label(), new Label()}
聽聽聽聽聽 , {
聽聽聽聽聽 new Label(), new Label(),
聽聽聽聽聽 new Label(), new Label(),
聽聽聽聽聽 new Label(), new Label(),
聽聽聽聽聽 new Label()}
聽聽聽聽聽 , {
聽聽聽聽聽 new Label(),
聽聽聽聽聽 new Label(), new Label(),
聽聽聽聽聽 new Label(), new Label(),
聽聽聽聽聽 new Label(), new Label()}
聽 };
聽 private final Button todayButton = new Button();
聽 private final Button cancelButton = new Button();
聽 public DatePicker() {
聽聽聽 super();
聽聽聽 selectedDate = getToday();
聽聽聽 init();
聽 }
聽 public DatePicker(final Date initialDate) {
聽聽聽 super();
聽聽聽 if (null == initialDate)
聽聽聽聽聽 selectedDate = getToday();
聽聽聽 else
聽聽聽聽聽 (selectedDate = new GregorianCalendar()).setTime(initialDate);
聽聽聽 originalDate = new GregorianCalendar(selectedDate.get(Calendar.YEAR),
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 selectedDate.get(Calendar.MONTH),
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 selectedDate.get(Calendar.DATE));
聽聽聽 init();
聽 }
聽 public boolean isHideOnSelect() {
聽聽聽 return hideOnSelect;
聽 }
聽 public void setHideOnSelect(final boolean hideOnSelect) {
聽聽聽 if (this.hideOnSelect != hideOnSelect) {
聽聽聽聽聽 this.hideOnSelect = hideOnSelect;
聽聽聽聽聽 initButtons(false);
聽聽聽 }
聽 }
聽 public Date getDate() {
聽聽聽 if (null != selectedDate)
聽聽聽聽聽 return selectedDate.getTime();
聽聽聽 return null;
聽 }
聽 private void init() {
聽聽聽 setLayout(new AbsoluteLayout());
聽聽聽 /*
聽聽聽 this.setMinimumSize(new Dimension(161, 226));
聽聽聽 this.setMaximumSize(getMinimumSize());
聽聽聽 this.setPreferredSize(getMinimumSize());
聽聽聽 this.setBorder(new BorderUIResource.EtchedBorderUIResource());
聽聽聽 */
聽聽 this.setSize(new Dimension(161, 226));
聽聽聽 backButton.setFont(smallFont);
聽聽聽 backButton.setLabel("<");
聽 //聽 backButton.setSize(insets);
聽//聽聽 backButton.setDefaultCapable(false);
聽聽聽 backButton.addActionListener(new ActionListener() {
聽聽聽聽聽 public void actionPerformed(final ActionEvent evt) {
聽聽聽聽聽聽聽 onBackClicked(evt);
聽聽聽聽聽 }
聽聽聽 });
聽聽聽 add(backButton, new AbsoluteConstraints(10, 10, 20, 20));
聽聽聽 monthAndYear.setFont(largeFont);
聽聽聽 monthAndYear.setAlignment((int)TextField.CENTER_ALIGNMENT);
聽聽聽 monthAndYear.setText(formatDateText(selectedDate.getTime()));
聽聽聽 add(monthAndYear, new AbsoluteConstraints(30, 10, 100, 20));
聽聽聽 forwardButton.setFont(smallFont);
聽聽聽 forwardButton.setLabel(">");
聽//聽聽 forwardButton.setMargin(insets);
//聽聽聽 forwardButton.setDefaultCapable(false);
聽聽聽 forwardButton.addActionListener(new ActionListener() {
聽聽聽聽聽 public void actionPerformed(final ActionEvent evt) {
聽聽聽聽聽聽聽 onForwardClicked(evt);
聽聽聽聽聽 }
聽聽聽 });
聽聽聽 add(forwardButton, new AbsoluteConstraints(130, 10, 20, 20));
聽聽聽 int x = startX;
聽聽聽 for (int ii = 0; ii < dayHeadings.length; ii++) {
聽聽聽 //聽 dayHeadings[ii].setOpaque(true);
聽聽聽聽聽 dayHeadings[ii].setBackground(Color.LIGHT_GRAY);
聽聽聽聽聽 dayHeadings[ii].setForeground(Color.WHITE);
聽聽聽聽聽 dayHeadings[ii].setAlignment((int)TextField.CENTER_ALIGNMENT);
聽聽聽 //聽 dayHeadings[ii].setHorizontalAlignment(Label.CENTER);
聽聽聽聽聽 add(dayHeadings[ii], new AbsoluteConstraints(x, 40, 21, 21));
聽聽聽聽聽 x += 20;
聽聽聽 }
聽聽聽 x = startX;
聽聽聽 int y = startY;
聽聽聽 for (int ii = 0; ii < daysInMonth.length; ii++) {
聽聽聽聽聽 for (int jj = 0; jj < daysInMonth[ii].length; jj++) {
聽聽聽聽聽 //聽 daysInMonth[ii][jj].setOpaque(true);
聽聽聽聽聽聽聽 daysInMonth[ii][jj].setBackground(white);
聽聽聽聽聽聽聽 daysInMonth[ii][jj].setFont(smallFont);
聽聽聽聽 //聽聽 daysInMonth[ii][jj].setHorizontalAlignment(Label.CENTER);
聽聽聽聽聽聽聽 daysInMonth[ii][jj].setText("");
聽聽聽聽聽聽聽 daysInMonth[ii][jj].addMouseListener(new MouseAdapter() {
聽聽聽聽聽聽聽聽聽 public void mouseClicked(final MouseEvent evt) {
聽聽聽聽聽聽聽聽聽聽聽 onDayClicked(evt);
聽聽聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽 });
聽聽聽聽聽聽聽 add(daysInMonth[ii][jj], new AbsoluteConstraints(x, y, 21, 21));
聽聽聽聽聽聽聽 x += 20;
聽聽聽聽聽 }
聽聽聽聽聽 x = startX;
聽聽聽聽聽 y += 20;
聽聽聽 }
聽聽聽 initButtons(true);
聽聽聽 calculateCalendar();
聽 }
聽 private void initButtons(final boolean firstTime) {
聽聽聽 if (firstTime) {
聽聽聽聽聽 final Dimension buttonSize = new Dimension(68, 24);
聽聽聽聽聽 todayButton.setLabel("浠婂ぉ");
聽聽聽聽聽 todayButton.setSize(buttonSize);
聽聽聽聽聽 /*
聽聽聽聽聽 todayButton.setMargin(insets);
聽聽聽聽聽 todayButton.setMaximumSize(buttonSize);
聽聽聽聽聽 todayButton.setMinimumSize(buttonSize);
聽聽聽聽聽 todayButton.setPreferredSize(buttonSize);
聽聽聽聽聽 todayButton.setDefaultCapable(true);
聽聽聽聽聽 todayButton.setSelected(true);
聽聽聽聽聽 */
聽聽聽聽聽 todayButton.addActionListener(new ActionListener() {
聽聽聽聽聽聽聽 public void actionPerformed(final ActionEvent evt) {
聽聽聽聽聽聽聽聽聽 onToday(evt);
聽聽聽聽聽聽聽 }
聽聽聽聽聽 });
聽聽聽聽聽 cancelButton.setLabel("鍙栨秷");
聽聽聽聽聽 cancelButton.setSize(buttonSize);
聽聽聽聽聽 /*
聽聽聽聽聽 cancelButton.setMargin(insets);
聽聽聽聽聽 cancelButton.setMaximumSize(buttonSize);
聽聽聽聽聽 cancelButton.setMinimumSize(buttonSize);
聽聽聽聽聽 cancelButton.setPreferredSize(buttonSize);
聽聽聽聽聽 */
聽聽聽聽聽 cancelButton.addActionListener(new ActionListener() {
聽聽聽聽聽聽聽 public void actionPerformed(final ActionEvent evt) {
聽聽聽聽聽聽聽聽聽 onCancel(evt);
聽聽聽聽聽聽聽 }
聽聽聽聽聽 });
聽聽聽 }
聽聽聽 else {
聽聽聽聽聽 this.remove(todayButton);
聽聽聽聽聽 this.remove(cancelButton);
聽聽聽 }
聽聽聽 if (hideOnSelect) {
聽聽聽聽聽 add(todayButton, new AbsoluteConstraints(25, 190, 52, -1));
聽聽聽聽聽 add(cancelButton, new AbsoluteConstraints(87, 190, 52, -1));
聽聽聽 }
聽聽聽 else {
聽聽聽聽聽 add(todayButton, new AbsoluteConstraints(55, 190, 52, -1));
聽聽聽 }
聽 }
聽 private void onToday(final java.awt.event.ActionEvent evt) {
聽聽聽 selectedDate = getToday();
聽聽聽 setVisible(!hideOnSelect);
聽聽聽 if (isVisible()) {
聽聽聽聽聽 monthAndYear.setText(formatDateText(selectedDate.getTime()));
聽聽聽聽聽 calculateCalendar();
聽聽聽 }
聽 }
聽 private void onCancel(final ActionEvent evt) {
聽聽聽 selectedDate = originalDate;
聽聽聽 setVisible(!hideOnSelect);
聽 }
聽 private void onForwardClicked(final java.awt.event.ActionEvent evt) {
聽聽聽 final int day = selectedDate.get(Calendar.DATE);
聽聽聽 selectedDate.set(Calendar.DATE, 1);
聽聽聽 selectedDate.add(Calendar.MONTH, 1);
聽聽聽 selectedDate.set(Calendar.DATE,
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Math.min(day, calculateDaysInMonth(selectedDate)));
聽聽聽 monthAndYear.setText(formatDateText(selectedDate.getTime()));
聽聽聽 calculateCalendar();
聽 }
聽 private void onBackClicked(final java.awt.event.ActionEvent evt) {
聽聽聽 final int day = selectedDate.get(Calendar.DATE);
聽聽聽 selectedDate.set(Calendar.DATE, 1);
聽聽聽 selectedDate.add(Calendar.MONTH, -1);
聽聽聽 selectedDate.set(Calendar.DATE,
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 Math.min(day, calculateDaysInMonth(selectedDate)));
聽聽聽 monthAndYear.setText(formatDateText(selectedDate.getTime()));
聽聽聽 calculateCalendar();
聽 }
聽 private void onDayClicked(final java.awt.event.MouseEvent evt) {
聽聽聽 final Label fld = (Label) evt.getSource();
聽聽聽 if (!"".equals(fld.getText())) {
聽聽聽聽聽 fld.setBackground(highlight);
聽聽聽聽聽 selectedDay = fld;
聽聽聽聽聽 selectedDate.set(Calendar.DATE, Integer.parseInt(fld.getText()));
聽聽聽聽聽 setVisible(!hideOnSelect);
聽聽聽 }
聽 }
聽 private static GregorianCalendar getToday() {
聽聽聽 final GregorianCalendar gc = new GregorianCalendar();
聽聽聽 gc.set(Calendar.HOUR_OF_DAY, 0);
聽聽聽 gc.set(Calendar.MINUTE, 0);
聽聽聽 gc.set(Calendar.SECOND, 0);
聽聽聽 gc.set(Calendar.MILLISECOND, 0);
聽聽聽 return gc;
聽 }
聽 private void calculateCalendar() {
聽聽聽 if (null != selectedDay) {
聽聽聽聽聽 selectedDay.setBackground(white);
聽聽聽聽聽 selectedDay = null;
聽聽聽 }
聽聽聽 final GregorianCalendar c = new GregorianCalendar(selectedDate.get(Calendar.
聽聽聽聽聽聽聽 YEAR), selectedDate.get(Calendar.MONTH), 1);
聽聽聽 final int maxDay = calculateDaysInMonth(c);
聽聽聽 final int selectedDay = Math.min(maxDay, selectedDate.get(Calendar.DATE));
聽聽聽 int dow = c.get(Calendar.DAY_OF_WEEK);
聽聽聽 for (int dd = 0; dd < dow; dd++) {
聽聽聽聽聽 daysInMonth[0][dd].setText("");
聽聽聽 }
聽聽聽 int week;
聽聽聽 do {
聽聽聽聽聽 week = c.get(Calendar.WEEK_OF_MONTH);
聽聽聽聽聽 dow = c.get(Calendar.DAY_OF_WEEK);
聽聽聽聽聽 final Label fld = this.daysInMonth[week - 1][dow - 1];
聽聽聽聽聽 fld.setText(Integer.toString(c.get(Calendar.DATE)));
聽聽聽聽聽 if (selectedDay == c.get(Calendar.DATE)) {
聽聽聽聽聽聽聽 fld.setBackground(highlight);
聽聽聽聽聽聽聽 this.selectedDay = fld;
聽聽聽聽聽 }
聽聽聽聽聽 if (c.get(Calendar.DATE) >= maxDay)
聽聽聽聽聽聽聽 break;
聽聽聽聽聽 c.add(Calendar.DATE, 1);
聽聽聽 }
聽聽聽 while (c.get(Calendar.DATE) <= maxDay); week--;
聽聽聽 for (int ww = week; ww < daysInMonth.length; ww++) {
聽聽聽聽聽 for (int dd = dow; dd < daysInMonth[ww].length; dd++) {
聽聽聽聽聽聽聽 daysInMonth[ww][dd].setText("");
聽聽聽聽聽 }
聽聽聽聽聽 dow = 0;
聽聽聽 }
聽聽聽 c.set(Calendar.DATE, selectedDay);
聽聽聽 selectedDate = c;
聽 }
聽 private static int calculateDaysInMonth(final Calendar c) {
聽聽聽 int daysInMonth = 0;
聽聽聽 switch (c.get(Calendar.MONTH)) {
聽聽聽聽聽 case 0:
聽聽聽聽聽 case 2:
聽聽聽聽聽 case 4:
聽聽聽聽聽 case 6:
聽聽聽聽聽 case 7:
聽聽聽聽聽 case 9:
聽聽聽聽聽 case 11:
聽聽聽聽聽聽聽 daysInMonth = 31;
聽聽聽聽聽聽聽 break;
聽聽聽聽聽 case 3:
聽聽聽聽聽 case 5:
聽聽聽聽聽 case 8:
聽聽聽聽聽 case 10:
聽聽聽聽聽聽聽 daysInMonth = 30;
聽聽聽聽聽聽聽 break;
聽聽聽聽聽 case 1:
聽聽聽聽聽聽聽 final int year = c.get(Calendar.YEAR);
聽聽聽聽聽聽聽 daysInMonth = (0 == year % 1000) ? 29 : (0 == year % 100) ? 28 :
聽聽聽聽聽聽聽聽聽聽聽 (0 == year % 4) ? 29 : 28;
聽聽聽聽聽聽聽 break;
聽聽聽 }
聽聽聽 return daysInMonth;
聽 }
聽 private static String formatDateText(final Date dt) {
聽聽聽 final DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
聽聽聽 final StringBuffer mm = new StringBuffer();
聽聽聽 final StringBuffer yy = new StringBuffer();
聽聽聽 final FieldPosition mmfp = new FieldPosition(DateFormat.MONTH_FIELD);
聽聽聽 final FieldPosition yyfp = new FieldPosition(DateFormat.YEAR_FIELD);
聽聽聽 df.format(dt, mm, mmfp);
聽聽聽 df.format(dt, yy, yyfp);
聽聽聽 return (mm.toString().substring(mmfp.getBeginIndex(), mmfp.getEndIndex()) +
聽聽聽聽聽聽聽聽聽聽聽 "鏈?" +
聽聽聽聽聽聽聽聽聽聽聽 yy.toString().substring(yyfp.getBeginIndex(), yyfp.getEndIndex()) +
聽聽聽聽聽聽聽聽聽聽聽 "騫?);
聽 }
}