闹钟java代码,java闹钟简单代码

寻找java编程高手写一个闹钟的程序

自己之前做过的一个:

成都创新互联专业为企业提供涞源网站建设、涞源做网站、涞源网站设计、涞源网站制作等企业网站建设、网页设计与制作、涞源企业网站模板建站服务,十载涞源做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.Timer;

public class Clock extends JFrame implements ActionListener {

public final int HEIGTH = 200, L0 = 50, T0 = 50,N=8;

public final double RAD = Math.PI / 180.0;

int x, y, old_X, old_Y, r, x0, y0, w, h, ang;

int sdo, mdo, hdo, old_M, old_H, hh, mm, ss;

int delay = 1000;

Calendar now;

String st, alarm, Items1, Items2,str[];

JButton jb;

JComboBox jc1, jc2, jc3;

JLabel jl1, jl2, jl3, jl4;

JMenu jm1, jm2, jm3, jm4;

JMenuBar jmb;

JMenuItem jmi1, jmi2, jmi3, jmi4, jmi5, jmi6, jmi7, jmi8, jmi9,jmi10;

JTextField jtf1, jtf2, time;

JPanel jp1, jp2, jp3;

Timer timer;

TimeZone tz = TimeZone.getTimeZone("JST");

Toolkit toolkit=Toolkit.getDefaultToolkit();;

/**

* br

* 方法说明:实现ActionListener类必须过载的方法

*/

public static void main(String[] args) {

Clock cp = new Clock();

cp.setVisible(true);

}

Clock() {

super("Java闹钟!");

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

setSize(550, 700);

setVisible(true);

Container contentPane = getContentPane();

jp2 = new JPanel();

jmb = new JMenuBar();

jm1 = new JMenu("背景颜色设置 ", true);

jmi1 = new JMenuItem("外圈颜色");

jmi1.addActionListener(this);

jmi1.setActionCommand("color1");

jm1.add(jmi1);

jmi2 = new JMenuItem("闹钟边线颜色");

jmi2.addActionListener(this);

jmi2.setActionCommand("color2");

jm1.add(jmi2);

jmi3=new JMenuItem("底盘颜色");

jmi3.addActionListener(this);

jmi3.setActionCommand("color3");

jm1.add(jmi3);

jmi4=new JMenuItem("系统时间背静颜色");

jmi4.addActionListener(this);

jmi4.setActionCommand("color4");

jm1.add(jmi4);

jmb.add(jm1);

jm2 = new JMenu("指针颜色设置 ", true);

jmi5 = new JMenuItem("秒针颜色");

jmi5.addActionListener(this);

jmi5.setActionCommand("color5");

jm2.add(jmi5);

jmi6 = new JMenuItem("分针颜色");

jmi6.addActionListener(this);

jmi6.setActionCommand("color6");

jm2.add(jmi6);

jmi7 = new JMenuItem("时针颜色");

jmi7.addActionListener(this);

jmi7.setActionCommand("color7");

jm2.add(jmi7);

jmb.add(jm2);

jm3 = new JMenu("闹铃声音设置 ", true);

jmi8 = new JMenuItem("响铃1");

jmi8.addActionListener(this);

jmi8.setActionCommand("ring1");

jm3.add(jmi8);

jmi9 = new JMenuItem("静音");

jmi9.addActionListener(this);

jmi9.setActionCommand("ring2");

jm3.add(jmi9);

jmb.add(jm3);

jm4 = new JMenu("帮助 ", true);

jmi10=new JMenuItem("使用说明");

jmi10.addActionListener(this);

jmi10.setActionCommand("help");

jm4.add(jmi10);

jmb.add(jm4);

jp2.add(jmb);

contentPane.add(jp2, BorderLayout.NORTH);

jp3 = new JPanel();

jl1 = new JLabel("闹铃时间");

jl1.setFont(new Font("楷体_GB2312", Font.BOLD, 18));

time = new JTextField("00:00", 5);

alarm = time.getText();

jb = new JButton("修改闹铃时间");

jb.addActionListener(this);

jb.setActionCommand("CC");

jp3.add(jl1);

jp3.add(time);

jp3.add(jb);

contentPane.add(jp3, BorderLayout.SOUTH);

ClockPanel clock = new ClockPanel();

contentPane.add(clock, BorderLayout.CENTER);

// 窗体添加事件监听,监听秒表的触发

ActionListener taskPerformer = new ActionListener() {

public void actionPerformed(ActionEvent evt) {

repaint();

}

};

new Timer(delay, taskPerformer).start();

}

/**

* br

* 方法说明:绘制图形

*/

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 (hh 10)

st = "0" + hh;

else

st = "" + hh;

if (mm 10)

st = st + ":0" + mm;

else

st = st + ":" + mm;

if(alarm.equals(st))

{

if(toolkit!=null)

toolkit.beep();

else {}

}

if (ss 10)

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_X 0) {

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 || newHou 0) {

JOptionPane.showMessageDialog(this, "格式错误\n小时应该是不小于0不大于23的正数",

"Error", JOptionPane.ERROR_MESSAGE);

repaint();

break judge;

}

if (newMin = 60 || newHou 0) {

JOptionPane.showMessageDialog(this, "格式错误\n分钟应该是小于60的正数", "Error",

JOptionPane.ERROR_MESSAGE);

repaint();

break judge;

}

new SetTime(newHou, newMin);

}

}

if (e.getActionCommand() == "ring1") {

toolkit=Toolkit.getDefaultToolkit();

}

if(e.getActionCommand() == "ring2"){

System.out.println("静音");

toolkit=null;

}

if (e.getActionCommand() == "color1") {

String color;

Color c;

System.out.println("color1");

color = JOptionPane.showInputDialog(this, "请输入喜欢的外圈颜色(0--255)", "128");

if (color == null) {

} else {

if (Integer.parseInt(color) 0

|| Integer.parseInt(color) 255)

JOptionPane.showInputDialog(this, "请输入喜欢的外圈颜色(0--255)", "128");

else {

c = new Color(Integer.parseInt(color));

C1 = c;

}

}

}

if(e.getActionCommand() == "color2"){

String color;

Color c;

System.out.println("color2");

color = JOptionPane.showInputDialog(this, "请输入喜欢的边线颜色(0--255)", "128");

if(color==null){}

else{if (Integer.parseInt(color) 0|| Integer.parseInt(color) 255)

JOptionPane.showInputDialog(this, "请输入喜欢的边线颜色(0--255)", "128");

else {

c = new Color(Integer.parseInt(color));

C2 = c;

}

}

}

if(e.getActionCommand() == "color3"){

String color;

Color c;

System.out.println("color3");

color = JOptionPane.showInputDialog(this, "请输入喜欢的内盘颜色(0--255)", "128");

if(color==null){}

else{if (Integer.parseInt(color) 0|| Integer.parseInt(color) 255)

JOptionPane.showInputDialog(this, "请输入喜欢的内盘颜色(0--255)", "128");

else {

c = new Color(Integer.parseInt(color));

C3 = c;

}

}

}

if(e.getActionCommand() == "color4"){

String color;

Color c;

System.out.println("color4");

color = JOptionPane.showInputDialog(this, "请输入喜欢的背景颜色(0--255)", "128");

if(color==null){}

else{if (Integer.parseInt(color) 0|| Integer.parseInt(color) 255)

JOptionPane.showInputDialog(this, "请输入喜欢的背景颜色(0--255)", "128");

else {

c = new Color(Integer.parseInt(color));

C4 = c;

}

}

}

if(e.getActionCommand() == "color5"){

String color;

Color c;

System.out.println("color5");

color = JOptionPane.showInputDialog(this, "请输入喜欢的秒针颜色(0--255)", "128");

if(color==null){}

else{if (Integer.parseInt(color) 0|| Integer.parseInt(color) 255)

JOptionPane.showInputDialog(this, "请输入喜欢的秒针颜色(0--255)", "128");

else {

c = new Color(Integer.parseInt(color));

C5 = c;

}

}

}

if(e.getActionCommand() == "color6"){

String color;

Color c;

System.out.println("color6");

color = JOptionPane.showInputDialog(this, "请输入喜欢的分针颜色(0--255)", "128");

if(color==null){}

else{if (Integer.parseInt(color) 0|| Integer.parseInt(color) 255)

JOptionPane.showInputDialog(this, "请输入喜欢的分针颜色(0--255)", "128");

else {

c = new Color(Integer.parseInt(color));

C6 = c;

}

}

}

if(e.getActionCommand() == "color7"){

String color;

Color c;

System.out.println("color7");

color = JOptionPane.showInputDialog(this, "请输入喜欢的时针颜色(0--255)", "128");

if(color==null){}

else{if (Integer.parseInt(color) 0|| Integer.parseInt(color) 255)

JOptionPane.showInputDialog(this, "请输入喜欢的时针颜色(0--255)", "128");

else {

c = new Color(Integer.parseInt(color));

C7 = c;

}

}

}

if(e.getActionCommand() == "help"){

String help;

help = JOptionPane.showInputDialog(this, "输入quit退出该闹钟的使用", "这是运行在Java中的指针式时钟");

if(help.equals("quit"))

dispose();

else {}

// timer.restart();

}

}

class SetTime {

String Hour;

String Minute;

public SetTime() { }

public SetTime(int hour, int minute) {

// 当时间参数小于10的时候在前面添加字符0

if (hour 10) {

Hour = "0" + String.valueOf(hour);

} else {

Hour = "" + String.valueOf(hour);

}

if (minute 10) {

Minute = "0" + String.valueOf(minute);

} else {

Minute = "" + String.valueOf(minute);

}

alarm = Hour + ":" + Minute;

time.setText(alarm);

repaint();

}

}

}

有哪位大神知道用java写闹钟程序怎么计算闹钟时间与当前时间差么,拜托了

SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date nowdate = new Date();//系统当前时间

java.util.Date begin=dfs.parse("2015-12-25 12:30:00");//闹钟设置时间可以获取,我暂时写死

java.util.Date end = dfs.parse(nowdate);

long between=(begin.getTime()-end.getTime())/1000;//除以1000是为了转换成秒

long day1=between/(24*3600);

long hour1=between%(24*3600)/3600;

long minute1=between%3600/60;

long second1=between%60/60;

System.out.println(""+day1+"天"+hour1+"小时"+minute1+"分"+second1+"秒");

请用JAVA编一个闹钟程序,符合要求追加100分!急!!

import java.util.*;

import java.awt.*;

import java.applet.*;

import java.text.*;

public class AlarmClock extends Applet implements Runnable

{

Thread timer=null; //创建线程timer

Image clockp,gif1,gif2,clock6,clock7; //clockp:闹钟的外壳,闹铃和报时鸟

int s,m,h,hh;

AudioClip ipAu,danger,chirp;

boolean canPaint=true;

boolean flag=false;

boolean strike=true;

int counter=0;

int lasts;

Image offscreen_buf=null;

int i,j,t=0;

int timeout=166;

int lastxs=0,lastys=0,lastxm=0,lastym=0,lastxh=0,lastyh=0;

Date dummy=new Date(); //生成Data对象

GregorianCalendar cal=new GregorianCalendar();

SimpleDateFormat df=new SimpleDateFormat("yyyy MM dd HH:mm:ss");//设置时间格式

String lastdate=df.format(dummy);

Font F=new Font("TimesRoman",Font.PLAIN,14);//设置字体格式

Date dat=null;

Date timeNow=null;

Color fgcol=Color.blue;

Color fgcol2=Color.darkGray;

Panel setpanel;

Color backcolor=Color.pink;

TextField showhour,showmin,showsec,sethour,setmin,setsec;//显示当前时间文本框和定时文本框

Button onbutton;

Button offbutton;

Label hlabel1,mlabel1,slabel1,hlabel2,mlabel2,slabel2;//显示时间单位时所用的标签(时、分、秒)

Label info1=new Label("欢迎使用定时提醒闹钟"),info2=new Label("");

Label note1=new Label("当前时间:"),note2=new Label("闹钟设置:");

boolean setalerm=false,clickflag=false;//判断是否响铃和振动

int fixh=0,fixm=0,fixs=0;//记录闹钟的定时

public void init()//初始化方法

{

Integer gif_number;

int fieldx=50,fieldy1=120,fieldy2=220,fieldw=30,fieldh=20,space=50;//显示时间和定时文本框的定位参数

setLayout(null); //将布局管理器初始化为null

setpanel=new Panel();

setpanel.setLayout(null);

setpanel.add(note1);

setpanel.add(note2);

note1.setBounds(30,100,60,20);

note1.setBackground(backcolor);

note1.setForeground(Color.black);

note2.setBounds(30,180,60,20);

note2.setBackground(backcolor);

note2.setForeground(Color.black);

hlabel1=new Label();

mlabel1=new Label();

slabel1=new Label();

hlabel2=new Label();

mlabel2=new Label();

slabel2=new Label();

//显示当前时间用的文本框

showhour=new TextField("00",5);

showmin=new TextField("00",5);

showsec=new TextField("00",5);

//定时用的文本框(时、分、秒)

sethour=new TextField("00",5);

setmin=new TextField("00",5);

setsec=new TextField("00",5);

//当前时间用的文本框的位置、大小

setpanel.add(showhour);

showhour.setBounds(fieldx,fieldy1,fieldw,fieldh);

showhour.setBackground(Color.white);

//在文本框后加入单位“时”

setpanel.add(hlabel1);

hlabel1.setText("时");

hlabel1.setBackground(backcolor);

hlabel1.setForeground(Color.black);

hlabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=fieldx+space;

//当前时间的分钟文本框的位置、大小

setpanel.add(showmin);

showmin.setBounds(fieldx,fieldy1,fieldw,fieldh);

showmin.setBackground(Color.white);

//在文本框后加入单位“分”

setpanel.add(mlabel1);

mlabel1.setText("分");

mlabel1.setBackground(backcolor);

mlabel1.setForeground(Color.black);

mlabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=fieldx+space;

//当前时间的秒文本框的位置、大小

setpanel.add(showsec);

showsec.setBounds(fieldx,fieldy1,fieldw,fieldh);

showsec.setBackground(Color.white);

//在文本框后加入单位“秒”

setpanel.add(slabel1);

slabel1.setText("秒");

slabel1.setBackground(backcolor);

slabel1.setForeground(Color.black);

slabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=50;

//定时的小时文本框的位置、大小

setpanel.add(sethour);

sethour.setBounds(fieldx,fieldy2,fieldw,fieldh);

sethour.setBackground(Color.white);

//在文本框后加入单位“时”

setpanel.add(hlabel2);

hlabel2.setText("时");

hlabel2.setBackground(backcolor);

hlabel2.setForeground(Color.black);

hlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

fieldx=fieldx+space;

//定时的分钟文本框的位置、大小

setpanel.add(setmin);

setmin.setBounds(fieldx,fieldy2,fieldw,fieldh);

setmin.setBackground(Color.white);

//在文本框后加入单位“分”

setpanel.add(mlabel2);

mlabel2.setText("分");

mlabel2.setBackground(backcolor);

mlabel2.setForeground(Color.black);

mlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

fieldx=fieldx+space;

//定时的秒文本框的位置、大小

setpanel.add(setsec);

setsec.setBounds(fieldx,fieldy2,fieldw,fieldh);

setsec.setBackground(Color.white);

//在文本框后加入单位“秒”

setpanel.add(slabel2);

slabel2.setText("秒");

slabel2.setBackground(backcolor);

slabel2.setForeground(Color.black);

slabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

//设置闹钟控制按钮(on,off)

onbutton=new Button("开");

offbutton=new Button("关");

setpanel.add(onbutton);

setpanel.add(offbutton);

onbutton.setBounds(90,180,40,20);

offbutton.setBounds(140,180,40,20);

//加入一些附加的信息标签(题头,题尾)

setpanel.add(info1);

info1.setBackground(backcolor);

info1.setForeground(Color.blue);

info1.setBounds(50,50,150,20);

setpanel.add(info2);

info2.setBackground(backcolor);

info2.setForeground(Color.blue);

info2.setBounds(150,280,100,20);

//将面板加入当前容器中,并设置面板的大小和背景色

add(setpanel);

setpanel.setBounds(300,1,250,420);

setpanel.setBackground(backcolor);

//获取声音文件

ipAu=getAudioClip(getDocumentBase(),"bells/仙剑.mid");

danger=getAudioClip(getDocumentBase(),"bells/0.mid");

chirp=getAudioClip(getDocumentBase(),"bells/3.mid");

int xcenter,ycenter,s,m,h;

xcenter=145;

ycenter=162;

s=(int)cal.get(Calendar.SECOND);

m=(int)cal.get(Calendar.MINUTE);

h=(int)cal.get(Calendar.HOUR_OF_DAY);

//初始化指针位置

lastxs=(int)(Math.cos(s*3.14f/30-3.14f/2)*30+xcenter);

lastys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);

lastxm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);

lastym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);

lastxh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*18+xcenter);

lastyh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*18+ycenter);

lasts=s;

MediaTracker mt=new MediaTracker(this);//创建Tracke对象

clockp=getImage(getDocumentBase(),"休闲.png");

gif1=getImage(getDocumentBase(),"gif1.gif");

gif2=getImage(getDocumentBase(),"gif2.gif");

clock6=getImage(getDocumentBase(),"clock6.gif");

clock7=getImage(getDocumentBase(),"clock7.gif");

mt.addImage(clockp,i++);

mt.addImage(gif1,i++);

mt.addImage(gif2,i++);

mt.addImage(clock6,i++);

mt.addImage(clock7,i++);

try{mt.waitForAll();}catch(InterruptedException e){};//等待加载结束

resize(600,420);//设置窗口大小

}

public void paint(Graphics g){//重写paint()方法

int xh,yh,xm,ym,xs,ys,strike_times;

int xcenter,ycenter;

String today;

Integer gif_number;

xcenter=148;

ycenter=186;

dat=new Date();

cal.setTime(dat);

//读取当前时间

s=(int)cal.get(Calendar.SECOND);

m=(int)cal.get(Calendar.MINUTE);

h=(int)cal.get(Calendar.HOUR_OF_DAY);

today=df.format(dat);

//指针位置

xs=(int)(Math.cos(s*3.14f/30-3.14f/2)*30+xcenter);

ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);

xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);

ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);

xh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*18+xcenter);

yh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*18+ycenter);

//设置字体和颜色

g.setFont(F);

g.setColor(fgcol);

g.setColor(fgcol2);

g.setColor(getBackground());

g.fillRect(1,1,634,419);

g.drawImage(clockp,75,110,this);

g.drawImage(clock6,83,280,this);

g.setColor(fgcol2);

g.setColor(getBackground());

g.setColor(fgcol2);

//以数字方式显示年、月、日和时间

g.drawString(today,55,415);

g.drawLine(xcenter,ycenter,xs,ys);

g.setColor(fgcol);

//画指针

g.drawLine(xcenter,ycenter-1,xm,ym);

g.drawLine(xcenter-1,ycenter,xm,ym);

g.drawLine(xcenter,ycenter-1,xh,yh);

g.drawLine(xcenter-1,ycenter,xh,yh);

lastxs=xs;lastys=ys;

lastxm=xh;lastym=ym;

lastxh=xh;lastyh=yh;

lastdate=today;

if(h12)hh=h;//将系统时间变换到0-11区间

else hh=h-12;

if(hh==0) strike_times=12;//计算整点时钟声数

else strike_times=hh;

if((s==0m==0)||flag){//判断是否整点,是否是主动刷新

if(counterstrike_times){

flag=true;

g.drawImage(gif2,115,35,this);

if(lasts!=s){

if(strike){

counter++;

danger.play();//播放闹铃声

}

if(strike)strike=false;

else strike=true;

}

}

else {

counter=0;

flag=false;

}

}

else

g.drawImage(gif1,115,35,this);

int timedelta;//记录当前时间与闹铃定时的时差

Integer currh,currm,currs;//分别记录当前的时、分、秒

timeNow=new Date();

currh=new Integer(timeNow.getHours());

currm=new Integer(timeNow.getMinutes());

currs=new Integer(timeNow.getSeconds());

//判断是否要更新当前显示的时间,这样可以避免文本框出现频率闪动

if(currh.intValue()!=Integer.valueOf(showhour.getText()).intValue())

showhour.setText(currh.toString());

if(currm.intValue()!=Integer.valueOf(showmin.getText()).intValue())

showmin.setText(currh.toString());

if(currs.intValue()!=Integer.valueOf(showsec.getText()).intValue())

showsec.setText(currh.toString());

if(setalerm){ //判断是否设置了闹钟

//判断当前时间是否为闹钟所定的时间

if((currh.intValue()==fixh)(currm.intValue()==fixm)(currs.intValue()==fixs))

clickflag=true;

timedelta=currm.intValue()*60+currs.intValue()-fixm*60-fixs;

if((timedelta60)(clickflag==true)){ //若当前时间与闹钟相差时间达到60秒

chirp.play();

g.drawImage(clock7,83,280,this);

}

else{

chirp.stop();

clickflag=false;

}

}

if(lasts!=s)

ipAu.play();//播放滴答声

lasts=s;

if(canPaint){

t+=1;

if(t==12)t=0;

}

canPaint=false;

dat=null;

}

public void start(){

if(timer==null){

timer=new Thread(this);//将timer实例化

timer.start();

}

}

public void stop(){

timer=null;

}

public void run(){

while(timer!=null){

try{timer.sleep(timeout);}catch(InterruptedException e){}

canPaint=true;

repaint();//刷新画面

}

timer=null;

}

public void update(Graphics g){ //采用双缓冲技术的update()方法

if(offscreen_buf==null)

offscreen_buf=createImage(600,420);

Graphics offg=offscreen_buf.getGraphics();

offg.clipRect(1,1,599,419);

paint(offg);

Graphics ong=getGraphics();

ong.clipRect(1,1,599,419);

ong.drawImage(offscreen_buf,0,0,this);

}

public boolean action(Event evt,Object arg){ //按钮事件处理函数

if(evt.target instanceof Button){

String lable=(String)arg;

if(lable.equals("开")){

setalerm=true;

//获取输入的时间

fixh=Integer.valueOf(sethour.getText()).intValue();

fixm=Integer.valueOf(setmin.getText()).intValue();

fixs=Integer.valueOf(setsec.getText()).intValue();

clickflag=false;

}

if(lable.equals("关")){

setalerm=false;

if(chirp!=null)

chirp.stop();

clickflag=false;

}

return true;

}

return false;

}

}

java 闹钟程序

import java.util.*;

import java.awt.*;

import java.applet.*;

import java.text.*;

public class AlarmClock extends Applet implements Runnable

{

Thread timer=null; //创建线程timer

Image clockp,gif1,gif2,clock6,clock7; //clockp:闹钟的外壳,闹铃和报时鸟

int s,m,h,hh;

AudioClip ipAu,danger,chirp;

boolean canPaint=true;

boolean flag=false;

boolean strike=true;

int counter=0;

int lasts;

Image offscreen_buf=null;

int i,j,t=0;

int timeout=166;

int lastxs=0,lastys=0,lastxm=0,lastym=0,lastxh=0,lastyh=0;

Date dummy=new Date(); //生成Data对象

GregorianCalendar cal=new GregorianCalendar();

SimpleDateFormat df=new SimpleDateFormat("yyyy MM dd HH:mm:ss");//设置时间格式

String lastdate=df.format(dummy);

Font F=new Font("TimesRoman",Font.PLAIN,14);//设置字体格式

Date dat=null;

Date timeNow=null;

Color fgcol=Color.blue;

Color fgcol2=Color.darkGray;

Panel setpanel;

Color backcolor=Color.pink;

TextField showhour,showmin,showsec,sethour,setmin,setsec;//显示当前时间文本框和定时文本框

Button onbutton;

Button offbutton;

Label hlabel1,mlabel1,slabel1,hlabel2,mlabel2,slabel2;//显示时间单位时所用的标签(时、分、秒)

Label info1=new Label("欢迎使用定时提醒闹钟"),info2=new Label("");

Label note1=new Label("当前时间:"),note2=new Label("闹钟设置:");

boolean setalerm=false,clickflag=false;//判断是否响铃和振动

int fixh=0,fixm=0,fixs=0;//记录闹钟的定时

public void init()//初始化方法

{

Integer gif_number;

int fieldx=50,fieldy1=120,fieldy2=220,fieldw=30,fieldh=20,space=50;//显示时间和定时文本框的定位参数

setLayout(null); //将布局管理器初始化为null

setpanel=new Panel();

setpanel.setLayout(null);

setpanel.add(note1);

setpanel.add(note2);

note1.setBounds(30,100,60,20);

note1.setBackground(backcolor);

note1.setForeground(Color.black);

note2.setBounds(30,180,60,20);

note2.setBackground(backcolor);

note2.setForeground(Color.black);

hlabel1=new Label();

mlabel1=new Label();

slabel1=new Label();

hlabel2=new Label();

mlabel2=new Label();

slabel2=new Label();

//显示当前时间用的文本框

showhour=new TextField("00",5);

showmin=new TextField("00",5);

showsec=new TextField("00",5);

//定时用的文本框(时、分、秒)

sethour=new TextField("00",5);

setmin=new TextField("00",5);

setsec=new TextField("00",5);

//当前时间用的文本框的位置、大小

setpanel.add(showhour);

showhour.setBounds(fieldx,fieldy1,fieldw,fieldh);

showhour.setBackground(Color.white);

//在文本框后加入单位“时”

setpanel.add(hlabel1);

hlabel1.setText("时");

hlabel1.setBackground(backcolor);

hlabel1.setForeground(Color.black);

hlabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=fieldx+space;

//当前时间的分钟文本框的位置、大小

setpanel.add(showmin);

showmin.setBounds(fieldx,fieldy1,fieldw,fieldh);

showmin.setBackground(Color.white);

//在文本框后加入单位“分”

setpanel.add(mlabel1);

mlabel1.setText("分");

mlabel1.setBackground(backcolor);

mlabel1.setForeground(Color.black);

mlabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=fieldx+space;

//当前时间的秒文本框的位置、大小

setpanel.add(showsec);

showsec.setBounds(fieldx,fieldy1,fieldw,fieldh);

showsec.setBackground(Color.white);

//在文本框后加入单位“秒”

setpanel.add(slabel1);

slabel1.setText("秒");

slabel1.setBackground(backcolor);

slabel1.setForeground(Color.black);

slabel1.setBounds(fieldx+fieldw+3,fieldy1,14,20);

fieldx=50;

//定时的小时文本框的位置、大小

setpanel.add(sethour);

sethour.setBounds(fieldx,fieldy2,fieldw,fieldh);

sethour.setBackground(Color.white);

//在文本框后加入单位“时”

setpanel.add(hlabel2);

hlabel2.setText("时");

hlabel2.setBackground(backcolor);

hlabel2.setForeground(Color.black);

hlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

fieldx=fieldx+space;

//定时的分钟文本框的位置、大小

setpanel.add(setmin);

setmin.setBounds(fieldx,fieldy2,fieldw,fieldh);

setmin.setBackground(Color.white);

//在文本框后加入单位“分”

setpanel.add(mlabel2);

mlabel2.setText("分");

mlabel2.setBackground(backcolor);

mlabel2.setForeground(Color.black);

mlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

fieldx=fieldx+space;

//定时的秒文本框的位置、大小

setpanel.add(setsec);

setsec.setBounds(fieldx,fieldy2,fieldw,fieldh);

setsec.setBackground(Color.white);

//在文本框后加入单位“秒”

setpanel.add(slabel2);

slabel2.setText("秒");

slabel2.setBackground(backcolor);

slabel2.setForeground(Color.black);

slabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

//设置闹钟控制按钮(on,off)

onbutton=new Button("开");

offbutton=new Button("关");

setpanel.add(onbutton);

setpanel.add(offbutton);

onbutton.setBounds(90,180,40,20);

offbutton.setBounds(140,180,40,20);

//加入一些附加的信息标签(题头,题尾)

setpanel.add(info1);

info1.setBackground(backcolor);

info1.setForeground(Color.blue);

info1.setBounds(50,50,150,20);

setpanel.add(info2);

info2.setBackground(backcolor);

info2.setForeground(Color.blue);

info2.setBounds(150,280,100,20);

//将面板加入当前容器中,并设置面板的大小和背景色

add(setpanel);

setpanel.setBounds(300,1,250,420);

setpanel.setBackground(backcolor);

//获取声音文件

ipAu=getAudioClip(getDocumentBase(),"bells/仙剑.mid");

danger=getAudioClip(getDocumentBase(),"bells/0.mid");

chirp=getAudioClip(getDocumentBase(),"bells/3.mid");

int xcenter,ycenter,s,m,h;

xcenter=145;

ycenter=162;

s=(int)cal.get(Calendar.SECOND);

m=(int)cal.get(Calendar.MINUTE);

h=(int)cal.get(Calendar.HOUR_OF_DAY);

//初始化指针位置

lastxs=(int)(Math.cos(s*3.14f/30-3.14f/2)*30+xcenter);

lastys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);

lastxm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);

lastym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);

lastxh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*18+xcenter);

lastyh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*18+ycenter);

lasts=s;

MediaTracker mt=new MediaTracker(this);//创建Tracke对象

clockp=getImage(getDocumentBase(),"休闲.png");

gif1=getImage(getDocumentBase(),"gif1.gif");

gif2=getImage(getDocumentBase(),"gif2.gif");

clock6=getImage(getDocumentBase(),"clock6.gif");

clock7=getImage(getDocumentBase(),"clock7.gif");

mt.addImage(clockp,i++);

mt.addImage(gif1,i++);

mt.addImage(gif2,i++);

mt.addImage(clock6,i++);

mt.addImage(clock7,i++);

try{mt.waitForAll();}catch(InterruptedException e){};//等待加载结束

resize(600,420);//设置窗口大小

}

public void paint(Graphics g){//重写paint()方法

int xh,yh,xm,ym,xs,ys,strike_times;

int xcenter,ycenter;

String today;

Integer gif_number;

xcenter=148;

ycenter=186;

dat=new Date();

cal.setTime(dat);

//读取当前时间

s=(int)cal.get(Calendar.SECOND);

m=(int)cal.get(Calendar.MINUTE);

h=(int)cal.get(Calendar.HOUR_OF_DAY);

today=df.format(dat);

//指针位置

xs=(int)(Math.cos(s*3.14f/30-3.14f/2)*30+xcenter);

ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);

xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);

ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);

xh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*18+xcenter);

yh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*18+ycenter);

//设置字体和颜色

g.setFont(F);

g.setColor(fgcol);

g.setColor(fgcol2);

g.setColor(getBackground());

g.fillRect(1,1,634,419);

g.drawImage(clockp,75,110,this);

g.drawImage(clock6,83,280,this);

g.setColor(fgcol2);

g.setColor(getBackground());

g.setColor(fgcol2);

//以数字方式显示年、月、日和时间

g.drawString(today,55,415);

g.drawLine(xcenter,ycenter,xs,ys);

g.setColor(fgcol);

//画指针

g.drawLine(xcenter,ycenter-1,xm,ym);

g.drawLine(xcenter-1,ycenter,xm,ym);

g.drawLine(xcenter,ycenter-1,xh,yh);

g.drawLine(xcenter-1,ycenter,xh,yh);

lastxs=xs;lastys=ys;

lastxm=xh;lastym=ym;

lastxh=xh;lastyh=yh;

lastdate=today;

if(h12)hh=h;//将系统时间变换到0-11区间

else hh=h-12;

if(hh==0) strike_times=12;//计算整点时钟声数

else strike_times=hh;

if((s==0m==0)||flag){//判断是否整点,是否是主动刷新

if(counterstrike_times){

flag=true;

g.drawImage(gif2,115,35,this);

if(lasts!=s){

if(strike){

counter++;

danger.play();//播放闹铃声

}

if(strike)strike=false;

else strike=true;

}

}

else {

counter=0;

flag=false;

}

}

else

g.drawImage(gif1,115,35,this);

int timedelta;//记录当前时间与闹铃定时的时差

Integer currh,currm,currs;//分别记录当前的时、分、秒

timeNow=new Date();

currh=new Integer(timeNow.getHours());

currm=new Integer(timeNow.getMinutes());

currs=new Integer(timeNow.getSeconds());

//判断是否要更新当前显示的时间,这样可以避免文本框出现频率闪动

if(currh.intValue()!=Integer.valueOf(showhour.getText()).intValue())

showhour.setText(currh.toString());

if(currm.intValue()!=Integer.valueOf(showmin.getText()).intValue())

showmin.setText(currh.toString());

if(currs.intValue()!=Integer.valueOf(showsec.getText()).intValue())

showsec.setText(currh.toString());

if(setalerm){ //判断是否设置了闹钟

//判断当前时间是否为闹钟所定的时间

if((currh.intValue()==fixh)(currm.intValue()==fixm)(currs.intValue()==fixs))

clickflag=true;

timedelta=currm.intValue()*60+currs.intValue()-fixm*60-fixs;

if((timedelta60)(clickflag==true)){ //若当前时间与闹钟相差时间达到60秒

chirp.play();

g.drawImage(clock7,83,280,this);

}

else{

chirp.stop();

clickflag=false;

}

}

if(lasts!=s)

ipAu.play();//播放滴答声

lasts=s;

if(canPaint){

t+=1;

if(t==12)t=0;

}

canPaint=false;

dat=null;

}

public void start(){

if(timer==null){

timer=new Thread(this);//将timer实例化

timer.start();

}

}

public void stop(){

timer=null;

}

public void run(){

while(timer!=null){

try{timer.sleep(timeout);}catch(InterruptedException e){}

canPaint=true;

repaint();//刷新画面

}

timer=null;

}

public void update(Graphics g){ //采用双缓冲技术的update()方法

if(offscreen_buf==null)

offscreen_buf=createImage(600,420);

Graphics offg=offscreen_buf.getGraphics();

offg.clipRect(1,1,599,419);

paint(offg);

Graphics ong=getGraphics();

ong.clipRect(1,1,599,419);

ong.drawImage(offscreen_buf,0,0,this);

}

public boolean action(Event evt,Object arg){ //按钮事件处理函数

if(evt.target instanceof Button){

String lable=(String)arg;

if(lable.equals("开")){

setalerm=true;

//获取输入的时间

fixh=Integer.valueOf(sethour.getText()).intValue();

fixm=Integer.valueOf(setmin.getText()).intValue();

fixs=Integer.valueOf(setsec.getText()).intValue();

clickflag=false;

}

if(lable.equals("关")){

setalerm=false;

if(chirp!=null)

chirp.stop();

clickflag=false;

}

return true;

}

return false;

}

}

用JAVA编一个小闹钟

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.Timer;

public class Clock extends JFrame implements ActionListener {

public final int HEIGTH = 200, L0 = 50, T0 = 50,N=8;

public final double RAD = Math.PI / 180.0;

int x, y, old_X, old_Y, r, x0, y0, w, h, ang;

int sdo, mdo, hdo, old_M, old_H, hh, mm, ss;

int delay = 1000;

Calendar now;

String st, alarm, Items1, Items2,str[];

JButton jb;

JComboBox jc1, jc2, jc3;

JLabel jl1, jl2, jl3, jl4;

JMenu jm1, jm2, jm3, jm4;

JMenuBar jmb;

JMenuItem jmi1, jmi2, jmi3, jmi4, jmi5, jmi6, jmi7, jmi8, jmi9,jmi10;

JTextField jtf1, jtf2, time;

JPanel jp1, jp2, jp3;

Timer timer;

TimeZone tz = TimeZone.getTimeZone("JST");

Toolkit toolkit=Toolkit.getDefaultToolkit();;

/**

* br

* 方法说明:实现ActionListener类必须过载的方法

*/

public static void main(String[] args) {

Clock cp = new Clock();

cp.setVisible(true);

}

Clock() {

super("Java闹钟!");

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

setSize(550, 700);

setVisible(true);

Container contentPane = getContentPane();

jp2 = new JPanel();

jmb = new JMenuBar();

jm1 = new JMenu("背景颜色设置 ", true);

jmi1 = new JMenuItem("外圈颜色");

jmi1.addActionListener(this);

jmi1.setActionCommand("color1");

jm1.add(jmi1);

jmi2 = new JMenuItem("闹钟边线颜色");

jmi2.addActionListener(this);

jmi2.setActionCommand("color2");

jm1.add(jmi2);

jmi3=new JMenuItem("底盘颜色");

jmi3.addActionListener(this);

jmi3.setActionCommand("color3");

jm1.add(jmi3);

jmi4=new JMenuItem("系统时间背静颜色");

jmi4.addActionListener(this);

jmi4.setActionCommand("color4");

jm1.add(jmi4);

jmb.add(jm1);

jm2 = new JMenu("指针颜色设置 ", true);

jmi5 = new JMenuItem("秒针颜色");

jmi5.addActionListener(this);

jmi5.setActionCommand("color5");

jm2.add(jmi5);

jmi6 = new JMenuItem("分针颜色");

jmi6.addActionListener(this);

jmi6.setActionCommand("color6");

jm2.add(jmi6);

jmi7 = new JMenuItem("时针颜色");

jmi7.addActionListener(this);

jmi7.setActionCommand("color7");

jm2.add(jmi7);

jmb.add(jm2);

jm3 = new JMenu("闹铃声音设置 ", true);

jmi8 = new JMenuItem("响铃1");

jmi8.addActionListener(this);

jmi8.setActionCommand("ring1");

jm3.add(jmi8);

jmi9 = new JMenuItem("静音");

jmi9.addActionListener(this);

jmi9.setActionCommand("ring2");

jm3.add(jmi9);

jmb.add(jm3);

jm4 = new JMenu("帮助 ", true);

jmi10=new JMenuItem("使用说明");

jmi10.addActionListener(this);

jmi10.setActionCommand("help");

jm4.add(jmi10);

jmb.add(jm4);

jp2.add(jmb);

contentPane.add(jp2, BorderLayout.NORTH);

jp3 = new JPanel();

jl1 = new JLabel("闹铃时间");

jl1.setFont(new Font("楷体_GB2312", Font.BOLD, 18));

time = new JTextField("00:00", 5);

alarm = time.getText();

jb = new JButton("修改闹铃时间");

jb.addActionListener(this);

jb.setActionCommand("CC");

jp3.add(jl1);

jp3.add(time);

jp3.add(jb);

contentPane.add(jp3, BorderLayout.SOUTH);

ClockPanel clock = new ClockPanel();

contentPane.add(clock, BorderLayout.CENTER);

// 窗体添加事件监听,监听秒表的触发

ActionListener taskPerformer = new ActionListener() {

public void actionPerformed(ActionEvent evt) {

repaint();

}

};

new Timer(delay, taskPerformer).start();

}

/**

* br

* 方法说明:绘制图形

*/

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 (hh 10)

st = "0" + hh;

else

st = "" + hh;

if (mm 10)

st = st + ":0" + mm;

else

st = st + ":" + mm;

if(alarm.equals(st))

{

if(toolkit!=null)

toolkit.beep();

else {}

}

if (ss 10)

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_X 0) {

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 || newHou 0) {

JOptionPane.showMessageDialog(this, "格式错误\n小时应该是不小于0不大于23的正数",

"Error", JOptionPane.ERROR_MESSAGE);

repaint();

break judge;

}

if (newMin = 60 || newHou 0) {

JOptionPane.showMessageDialog(this, "格式错误\n分钟应该是小于60的正数", "Error",

JOptionPane.ERROR_MESSAGE);

repaint();

break judge;

}

new SetTime(newHou, newMin);

}

}

if (e.getActionCommand() == "ring1") {

toolkit=Toolkit.getDefaultToolkit();

}

if(e.getActionCommand() == "ring2"){

System.out.println("静音");

toolkit=null;

}

if (e.getActionCommand() == "color1") {

String color;

Color c;

System.out.println("color1");

color = JOptionPane.showInputDialog(this, "请输入喜欢的外圈颜色(0--255)", "128");

if (color == null) {

} else {

if (Integer.parseInt(color) 0

|| Integer.parseInt(color) 255)

JOptionPane.showInputDialog(this, "请输入喜欢的外圈颜色(0--255)", "128");

else {

c = new Color(Integer.parseInt(color));

C1 = c;

}

}

}

if(e.getActionCommand() == "color2"){

String color;

Color c;

System.out.println("color2");

color = JOptionPane.showInputDialog(this, "请输入喜欢的边线颜色(0--255)", "128");

if(color==null){}

else{if (Integer.parseInt(color) 0|| Integer.parseInt(color) 255)

JOptionPane.showInputDialog(this, "请输入喜欢的边线颜色(0--255)", "128");

else {

c = new Color(Integer.parseInt(color));

C2 = c;

}

}

}

if(e.getActionCommand() == "color3"){

String color;

Color c;

System.out.println("color3");

color = JOptionPane.showInputDialog(this, "请输入喜欢的内盘颜色(0--255)", "128");

if(color==null){}

else{if (Integer.parseInt(color) 0|| Integer.parseInt(color) 255)

JOptionPane.showInputDialog(this, "请输入喜欢的内盘颜色(0--255)", "128");

else {

c = new Color(Integer.parseInt(color));

C3 = c;

}

}

}

if(e.getActionCommand() == "color4"){

String color;

Color c;

System.out.println("color4");

color = JOptionPane.showInputDialog(this, "请输入喜欢的背景颜色(0--255)", "128");

if(color==null){}

else{if (Integer.parseInt(color) 0|| Integer.parseInt(color) 255)

JOptionPane.showInputDialog(this, "请输入喜欢的背景颜色(0--255)", "128");

else {

c = new Color(Integer.parseInt(color));

C4 = c;

}

}

}

if(e.getActionCommand() == "color5"){

String color;

Color c;

System.out.println("color5");

color = JOptionPane.showInputDialog(this, "请输入喜欢的秒针颜色(0--255)", "128");

if(color==null){}

else{if (Integer.parseInt(color) 0|| Integer.parseInt(color) 255)

JOptionPane.showInputDialog(this, "请输入喜欢的秒针颜色(0--255)", "128");

else {

c = new Color(Integer.parseInt(color));

C5 = c;

}

}

}

if(e.getActionCommand() == "color6"){

String color;

Color c;

System.out.println("color6");

color = JOptionPane.showInputDialog(this, "请输入喜欢的分针颜色(0--255)", "128");

if(color==null){}

else{if (Integer.parseInt(color) 0|| Integer.parseInt(color) 255)

JOptionPane.showInputDialog(this, "请输入喜欢的分针颜色(0--255)", "128");

else {

c = new Color(Integer.parseInt(color));

C6 = c;

}

}

}

if(e.getActionCommand() == "color7"){

String color;

Color c;

System.out.println("color7");

color = JOptionPane.showInputDialog(this, "请输入喜欢的时针颜色(0--255)", "128");

if(color==null){}

else{if (Integer.parseInt(color) 0|| Integer.parseInt(color) 255)

JOptionPane.showInputDialog(this, "请输入喜欢的时针颜色(0--255)", "128");

else {

c = new Color(Integer.parseInt(color));

C7 = c;

}

}

}

if(e.getActionCommand() == "help"){

String help;

help = JOptionPane.showInputDialog(this, "输入quit退出该闹钟的使用", "这是运行在Java中的指针式时钟");

if(help.equals("quit"))

dispose();

else {}

// timer.restart();

}

}

class SetTime {

String Hour;

String Minute;

public SetTime() { }

public SetTime(int hour, int minute) {

// 当时间参数小于10的时候在前面添加字符0

if (hour 10) {

Hour = "0" + String.valueOf(hour);

} else {

Hour = "" + String.valueOf(hour);

}

if (minute 10) {

Minute = "0" + String.valueOf(minute);

} else {

Minute = "" + String.valueOf(minute);

}

alarm = Hour + ":" + Minute;

time.setText(alarm);

repaint();

}

}

}

JAVA 闹钟程序

import java.util.*;

import java.awt.*;

import java.applet.*;

import java.text.*;

import java.awt.event.*;

public class Alarm extends Applet implements Runnable

{

Thread timer=null; //创建线程timer

Image gif1; //clockp:闹钟的外壳,闹铃和报时物

boolean setflag=false,stopflag=false,cancelflag=false;

Panel setpanel;

//获取声音文件

AudioClip ring=getAudioClip(getCodeBase(), "1.mid");

Button setbutton=new Button("SET");

Button cancelbutton=new Button("CANCEL");

Button stopbutton=new Button("STOP");

//响应按钮事件

private ActionListener setli=new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

setflag=true;

}

};

private ActionListener cancelli=new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

setflag=true;

}

};

private ActionListener stopli=new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

ring.stop();

//清除的方法

//g.clearRect(83,280,20,30);

}

};

Label note1=new Label("Alarm clock:");

//GregorianCalendar提供的是一个日历式的东东,上面又多了很多的参数,是方便操作了不少。而Date类的功能远不及其,求个和日期有联系的还要自己计算。

GregorianCalendar cal=new GregorianCalendar();

GregorianCalendar cal2=new GregorianCalendar();

SimpleDateFormat df=new SimpleDateFormat("yyyy MM dd HH:mm:ss");//设置时间格式

Date dummy=new Date(); //生成Data对象

String lastdate=df.format(dummy);

Font F=new Font("TimesRoman",Font.PLAIN,14);//设置字体格式

Date dat=null;

Date timeNow;

Color fgcol=Color.blue;

Color fgcol2=Color.darkGray;

Color backcolor=Color.blue;

Label hlabel2,mlabel2,slabel2;//显示时间单位时所用的标签(时、分、秒)

int i;

int s,m,h;

TextField sethour,setmin,setsec;//显示当前时间文本框和定时文本框

//在Applet程序中,首先自动调用初始化完成必要的初始化工作,紧接着自动调用start,在进入执行程序和返回到该页面时被调用,而从该页面转到别的页面时,stop被调用,关闭浏览器时,执行destroy。

public void init()//初始化方法

{

int fieldx=50,fieldy1=120,fieldy2=220,fieldw=30,fieldh=20,space=50;//显示时间和定时文本框的定位参数

setLayout(null); //将布局管理器初始化为null

setpanel=new Panel();

setpanel.setLayout(null);

setpanel.add(note1);

note1.setBounds(30,100,60,20);

note1.setBackground(backcolor);

note1.setForeground(Color.black);

//定时用的文本框(时、分、秒)

sethour=new TextField("00",5);

setmin=new TextField("00",5);

setsec=new TextField("00",5);

hlabel2=new Label();

mlabel2=new Label();

slabel2=new Label();

//定时的小时文本框的位置、大小

setpanel.add(sethour);

sethour.setBounds(fieldx,fieldy2,fieldw,fieldh);

sethour.setBackground(Color.white);

//在文本框后加入单位“时”

setpanel.add(hlabel2);

hlabel2.setText("h");

hlabel2.setBackground(backcolor);

hlabel2.setForeground(Color.black);

hlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

fieldx=fieldx+space;

//定时的分钟文本框的位置、大小

setpanel.add(setmin);

setmin.setBounds(fieldx,fieldy2,fieldw,fieldh);

setmin.setBackground(Color.white);

//在文本框后加入单位“分”

setpanel.add(mlabel2);

mlabel2.setText("m");

mlabel2.setBackground(backcolor);

mlabel2.setForeground(Color.black);

mlabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

fieldx=fieldx+space;

//定时的秒文本框的位置、大小

setpanel.add(setsec);

setsec.setBounds(fieldx,fieldy2,fieldw,fieldh);

setsec.setBackground(Color.white);

//在文本框后加入单位“秒”

setpanel.add(slabel2);

slabel2.setText("s");

slabel2.setBackground(backcolor);

slabel2.setForeground(Color.black);

slabel2.setBounds(fieldx+fieldw+3,fieldy2,14,20);

//设置闹钟控制按钮(on,off)

setpanel.add(cancelbutton);

setpanel.add(setbutton);

setpanel.add(stopbutton);

cancelbutton.setBounds(90,180,40,20);

setbutton.setBounds(140,180,40,20);

stopbutton.setBounds(522,180,40,20);

setbutton.addActionListener(setli);

cancelbutton.addActionListener(cancelli);

stopbutton.addActionListener(stopli);

stopbutton.setVisible(false);

//将面板加入当前容器中,并设置面板的大小和背景色

add(setpanel);

setpanel.setBounds(300,1,250,420);

setpanel.setBackground(backcolor);

/*int xcenter,ycenter,s,m,h;

//闹钟中心点所在位置

xcenter=145;

ycenter=162;

s=(int)cal.get(Calendar.SECOND);

m=(int)cal.get(Calendar.MINUTE);

h=(int)cal.get(Calendar.HOUR_OF_DAY);

//初始化指针位置

lastxs=(int)(Math.cos(s*3.14f/30-3.14f/2)*30+xcenter);

lastys=(int)(Math.sin(s*3.14f/30-3.14f/2)*30+ycenter);

lastxm=(int)(Math.cos(m*3.14f/30-3.14f/2)*25+xcenter);

lastym=(int)(Math.sin(m*3.14f/30-3.14f/2)*25+ycenter);

lastxh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*18+xcenter);

lastyh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*18+ycenter);

lasts=s; */

MediaTracker mt=new MediaTracker(this);//为给定组件创建一个跟踪媒体的MediaTracker对象,把图片添加到被跟踪的图片组


网站题目:闹钟java代码,java闹钟简单代码
当前URL:http://hbruida.cn/article/dseepej.html