package fm5.console.util;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
* 日期时间选择对话框,模仿Windows日期和时间属性对话框
*
*
*/
public class DateChooserJDialog extends JDialog implements ChangeListener,
ItemListener {
private JComboBox month = new JComboBox(new String[]{"一月", "二月", "三月",
"四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"});
private JSpinner year = new JSpinner(new SpinnerNumberModel(1, 1, 9999, 1));
private DateGrid dateGrid = new DateGrid();
private JSpinner time = new JSpinner(new SpinnerDateModel());
private JButton ok = new JButton("确认");
private JButton cancle = new JButton("取消");
private JPanel buttonPanel = new JPanel();
private JPanel datePanel = new JPanel();
private String ymdt = "";
private String date = "";
private String timeStamp = "";
/**
* Description:初始时间设置面板
*
* @param initialDate 日历操作句柄
* @param taskConfigFrame 任务设置界面句柄 final TaskConfigFrame taskConfigFrame
*/
public DateChooserJDialog() {
Calendar initialDate = new GregorianCalendar();
JPanel cp = new JPanel();
cp.setLayout(new BorderLayout());
cp.setBackground(Color.white);
cp.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
datePanel.setLayout(new GridLayout(1, 2, 5, 10));
JPanel p1 = new JPanel();
p1.setBackground(Color.white);
p1.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("日期"), BorderFactory.createEmptyBorder(5,
5, 10, 5)));
p1.setLayout(new BorderLayout(10, 10));
JPanel p11 = new JPanel(new GridLayout(1, 2, 10, 10));
p11.setBackground(Color.white);
p11.add(month);
month.addItemListener(this);
month.setPreferredSize(new Dimension(0, 20));
p11.add(year);
year.setEditor(new JSpinner.NumberEditor(year, "0"));
year.addChangeListener(this);
year.setPreferredSize(new Dimension(0, 20));
p1.add(p11, BorderLayout.NORTH);
p1.add(dateGrid);
datePanel.add(p1);
JPanel p2 = new JPanel(new BorderLayout(10, 10));
p2.setBackground(Color.white);
p2.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("时间"), BorderFactory.createEmptyBorder(0,
10, 10, 10)));
time.setPreferredSize(new Dimension(0, 20));
JSpinner.DateEditor editor = new JSpinner.DateEditor(time, "HH:mm:ss");
editor.getTextField().setHorizontalAlignment(JTextField.RIGHT);
time.setEditor(editor);
JPanel p = new JPanel();
p.setBackground(Color.white);
time.setPreferredSize(new Dimension(100, 20));
p.add(time);
p2.add(p, BorderLayout.SOUTH);
p2.add(new Clock(time));
datePanel.add(p2);
setCalendar(initialDate);
setPreferredSize(new Dimension(369, 200));
buttonPanel.add(ok);
buttonPanel.add(cancle);
cp.add(datePanel, BorderLayout.CENTER);
cp.add(buttonPanel, BorderLayout.SOUTH);
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String y = String.valueOf(dateGrid.year);
String m = String.valueOf(dateGrid.month);
String d = String.valueOf(dateGrid.day);
if (dateGrid.month < 10) {
m = "0" + dateGrid.month;
}
if (dateGrid.day < 10) {
d = "0" + dateGrid.day;
}
String t = time.getValue().toString().substring(11, 19);
ymdt = y + "-" + m + "-" + d + " " + t;
date = y + "-" + m + "-" + d;
timeStamp = t;
DateChooserJDialog.this.setVisible(false);
}
});
cancle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DateChooserJDialog.this.dispose();
}
});
add(cp, BorderLayout.CENTER);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
this.setTitle("日期和时间设置");
this.setModal(true);
this.setLocation(screenWidth / 3, screenHeight / 3);
this.setSize(400, 280);
this.setResizable(false);
}
public String getDatetime() {
this.setVisible(true);
return ymdt;
}
public String getTime() {
this.setVisible(true);
return timeStamp;
}
public String getDate() {
this.setVisible(true);
return date;
}
/**
* 设置日期和时间
*
* @param calendar 日期和时间
*/
public void setCalendar(Calendar calendar) {
if (calendar == null) {
calendar = new GregorianCalendar();
}
month.setSelectedIndex(calendar.get(Calendar.MONTH));
year.setValue(calendar.get(Calendar.YEAR));
int year = Integer.parseInt(this.year.getValue().toString());
int month = this.month.getSelectedIndex() + 1;
dateGrid.set(year, month);
dateGrid.setDay(calendar.get(Calendar.DAY_OF_MONTH));
time.setValue(calendar.getTime());
}
/**
* 获得日期和时间
*
* @return 日期和时间
*/
public Calendar getCalendar() {
int year = Integer.parseInt(this.year.getValue().toString());
int month = this.month.getSelectedIndex();
int day = dateGrid.getDay();
Calendar c = Calendar.getInstance();
Date date = (Date) time.getValue();
c.setTime(date);
c.set(year, month, day);
return c;
}
public void stateChanged(ChangeEvent e) {
dateGrid.set(Integer.parseInt(year.getValue().toString()), month.getSelectedIndex() + 1);
}
public void itemStateChanged(ItemEvent e) {
dateGrid.set(Integer.parseInt(year.getValue().toString()), month.getSelectedIndex() + 1);
}
private class ActionHandler extends WindowAdapter implements
ActionListener {
private DateChooserJDialog dc;
public ActionHandler(DateChooserJDialog dc) {
this.dc = dc;
}
public void windowClosing(WindowEvent e) {
actionPerformed(new ActionEvent(this, 0, "取消"));
}
public void actionPerformed(ActionEvent e) {
JDialog dlg = (JDialog) SwingUtilities.windowForComponent(dc);
dlg.dispose();
if ("取消".equals(e.getActionCommand())) {
dc = null;
}
}
public Calendar getCalendar() {
return dc == null ? null : dc.getCalendar();
}
}
private class DateGrid extends JPanel implements MouseListener {
JLabel[][] lbs = new JLabel[7][7];
int year = 1, month = 1, day = 1;
public DateGrid() {
super(new GridLayout(7, 7));
this.setBackground(Color.white);
Color blue = new Color(140, 150, 255);
Color white = new Color(220, 220, 255);
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
this.add(lbs[i][j] = new JLabel((String) null));
lbs[i][j].setOpaque(true);
lbs[i][j].setHorizontalAlignment(JLabel.CENTER);
lbs[i][j].setBackground(Color.white);
if (i > 0) {
lbs[i][j].addMouseListener(this);
} else {
lbs[0][j].setBackground(blue);
lbs[0][j].setForeground(white);
}
}
}
lbs[0][0].setText("日");
lbs[0][1].setText("一");
lbs[0][2].setText("二");
lbs[0][3].setText("三");
lbs[0][4].setText("四");
lbs[0][5].setText("五");
lbs[0][6].setText("六");
this.setBorder(BorderFactory.createLoweredBevelBorder());
}
public void setDay(int day) {
if (day < 1) {
day = 1;
}
int days = maxDay(year, month);
if (day > days) {
day = days;
}
this.day = day;
int d = 0;
for (int i = 1; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if (lbs[i][j].getText() != null) {
if (++d == day) {
lbs[i][j].setBackground(Color.blue);
lbs[i][j].setForeground(Color.white);
continue;
}
}
lbs[i][j].setBackground(Color.white);
lbs[i][j].setForeground(Color.black);
}
}
repaint();
}
public int getDay() {
return day;
}
public void set(int year, int month) {
if (year < 1) {
year = 1;
} else if (year > 9999) {
year = 9999;
}
if (month < 1) {
month = 1;
} else if (month > 12) {
month = 12;
}
int pastdays = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100;
pastdays += (month - 1) * 31;
pastdays -= (month - 1) / 2;
if (month >= 9 && month % 2 == 1) {
pastdays += 1;
}
if (month > 2) {
pastdays -= 2;
if ((year % 4 == 0) && (year % 400 != 0)) {
pastdays += 1;
}
}
int dayInWeak = pastdays % 7 - 1;
if (dayInWeak == -1) {
dayInWeak = 6;
}
// ~
for (int i = 1; i < 7; i++) {
for (int j = 0; j < 7; j++) {
lbs[i][j].setText(null);
}
}
int days = maxDay(year, month);
for (int i = 1; i <= days; i++) {
lbs[1 + (dayInWeak + i - 1) / 7][(dayInWeak + i - 1) % 7].setText(String.valueOf(i));
}
this.year = year;
this.month = month;
setDay(day);
}
private int maxDay(int year, int month) {
int days = (month % 2 == (month > 7 ? 0 : 1)) ? 31 : 30;
if (month == 2) {
days = (year % 4 == 0 && year % 400 != 0) ? 29 : 28;
}
return days;
}
public void mousePressed(MouseEvent e) {
Object o = e.getSource();
JLabel lb = null;
for (int i = 1; i < 7; i++) {
for (int j = 0; j < 7; j++) {
JLabel b = lbs[i][j];
if (b == o && b.getText() != null) {
lb = b;
break;
}
}
}
if (lb == null) {
return;
}
int d = 0;
for (int i = 1; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if (lbs[i][j].getText() != null) {
d++;
if (lbs[i][j] == lb) {
setDay(d);
return;
}
}
}
}
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
private class Clock extends JComponent implements ChangeListener {
Calendar c;
int[] xs = new int[4];
int[] ys = new int[4];
final Color handColor = new Color(24, 116, 109);
public Clock(JSpinner time) {
time.addChangeListener(this);
stateChanged(new ChangeEvent(time));
}
public void stateChanged(ChangeEvent e) {
JSpinner sp = (JSpinner) e.getSource();
Date date = (Date) sp.getValue();
if (c == null) {
c = Calendar.getInstance();
}
c.setTime(date);
repaint();
}
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
float ox = getWidth() / 2f;
float oy = getHeight() / 2f;
final float R = 60f;
Ellipse2D.Float oval = new Ellipse2D.Float();
for (int i = 0; i < 360; i += 360 / 12 / 5) {
float a = i / 360f * (float) Math.PI * 2;
float dx = (float) Math.sin(a) * R;
float dy = (float) Math.cos(a) * R;
g2.setColor(i % (360 / 12) == 0 ? Color.black : Color.lightGray);
oval.setFrame(ox - dx - 1f, oy - dy - 1f, 2, 2);
g2.fill(oval);
if (i % (360 / 12) == 0) {
g2.setColor(Color.cyan);
oval.setFrame(ox + dx - 2f, oy + dy - 1f, 2, 2);
g2.fill(oval);
}
}
int hour = c.get(Calendar.HOUR);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
float ha = (float) Math.PI * 2 / 12 * (hour + (float) minute / 60);
float ma = (float) Math.PI * 2 / 60
* (minute + (float) second / 60);
float sa = (float) Math.PI * 2 / 60 * second;
g2.setColor(Color.lightGray);
g2.translate(1, 1);
drawHand(g2, ox, oy, ha, 40);
g2.translate(1, 1);
drawHand(g2, ox, oy, ma, 48);
g2.translate(1, 1);
drawHand(g2, ox, oy, sa, 50, false);
g2.translate(-3, -3);
g2.setColor(handColor);
drawHand(g2, ox, oy, ha, 40);
drawHand(g2, ox, oy, ma, 48);
g2.setColor(Color.black);
drawHand(g2, ox, oy, sa, 50, false);
}
private void drawHand(Graphics2D g2, float ox, float oy, float a,
float len, boolean w) {
xs[2] = (int) (ox + (float) Math.sin(a) * len);
ys[2] = (int) (oy - (float) Math.cos(a) * len);
xs[0] = (int) (ox + (float) Math.sin(a + Math.PI) * 10);
ys[0] = (int) (oy - (float) Math.cos(a + Math.PI) * 10);
if (w) {
xs[1] = (int) (ox + (float) Math.sin(a + Math.PI / 2) * 3);
ys[1] = (int) (oy - (float) Math.cos(a + Math.PI / 2) * 3);
xs[3] = (int) (ox + (float) Math.sin(a + Math.PI * 3 / 2) * 3);
ys[3] = (int) (oy - (float) Math.cos(a + Math.PI * 3 / 2) * 3);
g2.fillPolygon(xs, ys, 4);
} else {
Line2D.Float line = new Line2D.Float(xs[0], ys[0], xs[2], ys[2]);
g2.draw(line);
}
}
private void drawHand(Graphics2D g2, float ox, float oy, float a,
float len) {
this.drawHand(g2, ox, oy, a, len, true);
}
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
* 日期时间选择对话框,模仿Windows日期和时间属性对话框
*
*
*/
public class DateChooserJDialog extends JDialog implements ChangeListener,
ItemListener {
private JComboBox month = new JComboBox(new String[]{"一月", "二月", "三月",
"四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"});
private JSpinner year = new JSpinner(new SpinnerNumberModel(1, 1, 9999, 1));
private DateGrid dateGrid = new DateGrid();
private JSpinner time = new JSpinner(new SpinnerDateModel());
private JButton ok = new JButton("确认");
private JButton cancle = new JButton("取消");
private JPanel buttonPanel = new JPanel();
private JPanel datePanel = new JPanel();
private String ymdt = "";
private String date = "";
private String timeStamp = "";
/**
* Description:初始时间设置面板
*
* @param initialDate 日历操作句柄
* @param taskConfigFrame 任务设置界面句柄 final TaskConfigFrame taskConfigFrame
*/
public DateChooserJDialog() {
Calendar initialDate = new GregorianCalendar();
JPanel cp = new JPanel();
cp.setLayout(new BorderLayout());
cp.setBackground(Color.white);
cp.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
datePanel.setLayout(new GridLayout(1, 2, 5, 10));
JPanel p1 = new JPanel();
p1.setBackground(Color.white);
p1.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("日期"), BorderFactory.createEmptyBorder(5,
5, 10, 5)));
p1.setLayout(new BorderLayout(10, 10));
JPanel p11 = new JPanel(new GridLayout(1, 2, 10, 10));
p11.setBackground(Color.white);
p11.add(month);
month.addItemListener(this);
month.setPreferredSize(new Dimension(0, 20));
p11.add(year);
year.setEditor(new JSpinner.NumberEditor(year, "0"));
year.addChangeListener(this);
year.setPreferredSize(new Dimension(0, 20));
p1.add(p11, BorderLayout.NORTH);
p1.add(dateGrid);
datePanel.add(p1);
JPanel p2 = new JPanel(new BorderLayout(10, 10));
p2.setBackground(Color.white);
p2.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("时间"), BorderFactory.createEmptyBorder(0,
10, 10, 10)));
time.setPreferredSize(new Dimension(0, 20));
JSpinner.DateEditor editor = new JSpinner.DateEditor(time, "HH:mm:ss");
editor.getTextField().setHorizontalAlignment(JTextField.RIGHT);
time.setEditor(editor);
JPanel p = new JPanel();
p.setBackground(Color.white);
time.setPreferredSize(new Dimension(100, 20));
p.add(time);
p2.add(p, BorderLayout.SOUTH);
p2.add(new Clock(time));
datePanel.add(p2);
setCalendar(initialDate);
setPreferredSize(new Dimension(369, 200));
buttonPanel.add(ok);
buttonPanel.add(cancle);
cp.add(datePanel, BorderLayout.CENTER);
cp.add(buttonPanel, BorderLayout.SOUTH);
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String y = String.valueOf(dateGrid.year);
String m = String.valueOf(dateGrid.month);
String d = String.valueOf(dateGrid.day);
if (dateGrid.month < 10) {
m = "0" + dateGrid.month;
}
if (dateGrid.day < 10) {
d = "0" + dateGrid.day;
}
String t = time.getValue().toString().substring(11, 19);
ymdt = y + "-" + m + "-" + d + " " + t;
date = y + "-" + m + "-" + d;
timeStamp = t;
DateChooserJDialog.this.setVisible(false);
}
});
cancle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DateChooserJDialog.this.dispose();
}
});
add(cp, BorderLayout.CENTER);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
this.setTitle("日期和时间设置");
this.setModal(true);
this.setLocation(screenWidth / 3, screenHeight / 3);
this.setSize(400, 280);
this.setResizable(false);
}
public String getDatetime() {
this.setVisible(true);
return ymdt;
}
public String getTime() {
this.setVisible(true);
return timeStamp;
}
public String getDate() {
this.setVisible(true);
return date;
}
/**
* 设置日期和时间
*
* @param calendar 日期和时间
*/
public void setCalendar(Calendar calendar) {
if (calendar == null) {
calendar = new GregorianCalendar();
}
month.setSelectedIndex(calendar.get(Calendar.MONTH));
year.setValue(calendar.get(Calendar.YEAR));
int year = Integer.parseInt(this.year.getValue().toString());
int month = this.month.getSelectedIndex() + 1;
dateGrid.set(year, month);
dateGrid.setDay(calendar.get(Calendar.DAY_OF_MONTH));
time.setValue(calendar.getTime());
}
/**
* 获得日期和时间
*
* @return 日期和时间
*/
public Calendar getCalendar() {
int year = Integer.parseInt(this.year.getValue().toString());
int month = this.month.getSelectedIndex();
int day = dateGrid.getDay();
Calendar c = Calendar.getInstance();
Date date = (Date) time.getValue();
c.setTime(date);
c.set(year, month, day);
return c;
}
public void stateChanged(ChangeEvent e) {
dateGrid.set(Integer.parseInt(year.getValue().toString()), month.getSelectedIndex() + 1);
}
public void itemStateChanged(ItemEvent e) {
dateGrid.set(Integer.parseInt(year.getValue().toString()), month.getSelectedIndex() + 1);
}
private class ActionHandler extends WindowAdapter implements
ActionListener {
private DateChooserJDialog dc;
public ActionHandler(DateChooserJDialog dc) {
this.dc = dc;
}
public void windowClosing(WindowEvent e) {
actionPerformed(new ActionEvent(this, 0, "取消"));
}
public void actionPerformed(ActionEvent e) {
JDialog dlg = (JDialog) SwingUtilities.windowForComponent(dc);
dlg.dispose();
if ("取消".equals(e.getActionCommand())) {
dc = null;
}
}
public Calendar getCalendar() {
return dc == null ? null : dc.getCalendar();
}
}
private class DateGrid extends JPanel implements MouseListener {
JLabel[][] lbs = new JLabel[7][7];
int year = 1, month = 1, day = 1;
public DateGrid() {
super(new GridLayout(7, 7));
this.setBackground(Color.white);
Color blue = new Color(140, 150, 255);
Color white = new Color(220, 220, 255);
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
this.add(lbs[i][j] = new JLabel((String) null));
lbs[i][j].setOpaque(true);
lbs[i][j].setHorizontalAlignment(JLabel.CENTER);
lbs[i][j].setBackground(Color.white);
if (i > 0) {
lbs[i][j].addMouseListener(this);
} else {
lbs[0][j].setBackground(blue);
lbs[0][j].setForeground(white);
}
}
}
lbs[0][0].setText("日");
lbs[0][1].setText("一");
lbs[0][2].setText("二");
lbs[0][3].setText("三");
lbs[0][4].setText("四");
lbs[0][5].setText("五");
lbs[0][6].setText("六");
this.setBorder(BorderFactory.createLoweredBevelBorder());
}
public void setDay(int day) {
if (day < 1) {
day = 1;
}
int days = maxDay(year, month);
if (day > days) {
day = days;
}
this.day = day;
int d = 0;
for (int i = 1; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if (lbs[i][j].getText() != null) {
if (++d == day) {
lbs[i][j].setBackground(Color.blue);
lbs[i][j].setForeground(Color.white);
continue;
}
}
lbs[i][j].setBackground(Color.white);
lbs[i][j].setForeground(Color.black);
}
}
repaint();
}
public int getDay() {
return day;
}
public void set(int year, int month) {
if (year < 1) {
year = 1;
} else if (year > 9999) {
year = 9999;
}
if (month < 1) {
month = 1;
} else if (month > 12) {
month = 12;
}
int pastdays = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100;
pastdays += (month - 1) * 31;
pastdays -= (month - 1) / 2;
if (month >= 9 && month % 2 == 1) {
pastdays += 1;
}
if (month > 2) {
pastdays -= 2;
if ((year % 4 == 0) && (year % 400 != 0)) {
pastdays += 1;
}
}
int dayInWeak = pastdays % 7 - 1;
if (dayInWeak == -1) {
dayInWeak = 6;
}
// ~
for (int i = 1; i < 7; i++) {
for (int j = 0; j < 7; j++) {
lbs[i][j].setText(null);
}
}
int days = maxDay(year, month);
for (int i = 1; i <= days; i++) {
lbs[1 + (dayInWeak + i - 1) / 7][(dayInWeak + i - 1) % 7].setText(String.valueOf(i));
}
this.year = year;
this.month = month;
setDay(day);
}
private int maxDay(int year, int month) {
int days = (month % 2 == (month > 7 ? 0 : 1)) ? 31 : 30;
if (month == 2) {
days = (year % 4 == 0 && year % 400 != 0) ? 29 : 28;
}
return days;
}
public void mousePressed(MouseEvent e) {
Object o = e.getSource();
JLabel lb = null;
for (int i = 1; i < 7; i++) {
for (int j = 0; j < 7; j++) {
JLabel b = lbs[i][j];
if (b == o && b.getText() != null) {
lb = b;
break;
}
}
}
if (lb == null) {
return;
}
int d = 0;
for (int i = 1; i < 7; i++) {
for (int j = 0; j < 7; j++) {
if (lbs[i][j].getText() != null) {
d++;
if (lbs[i][j] == lb) {
setDay(d);
return;
}
}
}
}
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
private class Clock extends JComponent implements ChangeListener {
Calendar c;
int[] xs = new int[4];
int[] ys = new int[4];
final Color handColor = new Color(24, 116, 109);
public Clock(JSpinner time) {
time.addChangeListener(this);
stateChanged(new ChangeEvent(time));
}
public void stateChanged(ChangeEvent e) {
JSpinner sp = (JSpinner) e.getSource();
Date date = (Date) sp.getValue();
if (c == null) {
c = Calendar.getInstance();
}
c.setTime(date);
repaint();
}
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
float ox = getWidth() / 2f;
float oy = getHeight() / 2f;
final float R = 60f;
Ellipse2D.Float oval = new Ellipse2D.Float();
for (int i = 0; i < 360; i += 360 / 12 / 5) {
float a = i / 360f * (float) Math.PI * 2;
float dx = (float) Math.sin(a) * R;
float dy = (float) Math.cos(a) * R;
g2.setColor(i % (360 / 12) == 0 ? Color.black : Color.lightGray);
oval.setFrame(ox - dx - 1f, oy - dy - 1f, 2, 2);
g2.fill(oval);
if (i % (360 / 12) == 0) {
g2.setColor(Color.cyan);
oval.setFrame(ox + dx - 2f, oy + dy - 1f, 2, 2);
g2.fill(oval);
}
}
int hour = c.get(Calendar.HOUR);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
float ha = (float) Math.PI * 2 / 12 * (hour + (float) minute / 60);
float ma = (float) Math.PI * 2 / 60
* (minute + (float) second / 60);
float sa = (float) Math.PI * 2 / 60 * second;
g2.setColor(Color.lightGray);
g2.translate(1, 1);
drawHand(g2, ox, oy, ha, 40);
g2.translate(1, 1);
drawHand(g2, ox, oy, ma, 48);
g2.translate(1, 1);
drawHand(g2, ox, oy, sa, 50, false);
g2.translate(-3, -3);
g2.setColor(handColor);
drawHand(g2, ox, oy, ha, 40);
drawHand(g2, ox, oy, ma, 48);
g2.setColor(Color.black);
drawHand(g2, ox, oy, sa, 50, false);
}
private void drawHand(Graphics2D g2, float ox, float oy, float a,
float len, boolean w) {
xs[2] = (int) (ox + (float) Math.sin(a) * len);
ys[2] = (int) (oy - (float) Math.cos(a) * len);
xs[0] = (int) (ox + (float) Math.sin(a + Math.PI) * 10);
ys[0] = (int) (oy - (float) Math.cos(a + Math.PI) * 10);
if (w) {
xs[1] = (int) (ox + (float) Math.sin(a + Math.PI / 2) * 3);
ys[1] = (int) (oy - (float) Math.cos(a + Math.PI / 2) * 3);
xs[3] = (int) (ox + (float) Math.sin(a + Math.PI * 3 / 2) * 3);
ys[3] = (int) (oy - (float) Math.cos(a + Math.PI * 3 / 2) * 3);
g2.fillPolygon(xs, ys, 4);
} else {
Line2D.Float line = new Line2D.Float(xs[0], ys[0], xs[2], ys[2]);
g2.draw(line);
}
}
private void drawHand(Graphics2D g2, float ox, float oy, float a,
float len) {
this.drawHand(g2, ox, oy, a, len, true);
}
}
}
相关推荐
JavaScript日历选择器是一种在网页上提供用户交互的日历组件,通常用于输入日期或选取日期范围。这种组件在各种Web应用程序中广泛使用,比如在线预订系统、事件安排、表单填写等。它允许用户通过图形界面方便地选择...
在JavaScript中,创建日历选择器通常涉及到以下几个关键知识点: 1. **DOM操作**:首先,我们需要在HTML页面中添加一个触发日历显示的元素,如输入框或按钮。然后,通过JavaScript来获取这些元素并添加事件监听器,...
在网页开发中,JavaScript(JS)常常用于实现各种交互效果,包括日历选择器控件。"JS日历选择器控件.zip" 提供的是一款基于JavaScript实现的日历选择器,它允许用户在输入日期时通过点击文本框来弹出一个日历视图,...
MaterialDateTimePicker 日期选择器,Material风格,很漂亮,包括日历选择器和时间选择器,很强大
layDate是一款由layui团队开发的高效、轻量且功能丰富的原生JavaScript日历插件,它提供了多种日历选择器样式,适用于各种网页场景。在这款控件中,开发者可以轻松实现日期选择、日期时间选择、年月选择、时间选择等...
在Android开发中,创建一个可左右滑动的日历选择器是一项常见的需求,它能为用户提供直观、便捷的方式来选择日期。这个“Android例子源码可以左右滑动的日历选择器”是一个很好的示例,展示了如何实现这样的功能。让...
在Android开发中,为了提供用户友好的界面,有时我们需要实现类似iOS的日历选择器功能。这个功能可以让用户方便地选择日期或时间,通常用于预订、提醒等应用场景。在这个项目中,我们将探讨如何在Android Studio环境...
《jQuery日历选择器详解与应用实践》 在网页开发中,日历选择器是一种常见的交互元素,它为用户提供了方便的时间输入方式。本篇文章将深入探讨jQuery日历选择器的实现原理、功能特点以及实际应用,帮助开发者更好地...
本篇文章将深入探讨如何使用jQuery创建一个简单日历选择器,以及这个选择器背后的代码原理。 首先,jQuery日历选择器通常用于网页上的日期输入,为用户提供友好的交互体验。用户不再需要手动输入日期,而是通过点击...
在IT行业中,尤其是在开发用户界面时,"可视化日历选择器"是一个常见的组件,它允许用户通过图形化的方式直观地选择日期。这种组件通常用于需要输入日期的表单或者需要进行日期选择的交互场景,如预订系统、日程管理...
在iOS开发中,日历选择器是一个常见的组件,用于让用户方便地选取特定的日期。"ios-日历选择器.zip" 提供了一个示例,它实现了根据日历选择日期的功能,并且在用户切换年份和月份时能更新并显示当前所选时间。这个...
而“jquery带节假日农历的日历选择器代码”则是在jQuery基础上开发的一个特定功能插件,它能够为网页提供一个集成农历和节日显示的日历选择器。这样的组件对于需要处理中国传统日期的网站或者应用来说非常实用,例如...
在iOS应用开发中,苹果日历选择器和时间选择器是常见的用户界面元素,用于让用户方便地选取日期和时间。本教程将详细讲解如何在iOS项目中实现一个类似苹果原生的日历选择器和时间选择器。我们将主要关注以下几个方面...
jQuery插件是扩展这个库功能的一种方式,为开发者提供了丰富的自定义组件,比如我们今天要讨论的“日历选择器”。这个插件主要用于在网页上创建用户友好的日期输入界面,常见于各种表单,如预订系统、调查问卷等。 ...
Material Desigin风格的日历选择器,支持多种样式.zip,A material-styled android view that provisions picking of a date, time & recurrence option, all from a single user-interface.
在IT领域,前端开发中经常会使用到各种各样的插件来增强用户体验,特别是涉及到日期选择时,jQuery日期日历选择器插件就是一种常见的解决方案。本文将深入探讨jQuery日期日历选择器插件的自定义功能及其应用。 首先...
《jQuery周日历选择器代码实现详解》 在网页开发中,日历选择器是一种常见的交互元素,用于方便用户选择日期。jQuery库以其强大的DOM操作和丰富的插件库,为开发者提供了实现各种日历选择器的便利。本文将详细介绍...
【完美日历选择器】是一款精心设计的日历组件,它具备了多项实用功能,使得开发者在构建应用程序时能轻松集成日期选择功能。该选择器以其简洁的代码结构、流畅的滑动体验、全面的日历特性以及自定义化的界面风格赢得...