日期时间选择器,精确到秒
GetDate.java
package com.tfj.demo; import java.awt.Dimension; import java.awt.Point; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.GregorianCalendar; import javax.swing.JButton; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JPanel; /** * @author Jacksile E-mail:[email protected] * @date 2016年4月13日 下午2:48:54 * @function */ public class GetDate extends JFrame { private JFormattedTextField datelineField; private DateChooserSecond dateChooser; private static int WIDTH = 400; private static int HEIGHT = 100; GetDate() { JPanel p = new JPanel(); setContentPane(p); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); datelineField = new JFormattedTextField(dateFormat); Calendar deadline = GregorianCalendar.getInstance(); datelineField.setValue(deadline.getTime()); p.add(datelineField); JButton b = new JButton("时间设置"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { if (dateChooser == null) { dateChooser = new DateChooserSecond(datelineField); } Point p = getLocationOnScreen(); p.y = p.y + 30; dateChooser.showDialog("日期时间选择器", p); } }); p.add(b); setSize(WIDTH, HEIGHT); //设置放在中间 Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int screenWidth = screenSize.width; int screenHeight = screenSize.height; setLocation((screenWidth - WIDTH) / 2, (screenHeight - HEIGHT) / 2); setVisible(true); setTitle("日期时间选择器"); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { new GetDate(); } }
DateChooserSecond.java
package com.tfj.demo; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dialog; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Frame; import java.awt.GridLayout; import java.awt.Point; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFormattedTextField; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.SpinnerNumberModel; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.border.LineBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; /** * @author Jacksile E-mail:[email protected] * @date 2016年4月13日 下午2:47:02 * @function */ public class DateChooserSecond extends JPanel implements ActionListener, ChangeListener { public DateChooserSecond(JFormattedTextField dateField) { this.dateField = dateField; setLayout(new BorderLayout()); setBorder(new LineBorder(backGroundColor, 2)); setBackground(backGroundColor); calendar = GregorianCalendar.getInstance(); JPanel topYearAndMonth = createYMHMSPanel(); add(topYearAndMonth, BorderLayout.NORTH); JPanel centerWeekAndDay = createWeekAndDayPanel(); add(centerWeekAndDay, BorderLayout.CENTER); } private JPanel createYMHMSPanel() { JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); panel.setBackground(controlLineColor); int currentYear = calendar.get(Calendar.YEAR); yearSpin = new JSpinner(new SpinnerNumberModel(currentYear, startYear, lastYear, 1)); yearSpin.setPreferredSize(new Dimension(48, 20)); yearSpin.setName("Year"); yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####")); yearSpin.addChangeListener(this); panel.add(yearSpin); JLabel yearLabel = new JLabel("年"); yearLabel.setForeground(controlTextColor); panel.add(yearLabel); int currentMonth = calendar.get(Calendar.MONTH) + 1; monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1, 12, 1)); monthSpin.setPreferredSize(new Dimension(35, 20)); monthSpin.setName("Month"); monthSpin.addChangeListener(this); panel.add(monthSpin); JLabel monthLabel = new JLabel("月"); monthLabel.setForeground(controlTextColor); panel.add(monthLabel); int currentHour = calendar.get(Calendar.HOUR_OF_DAY); hourSpin = new JSpinner(new SpinnerNumberModel(currentHour, 0, 23, 1)); hourSpin.setPreferredSize(new Dimension(35, 20)); hourSpin.setName("Hour"); hourSpin.addChangeListener(this); panel.add(hourSpin); JLabel seperatorLabel = new JLabel(":"); seperatorLabel.setForeground(controlTextColor); panel.add(seperatorLabel); int currentMinute = calendar.get(Calendar.MINUTE); minuteSpin = new JSpinner(new SpinnerNumberModel(currentMinute, 0, 59, 1)); minuteSpin.setPreferredSize(new Dimension(35, 20)); minuteSpin.setName("Minute"); minuteSpin.addChangeListener(this); panel.add(minuteSpin); seperatorLabel = new JLabel(":"); seperatorLabel.setForeground(controlTextColor); panel.add(seperatorLabel); int currentSecond = calendar.get(Calendar.SECOND); secondSpin = new JSpinner(new SpinnerNumberModel(currentSecond, 0, 59, 1)); secondSpin.setPreferredSize(new Dimension(35, 20)); secondSpin.setName("Second"); secondSpin.addChangeListener(this); panel.add(secondSpin); return panel; } private JPanel createWeekAndDayPanel() { String colname[] = { "SUN", "MON", "TUE", "WEN", "THU", "FRI", "SAT" }; JPanel panel = new JPanel(); // 设置固定字体,以免调用环境改变影响界面美观 panel.setFont(new Font("SansSerif", Font.PLAIN, 12)); panel.setLayout(new GridLayout(7, 7)); panel.setBackground(Color.white); JLabel cell = null; for (int i = 0; i < 7; i++) { cell = new JLabel(colname[i]); cell.setHorizontalAlignment(JLabel.RIGHT); if (i == 0 || i == 6) cell.setForeground(weekendFontColor); else cell.setForeground(weekFontColor); panel.add(cell); } int actionCommandId = 0; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { JButton numberButton = new JButton(); numberButton.setBorder(null); numberButton.setHorizontalAlignment(SwingConstants.RIGHT); numberButton.setActionCommand(String.valueOf(actionCommandId)); numberButton.addActionListener(this); numberButton.setBackground(palletTableColor); if (j == 0 || j == 6) numberButton.setForeground(weekendFontColor); else numberButton.setForeground(dateFontColor); daysButton[i][j] = numberButton; panel.add(numberButton); actionCommandId++; } } return panel; } private JDialog createDialog(String title) { Window window = SwingUtilities.getWindowAncestor(dateField); JDialog dialog = null; if (window instanceof Frame) { dialog = new DateChooserDialog(this, (Frame) window, title); } else { dialog = new DateChooserDialog(this, (Dialog) window, title); } dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); dialog.getContentPane().add(this, BorderLayout.CENTER); dialog.pack(); dialog.setSize(width, height); return dialog; } Point getAppropriateLocation(Frame owner, Point position) { Point result = new Point(position); Point p = owner.getLocation(); int offsetX = (position.x + width) - (p.x + owner.getWidth()); int offsetY = (position.y + height) - (p.y + owner.getHeight()); if (offsetX > 0) { result.x -= offsetX; } if (offsetY > 0) { result.y -= offsetY; } return result; } void setDateField() { dateField.setValue(calendar.getTime()); } public void showDialog(String title, Point position) { Frame owner = (Frame) SwingUtilities.getWindowAncestor(dateField); if (dialog == null || dialog.getOwner() != owner) dialog = createDialog(title); dialog.setLocation(getAppropriateLocation(owner, position)); flushCalendar(); dialog.setVisible(true); } private void flushCalendar() { calendar.setTime((Date) dateField.getValue()); int currentYear = calendar.get(Calendar.YEAR); yearSpin.setValue(currentYear); int currentMonth = calendar.get(Calendar.MONTH) + 1; monthSpin.setValue(currentMonth); int currentHour = calendar.get(Calendar.HOUR_OF_DAY); hourSpin.setValue(currentHour); int currentMinute = calendar.get(Calendar.MINUTE); minuteSpin.setValue(currentMinute); int currentSecond = calendar.get(Calendar.SECOND); secondSpin.setValue(currentSecond); dayColorUpdate(true); flushWeekAndDay(); } private void dayColorUpdate(boolean isOldDay) { int day = calendar.get(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, 1); int actionCommandId = day - 2 + calendar.get(Calendar.DAY_OF_WEEK); calendar.set(Calendar.DAY_OF_MONTH, day); int i = actionCommandId / 7; int j = actionCommandId % 7; if (isOldDay == true) { if (j == 0 || j == 6) daysButton[i][j].setForeground(weekendFontColor); else daysButton[i][j].setForeground(dateFontColor); } else daysButton[i][j].setForeground(todayBackColor); } private void flushWeekAndDay() { int day = calendar.get(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, 1); int maxDayNo = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); int dayNo = 2 - calendar.get(Calendar.DAY_OF_WEEK); calendar.set(Calendar.DAY_OF_MONTH, day); for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { String s = ""; if (dayNo >= 1 && dayNo <= maxDayNo) s = String.valueOf(dayNo); daysButton[i][j].setText(s); dayNo++; } } dayColorUpdate(false); } private int getSelectedYear() { return ((Integer) yearSpin.getValue()).intValue(); } private int getSelectedMonth() { return ((Integer) monthSpin.getValue()).intValue(); } private int getSelectedHour() { return ((Integer) hourSpin.getValue()).intValue(); } private int getSelectedMinute() { return ((Integer) minuteSpin.getValue()).intValue(); } private int getSelectedSecond() { return ((Integer) secondSpin.getValue()).intValue(); } public void stateChanged(ChangeEvent e) { JSpinner source = (JSpinner) e.getSource(); if (source.getName().equals("Second")) { calendar.set(Calendar.SECOND, getSelectedSecond()); return; } if (source.getName().equals("Minute")) { calendar.set(Calendar.MINUTE, getSelectedMinute()); return; } if (source.getName().equals("Hour")) { calendar.set(Calendar.HOUR_OF_DAY, getSelectedHour()); return; } dayColorUpdate(true); if (source.getName().equals("Year")) calendar.set(Calendar.YEAR, getSelectedYear()); else calendar.set(Calendar.MONTH, getSelectedMonth() - 1); flushWeekAndDay(); } public void actionPerformed(ActionEvent e) { JButton source = (JButton) e.getSource(); if (source.getText().length() == 0) return; dayColorUpdate(true); source.setForeground(todayBackColor); int newDay = Integer.parseInt(source.getText()); calendar.set(Calendar.DAY_OF_MONTH, newDay); } int startYear = 1980; // 默认【最小】显示年份 int lastYear = 2050; // 默认【最大】显示年份 int width = 320; // 界面宽度 int height = 200; // 界面高度 Color backGroundColor = Color.gray; // 底色 // 月历表格配色----------------// Color palletTableColor = Color.white; // 日历表底色 Color todayBackColor = Color.orange; // 今天背景色 Color weekFontColor = Color.blue; // 星期文字色 Color dateFontColor = Color.black; // 日期文字色 Color weekendFontColor = Color.red; // 周末文字色 // 控制条配色------------------// Color controlLineColor = Color.pink; // 控制条底色 Color controlTextColor = Color.white; // 控制条标签文字色 Color rbFontColor = Color.white; // RoundBox文字色 Color rbBorderColor = Color.red; // RoundBox边框色 Color rbButtonColor = Color.pink; // RoundBox按钮色 Color rbBtFontColor = Color.red; // RoundBox按钮文字色 JDialog dialog; JSpinner yearSpin; JSpinner monthSpin; JButton[][] daysButton = new JButton[6][7]; JSpinner hourSpin; JSpinner minuteSpin; JSpinner secondSpin; Calendar calendar = null; JFormattedTextField dateField; private class DateChooserDialog extends JDialog { DateChooserDialog(DateChooserSecond dateChooser, Frame frame, String title) { super(frame, title, true); this.dateChooser = dateChooser; } DateChooserDialog(DateChooserSecond dateChooser, Dialog dialog, String title) { super(dialog, title, true); this.dateChooser = dateChooser; } public void hide() { dateChooser.setDateField(); super.hide(); } private DateChooserSecond dateChooser; } }
日期时间选择器,精确到天,相当于日历
DateChooserDay.java
package com.tfj.demo; import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Cursor; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridLayout; import java.awt.Point; import java.awt.Polygon; import java.awt.Stroke; import java.awt.Toolkit; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Comparator; import java.util.Date; import java.util.List; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Popup; import javax.swing.PopupFactory; import javax.swing.SwingUtilities; import javax.swing.event.AncestorEvent; import javax.swing.event.AncestorListener; /** * @author Jacksile E-mail:[email protected] * @date 2016年4月13日 下午3:32:23 * @function */ public class DateChooserDay extends JPanel { private static final long serialVersionUID = 4529266044762990227L; private Date initDate; private Calendar now = Calendar.getInstance(); private Calendar select; private JPanel monthPanel;// 月历 private JP1 jp1;// 四块面板,组成 private JP2 jp2; private JP3 jp3; private JP4 jp4; private Font font = new Font("宋体", Font.PLAIN, 12); private final LabelManager lm = new LabelManager(); private JLabel showDate; // ,toSelect; private SimpleDateFormat sdf; private boolean isShow = false; private Popup pop; private static final int WIDTH = 400; private static final int HEIGHT = 100; /** * Creates a new instance of DateChooser */ public DateChooserDay() { this(new Date()); } public DateChooserDay(Date date) { this(date, "yyyy年MM月dd日"); } public DateChooserDay(String format) { this(new Date(), format); } public DateChooserDay(Date date, String format) { initDate = date; sdf = new SimpleDateFormat(format); select = Calendar.getInstance(); select.setTime(initDate); initPanel(); initLabel(); } /** * 是否允许用户选择 */ public void setEnabled(boolean b) { super.setEnabled(b); showDate.setEnabled(b); } /** * 得到当前选择框的日期 */ public Date getDate() { return select.getTime(); } // 根据初始化的日期,初始化面板 private void initPanel() { monthPanel = new JPanel(new BorderLayout()); monthPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE)); JPanel up = new JPanel(new BorderLayout()); up.add(jp1 = new JP1(), BorderLayout.NORTH); up.add(jp2 = new JP2(), BorderLayout.CENTER); monthPanel.add(jp3 = new JP3(), BorderLayout.CENTER); monthPanel.add(up, BorderLayout.NORTH); monthPanel.add(jp4 = new JP4(), BorderLayout.SOUTH); this.addAncestorListener(new AncestorListener() { public void ancestorAdded(AncestorEvent event) { } public void ancestorRemoved(AncestorEvent event) { } // 只要祖先组件一移动,马上就让popup消失 public void ancestorMoved(AncestorEvent event) { hidePanel(); } }); } // 初始化标签 private void initLabel() { showDate = new JLabel(sdf.format(initDate)); showDate.setRequestFocusEnabled(true); showDate.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { showDate.requestFocusInWindow(); } }); // toSelect=new JLabel(sdf.format(initDate)); // toSelect.setBorder(BorderFactory.createLineBorder(Color.BLACK)); // toSelect.setRequestFocusEnabled(true); this.setBackground(Color.WHITE); this.add(showDate, BorderLayout.CENTER); // this.add(toSelect,BorderLayout.EAST); this.setPreferredSize(new Dimension(90, 25)); this.setBorder(BorderFactory.createLineBorder(Color.GRAY)); showDate.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent me) { if (showDate.isEnabled()) { showDate.setCursor(new Cursor(Cursor.HAND_CURSOR)); showDate.setForeground(Color.RED); } } public void mouseExited(MouseEvent me) { if (showDate.isEnabled()) { showDate.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); showDate.setForeground(Color.BLACK); } } public void mousePressed(MouseEvent me) { if (showDate.isEnabled()) { showDate.setForeground(Color.CYAN); if (isShow) { hidePanel(); } else { showPanel(showDate); } } } public void mouseReleased(MouseEvent me) { if (showDate.isEnabled()) { showDate.setForeground(Color.BLACK); } } }); showDate.addFocusListener(new FocusListener() { public void focusLost(FocusEvent e) { hidePanel(); } public void focusGained(FocusEvent e) { } }); } // 根据新的日期刷新 private void refresh() { jp1.updateDate(); jp3.updateDate(); SwingUtilities.updateComponentTreeUI(this); } // 提交日期 private void commit() { System.out.println("选中的日期是:" + sdf.format(select.getTime())); showDate.setText(sdf.format(select.getTime())); hidePanel(); } // 隐藏日期选择面板 private void hidePanel() { if (pop != null) { isShow = false; pop.hide(); pop = null; } } // 显示日期选择面板 private void showPanel(Component owner) { if (pop != null) { pop.hide(); } Point show = new Point(0, showDate.getHeight()); SwingUtilities.convertPointToScreen(show, showDate); Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); int x = show.x; int y = show.y; if (x < 0) { x = 0; } if (x > size.width - 295) { x = size.width - 295; } if (y < size.height - 170) { } else { y -= 188; } pop = PopupFactory.getSharedInstance().getPopup(owner, monthPanel, x, y); pop.show(); isShow = true; } /** * 最上面的面板用来显示月份的增减 */ private class JP1 extends JPanel { JLabel yearleft, yearright, monthleft, monthright, center, centercontainer; public JP1() { super(new BorderLayout()); this.setBackground(new Color(160, 185, 215)); initJP1(); } private void initJP1() { yearleft = new JLabel(" <<", JLabel.CENTER); yearleft.setToolTipText("上一年"); yearright = new JLabel(">> ", JLabel.CENTER); yearright.setToolTipText("下一年"); yearleft.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0)); yearright.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0)); monthleft = new JLabel(" <", JLabel.RIGHT); monthleft.setToolTipText("上一月"); monthright = new JLabel("> ", JLabel.LEFT); monthright.setToolTipText("下一月"); monthleft.setBorder(BorderFactory.createEmptyBorder(2, 30, 0, 0)); monthright.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 30)); centercontainer = new JLabel("", JLabel.CENTER); centercontainer.setLayout(new BorderLayout()); center = new JLabel("", JLabel.CENTER); centercontainer.add(monthleft, BorderLayout.WEST); centercontainer.add(center, BorderLayout.CENTER); centercontainer.add(monthright, BorderLayout.EAST); this.add(yearleft, BorderLayout.WEST); this.add(centercontainer, BorderLayout.CENTER); this.add(yearright, BorderLayout.EAST); this.setPreferredSize(new Dimension(295, 25)); updateDate(); yearleft.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent me) { yearleft.setCursor(new Cursor(Cursor.HAND_CURSOR)); yearleft.setForeground(Color.RED); } public void mouseExited(MouseEvent me) { yearleft.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); yearleft.setForeground(Color.BLACK); } public void mousePressed(MouseEvent me) { select.add(Calendar.YEAR, -1); yearleft.setForeground(Color.WHITE); refresh(); } public void mouseReleased(MouseEvent me) { yearleft.setForeground(Color.BLACK); } }); yearright.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent me) { yearright.setCursor(new Cursor(Cursor.HAND_CURSOR)); yearright.setForeground(Color.RED); } public void mouseExited(MouseEvent me) { yearright.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); yearright.setForeground(Color.BLACK); } public void mousePressed(MouseEvent me) { select.add(Calendar.YEAR, 1); yearright.setForeground(Color.WHITE); refresh(); } public void mouseReleased(MouseEvent me) { yearright.setForeground(Color.BLACK); } }); monthleft.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent me) { monthleft.setCursor(new Cursor(Cursor.HAND_CURSOR)); monthleft.setForeground(Color.RED); } public void mouseExited(MouseEvent me) { monthleft.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); monthleft.setForeground(Color.BLACK); } public void mousePressed(MouseEvent me) { select.add(Calendar.MONTH, -1); monthleft.setForeground(Color.WHITE); refresh(); } public void mouseReleased(MouseEvent me) { monthleft.setForeground(Color.BLACK); } }); monthright.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent me) { monthright.setCursor(new Cursor(Cursor.HAND_CURSOR)); monthright.setForeground(Color.RED); } public void mouseExited(MouseEvent me) { monthright.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); monthright.setForeground(Color.BLACK); } public void mousePressed(MouseEvent me) { select.add(Calendar.MONTH, 1); monthright.setForeground(Color.WHITE); refresh(); } public void mouseReleased(MouseEvent me) { monthright.setForeground(Color.BLACK); } }); } private void updateDate() { center.setText(select.get(Calendar.YEAR) + "年" + (select.get(Calendar.MONTH) + 1) + "月"); } } private class JP2 extends JPanel { public JP2() { this.setPreferredSize(new Dimension(295, 20)); } protected void paintComponent(Graphics g) { g.setFont(font); g.drawString("星期日 星期一 星期二 星期三 星期四 星期五 星期六", 5, 10); g.drawLine(0, 15, getWidth(), 15); } } private class JP3 extends JPanel { public JP3() { super(new GridLayout(6, 7)); this.setPreferredSize(new Dimension(295, 100)); initJP3(); } private void initJP3() { updateDate(); } public void updateDate() { this.removeAll(); lm.clear(); Date temp = select.getTime(); Calendar select = Calendar.getInstance(); select.setTime(temp); select.set(Calendar.DAY_OF_MONTH, 1); int index = select.get(Calendar.DAY_OF_WEEK); int sum = (index == 1 ? 8 : index); select.add(Calendar.DAY_OF_MONTH, 0 - sum); for (int i = 0; i < 42; i++) { select.add(Calendar.DAY_OF_MONTH, 1); lm.addLabel(new MyLabel(select.get(Calendar.YEAR), select.get(Calendar.MONTH), select.get(Calendar.DAY_OF_MONTH))); } for (MyLabel my : lm.getLabels()) { this.add(my); } select.setTime(temp); } } private class MyLabel extends JLabel implements Comparator<MyLabel>, MouseListener, MouseMotionListener { private int year, month, day; private boolean isSelected; public MyLabel(int year, int month, int day) { super("" + day, JLabel.CENTER); this.year = year; this.day = day; this.month = month; this.addMouseListener(this); this.addMouseMotionListener(this); this.setFont(font); if (month == select.get(Calendar.MONTH)) { this.setForeground(Color.BLACK); } else { this.setForeground(Color.LIGHT_GRAY); } if (day == select.get(Calendar.DAY_OF_MONTH)) { this.setBackground(new Color(160, 185, 215)); } else { this.setBackground(Color.WHITE); } } public boolean getIsSelected() { return isSelected; } public void setSelected(boolean b, boolean isDrag) { isSelected = b; if (b && !isDrag) { int temp = select.get(Calendar.MONTH); select.set(year, month, day); if (temp == month) { SwingUtilities.updateComponentTreeUI(jp3); } else { refresh(); } } this.repaint(); } protected void paintComponent(Graphics g) { if (day == select.get(Calendar.DAY_OF_MONTH) && month == select.get(Calendar.MONTH)) { // 如果当前日期是选择日期,则高亮显示 g.setColor(new Color(160, 185, 215)); g.fillRect(0, 0, getWidth(), getHeight()); } if (year == now.get(Calendar.YEAR) && month == now.get(Calendar.MONTH) && day == now.get(Calendar.DAY_OF_MONTH)) { // 如果日期和当前日期一样,则用红框 Graphics2D gd = (Graphics2D) g; gd.setColor(Color.RED); Polygon p = new Polygon(); p.addPoint(0, 0); p.addPoint(getWidth() - 1, 0); p.addPoint(getWidth() - 1, getHeight() - 1); p.addPoint(0, getHeight() - 1); gd.drawPolygon(p); } if (isSelected) {// 如果被选中了就画出一个虚线框出来 Stroke s = new BasicStroke(1.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL, 1.0f, new float[] { 2.0f, 2.0f }, 1.0f); Graphics2D gd = (Graphics2D) g; gd.setStroke(s); gd.setColor(Color.BLACK); Polygon p = new Polygon(); p.addPoint(0, 0); p.addPoint(getWidth() - 1, 0); p.addPoint(getWidth() - 1, getHeight() - 1); p.addPoint(0, getHeight() - 1); gd.drawPolygon(p); } super.paintComponent(g); } public boolean contains(Point p) { return this.getBounds().contains(p); } private void update() { repaint(); } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { isSelected = true; update(); } public void mouseReleased(MouseEvent e) { Point p = SwingUtilities.convertPoint(this, e.getPoint(), jp3); lm.setSelect(p, false); commit(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseDragged(MouseEvent e) { Point p = SwingUtilities.convertPoint(this, e.getPoint(), jp3); lm.setSelect(p, true); } public void mouseMoved(MouseEvent e) { } public int compare(MyLabel o1, MyLabel o2) { Calendar c1 = Calendar.getInstance(); c1.set(o1.year, o2.month, o1.day); Calendar c2 = Calendar.getInstance(); c2.set(o2.year, o2.month, o2.day); return c1.compareTo(c2); } } private class LabelManager { private List<MyLabel> list; public LabelManager() { list = new ArrayList<MyLabel>(); } public List<MyLabel> getLabels() { return list; } public void addLabel(MyLabel my) { list.add(my); } public void clear() { list.clear(); } public void setSelect(MyLabel my, boolean b) { for (MyLabel m : list) { if (m.equals(my)) { m.setSelected(true, b); } else { m.setSelected(false, b); } } } public void setSelect(Point p, boolean b) { // 如果是拖动,则要优化一下,以提高效率 if (b) { // 表示是否能返回,不用比较完所有的标签,能返回的标志就是把上一个标签和 // 将要显示的标签找到了就可以了 boolean findPrevious = false, findNext = false; for (MyLabel m : list) { if (m.contains(p)) { findNext = true; if (m.getIsSelected()) { findPrevious = true; } else { m.setSelected(true, b); } } else if (m.getIsSelected()) { findPrevious = true; m.setSelected(false, b); } if (findPrevious && findNext) { return; } } } else { MyLabel temp = null; for (MyLabel m : list) { if (m.contains(p)) { temp = m; } else if (m.getIsSelected()) { m.setSelected(false, b); } } if (temp != null) { temp.setSelected(true, b); } } } } private class JP4 extends JPanel { public JP4() { super(new BorderLayout()); this.setPreferredSize(new Dimension(295, 20)); this.setBackground(new Color(160, 185, 215)); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); final JLabel jl = new JLabel("今天: " + sdf.format(new Date())); jl.setToolTipText("点击选择今天日期"); this.add(jl, BorderLayout.CENTER); jl.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent me) { jl.setCursor(new Cursor(Cursor.HAND_CURSOR)); jl.setForeground(Color.RED); } public void mouseExited(MouseEvent me) { jl.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); jl.setForeground(Color.BLACK); } public void mousePressed(MouseEvent me) { jl.setForeground(Color.WHITE); select.setTime(new Date()); refresh(); commit(); } public void mouseReleased(MouseEvent me) { jl.setForeground(Color.BLACK); } }); } } public static void main(String[] args) { final DateChooserDay mp = new DateChooserDay("yyyy-MM-dd"); JFrame jf = new JFrame("日期选择器"); jf.add(mp, BorderLayout.CENTER); jf.add(new JButton("点击下面可选择时间"), BorderLayout.NORTH); jf.setSize(WIDTH, HEIGHT); jf.setLocationRelativeTo(null); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
时间: 2024-10-24 05:14:31