闹钟源代码Java 闹钟java设计代码( 五 )


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++) {
c = getTime.charAt(i);
if (i == 2!Character.isDigit(c))
continue;
// 判断当前字符,如果不是数字则跳出该事件
if (i != 2!Character.isDigit(c)) {
JOptionPane.showMessageDialog(this, "格式错误\n请按格式输入5位数字",
"Error",JOptionPane.ERROR_MESSAGE);
repaint();
break judge;
}
}
char[] hour = { getTime.charAt(0), getTime.charAt(1) };
char[] minute = { getTime.charAt(3), getTime.charAt(4) };
newHou = Integer.parseInt(String.valueOf(hour));
newMin = Integer.parseInt(String.valueOf(minute));
if (newHou = 24 || newHou0) {
JOptionPane.showMessageDialog(this, "格式错误\n小时应该是不小于0不大于23的正数",
"Error", JOptionPane.ERROR_MESSAGE);
repaint();
break judge;
}
if (newMin = 60 || newHou0) {
JOptionPane.showMessageDialog(this, "格式错误\n分钟应该是小于60的正数", "Error",
JOptionPane.ERROR_MESSAGE);
repaint();
break judge;
}
new SetTime(newHou, newMin);

推荐阅读