java日历闹钟代码 java中日历类( 二 )


Color C1 = Color.lightGray;// 外圈颜色
Color C2 = Color.black;// 边线颜色
Color C3 = Color.magenta;// 内盘颜色
Color C4 = Color.blue;// 背景颜色
Color C5 = Color.yellow;// 秒针颜色
Color C6 = Color.green;// 分针颜色
Color C7 = Color.red;//时针颜色
public class ClockPanel extends JPanel {
public void paint(Graphics g) {
h = getSize().height - 200;
// 绘制圆形
g.setColor(C1);
g.fillOval(L0 + 30, T0 + 30, h - 60, h - 60);
g.setColor(C2);
g.drawOval(L0 + 31, T0 + 31, h - 62, h - 62);
g.setColor(C3);
g.fillOval(L0 + 50, T0 + 50, h - 100, h - 100);
g.setColor(C2);
g.drawOval(L0 + 51, T0 + 51, h - 102, h - 102);
r = h / 2 - 30;
x0 = 30 + r - 5 + L0;
y0 = 30 + r - 5 - T0;
ang = 60;
for (int i = 1; i = 12; i++) {
x = (int) ((r - 10) * Math.cos(RAD * ang) + x0);
y = (int) ((r - 10) * Math.sin(RAD * ang) + y0);
g.drawString("" + i, x, h - y);
ang -= 30;
}
x0 = 30 + r + L0;
y0 = 30 + r + T0;
g.drawString("指针式时钟", 215, 200);
// 获取时间
now = Calendar.getInstance();
hh = now.get(Calendar.HOUR_OF_DAY);// 小时
mm = now.get(Calendar.MINUTE);// 分钟
ss = now.get(Calendar.SECOND);// 秒
g.setColor(C4);
g.fillRect(5, 550, 150, 30);// 填充的矩形
g.setColor(C6);
if (hh10)
st = "0" + hh;
else
st = "" + hh;
if (mm10)
st = st + ":0" + mm;
else
st = st + ":" + mm;
if(alarm.equals(st))
{
if(toolkit!=null)
toolkit.beep();
else{}
}
if (ss10)
st = st + ":0" + ss;
else
st = st + ":" + ss;
{
g.setFont(new Font("华文楷体", Font.BOLD, 16));
g.drawString("系统时间:" + st, 10, 570);
}
// 计算时间和图形的关系
sdo = 90 - ss * 6;
mdo = 90 - mm * 6;
hdo = 90 - hh * 30 - mm / 2;
// 擦除秒针
if (old_X0) {
g.setColor(C3);
} else {
old_M = mdo;
old_H = hdo;
}
// 绘制秒针
g.setColor(C5);
x = (int) ((r - 26) * Math.cos(RAD * sdo) + x0);
y = (int) ((r - 26) * Math.sin(RAD * sdo) + y0) - 2 * T0;
g.drawLine(x0, y0, x, (h - y));
old_X = x;
old_Y = y;
// 擦除分针和时针
if (mdo != old_M) {
g.setColor(C3);
old_M = mdo;
}
if (hdo != old_H) {
g.setColor(C3);
old_H = hdo;
}
// 绘制分针
g.setColor(C6);
x = (int) ((r - 50) * Math.cos(RAD * mdo) + x0);
y = (int) ((r - 50) * Math.sin(RAD * mdo) + y0) - 2 * T0;
g.drawLine(x0, y0, x, (h - y));
// 绘制时针
g.setColor(C7);
x = (int) ((r - 90) * Math.cos(RAD * hdo) + x0);
y = (int) ((r - 90) * Math.sin(RAD * hdo) + y0) - 2 * T0;
g.drawLine(x0, y0, x, (h - y));
} // end paint
}
// 闹铃时间的判断及实现
// 闹铃声音的实现
public void actionPerformed(ActionEvent e) {
//JMenuItem m = (JMenuItem) e.getSource();
if (e.getActionCommand() == "CC") {
int newHou, newMin;
char c;
String getTime = JOptionPane.showInputDialog(this, "请输入闹铃时间格式如:", "00:00");
repaint();
//如果撤消设置时间,就什么打印null
if(getTime==null)
System.out.println(getTime);
//dispose();
judge: if (getTime != null) {
//打印输入的设置的时间
System.out.println(getTime);
// 判断输入的是不是5位字符
if (getTime.length() != 5) {
JOptionPane.showMessageDialog(time, "格式错误\n请按格式输入5位数字", "Error",
JOptionPane.ERROR_MESSAGE);
repaint();
break judge;
}
// 判断输入的是不是数字
for (int i = 0; i(getTime.length()); i++) {

推荐阅读