import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class JDateSelectPanel extends JPanel {
String DATE_FORMAT_ALL = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat STAND_DATE_FORMAT = new SimpleDateFormat(DATE_FORMAT_ALL);
String[] heads = { "日", "一", "二", "三", "四", "五", "六" };
int col = 7;
int totalday = 42;
JPanel daypanel = new JPanel();
JPanel selectpanel = new JPanel();
Calendar cal = Calendar.getInstance();
JTextField dayfiled = new JTextField(getCurrentTimeDesc());
JTextField datetime = new JTextField();
JPopupMenu pop = new JPopupMenu();
public JDateSelectPanel() {
setLayout(new GridLayout());
initDayPanel();
datetime.setHorizontalAlignment(JTextField.CENTER);
datetime.setText(getCurrentTimeDesc());
datetime.setEditable(false);
datetime.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e)){
Component component = e.getComponent();
int y = component.getY()+ component.getHeight();
pop.show(component, component.getX()+1, y+1);
pop.setVisible(true);
}
}
});
add(datetime);
}
public void initDayPanel(){
pop.add(selectpanel);
daypanel.setLayout(new GridLayout(6, 7, 1, 1));
dayfiled.setHorizontalAlignment(JTextField.CENTER);
JPanel headpanel = new JPanel();
headpanel.setLayout(new GridLayout(1, 7, 1, 1));
JLabel xinqi = null;
for (int i = 0; i < col; i++) {
xinqi = new JLabel(heads[i], JLabel.CENTER);
xinqi.setBorder(BorderFactory.createRaisedBevelBorder());
xinqi.setFont(new Font("微软雅黑", Font.BOLD, 13));
headpanel.add(xinqi);
}
selectpanel.setLayout(new GridBagLayout());
selectpanel.add(getButtonPanel(), new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
selectpanel.add(headpanel, new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
paintSelectPanel();
}
public JPanel getButtonPanel() {
JPanel buttonpanel = new JPanel();
buttonpanel.setLayout(new GridBagLayout());
JButton preyear = new JButton("<<");
JButton premonth = new JButton("<");
JButton nextmonth = new JButton(">");
JButton nextyear = new JButton(">>");
preyear.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
cal.add(Calendar.YEAR, -1);
refreshDayFiled();
}
});
premonth.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
cal.add(Calendar.MONTH, -1);
refreshDayFiled();
}
});
nextmonth.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
cal.add(Calendar.MONTH, 1);
refreshDayFiled();
}
});
nextyear.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
cal.add(Calendar.YEAR, 1);
refreshDayFiled();
}
});
buttonpanel.add(preyear, new GridBagConstraints(0, 0, 1, 1, 0.6, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
buttonpanel.add(premonth, new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
buttonpanel.add(dayfiled, new GridBagConstraints(2, 0, 3, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 1, 0, 1), 0, 0));
buttonpanel.add(nextmonth, new GridBagConstraints(5, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
buttonpanel.add(nextyear, new GridBagConstraints(6, 0, 1, 1, 0.6, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
return buttonpanel;
}
public void refreshDayFiled() {
dayfiled.setText(getTimeDesc(cal.getTimeInMillis()));
dayfiled.repaint();
paintSelectPanel();
}
public void fillSelectPanel() {
daypanel.removeAll();
int startindex = getStartIndex();
int total = startindex + getTotalDays();
JLabel kong = null;
for (int i = 0; i < startindex; i++) {
kong = new JLabel();
daypanel.add(kong);
}
int dayindex = 1;
for (int i = startindex; i < total; i++) {
final JLabel day = new JLabel(String.valueOf(dayindex++), JLabel.CENTER);
day.setBorder(BorderFactory.createRaisedBevelBorder());
day.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
day.setBackground(Color.BLUE);
day.setBorder(BorderFactory.createLoweredBevelBorder());
}
public void mouseExited(MouseEvent e) {
day.setBackground(null);
day.setBorder(BorderFactory.createRaisedBevelBorder());
}
public void mouseReleased(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e)){
JLabel label = (JLabel)e.getSource();
day.setBackground(null);
day.setBorder(BorderFactory.createRaisedBevelBorder());
int day = Integer.parseInt(label.getText());
cal.set(Calendar.DAY_OF_MONTH, day);
datetime.setText(getDateTime());
pop.setVisible(false);
}
}
});
if (i % 7 == 0 || (i + 1) % 7 == 0) {
day.setForeground(Color.RED);
}
daypanel.add(day);
}
for (int i = total; i < totalday; i++) {
kong = new JLabel();
daypanel.add(kong);
}
daypanel.updateUI();
}
public void paintSelectPanel() {
fillSelectPanel();
selectpanel.add(daypanel, new GridBagConstraints(0, 2, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
}
public int getStartIndex() {
int today = cal.get(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, 1);
int start = cal.get(Calendar.DAY_OF_WEEK);
cal.set(Calendar.DAY_OF_MONTH, today);
return start == 1 ? 1 : (start - 1);
}
public int getTotalDays() {
return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
public String getDateTime() {
return getTimeDesc(cal.getTimeInMillis());
}
public Calendar getDate() {
return cal;
}
public String getCurrentTimeDesc() {
return STAND_DATE_FORMAT.format(new Date(System.currentTimeMillis()));
}
public String getTimeDesc(long time) {
return STAND_DATE_FORMAT.format(new Date(time));
}
}
测试代码
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class test {
public static void main(String[] args) {
JFrame mainframe = new JFrame("日期选择器");
mainframe.setLayout(new GridBagLayout());
mainframe.add(new JLabel("时间:"), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
mainframe.add(new JDateSelectPanel(), new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.setSize(400, 300);
mainframe.setVisible(true);
}
}