黑马程序员技术交流社区

标题: 自制java闹钟小程序,有铃声的哦~~ [打印本页]

作者: 王小忠    时间: 2014-11-20 15:25
标题: 自制java闹钟小程序,有铃声的哦~~
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class JavaClip extends Frame {
        private static final long serialVersionUID = 1L;
        TextField text;
        TextField textd;
        TextField text1;
        TextField text2;
        TextField text3;
        TextField text4;
        long  delay;
        int h;
        int m;
        int ss;
        static TextField show;
        static Frame frame;
        public JavaClip() throws ParseException {
                frame = new Frame("海贼闹钟");
                frame.setLayout(null);
                frame.setBackground(new Color(184, 202, 238));
                frame.setBounds(300, 300, 440, 400);
                text = new TextField();
                textd = new TextField("设置时间格式如下:");
                StringBuffer sb=new StringBuffer("系统时间:");
                show=new TextField(sb.toString());
                show.setBounds(80, 110, 280, 30);
                show.setFont(new Font("华文楷体", ABORT, 18));
                show.setBackground(new Color(184, 202, 238));
                textd.setBounds(160, 140, 120, 30);
                text.setBounds(160, 170, 120, 30);
                Button ok = new Button("确定");
                ok.setBounds(190, 210, 60, 30);
                frame.setLocationRelativeTo(null);
                String str = JavaClip.getNow();
                Date time = new Date();
                SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                text.setText(str);
                /*time=s.parse(str);
                 h=time.getHours();
                 m=time.getMinutes();
                 ss=time.getSeconds();*/
                frame.add(text);
                ok.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                                String str = text.getText();
                                Timer t = new Timer();
                                Date time = new Date();
                                SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                try {
                                        time = s.parse(str);
                                        /*int hour=time.getHours();
                                        int minutes=time.getMinutes();
                                        int secend=time.getSeconds();
                                        if(hour-h>0){
                                                delay=(hour-h-1)*60*60*1000+(60-m+minutes)*60*1000+(60-ss+secend-10)*1000;
                                        }
                                        else if(minutes-m>0){
                                                //delay=hour*60*60+minutes*60+secend;
                                                delay=(minutes-m-1)*60*1000+(60-ss+secend-10)*1000;
                                        }
                                        else{
                                                delay=(secend-ss-10)*1000;
                                        }*/
                                } catch (ParseException e1) {
                                        // TODO Auto-generated catch block
                                        e1.printStackTrace();
                                }
                                TimerTaskClass ttc = new TimerTaskClass();
                                t.schedule(ttc, time);
                        }
                });
                frame.add(ok);
                frame.add(textd);
                frame.addWindowListener(new WindowCloser());
                frame.setVisible(true);
                showTime();
        }
        public static String getNow() {
                Calendar c1 = Calendar.getInstance();// getInstance()调用该方法或得一个Calendar实例对象
                String string = c1.get(Calendar.YEAR) + "-"
                                + (c1.get(Calendar.MONTH) + 1) + "-" + c1.get(Calendar.DATE)
                                + " " + c1.get(Calendar.HOUR) + ":" + c1.get(Calendar.MINUTE)
                                + ":" + c1.get(Calendar.SECOND);// get()方法可以或得Calendar里的一些字段值
                return string;
        }
        public static void showTime(){    //显示时间
                frame.add(show);
                SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String sb1=new String("系统时间:");
                while(true){
                        Date timeshow=new Date();
                        String sb2=s1.format(timeshow);
                        show.setText(sb1+sb2);
                        try {
                                Thread.sleep(1000);
                        } catch (InterruptedException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                        }
                }
        }
        public static void main(String args[]) throws ParseException {
                new JavaClip();
        }
        private class WindowCloser extends WindowAdapter {
                public void windowClosing(WindowEvent we) {
                        System.exit(0);
                }
        }
        class TimerTaskClass extends TimerTask {

                @SuppressWarnings("deprecation")
                @Override
                public void run() {
                        try {
                                URL cb;//创建一个资源定位符实例
                                File f = new File("E:\\HANDSUP.wav");//指定歌曲文件路径
                                cb = f.toURL();//定位
                                AudioClip aau;
                                aau = Applet.newAudioClip(cb);
                                aau.loop();// 循环播放 aau.play() 单曲 aau.stop()停止播放
                                textd.setText("笨猪,快起床");
                                text1=new TextField("最是人间留不住");
                                text2=new TextField("朱颜辞镜花辞树");
                                text3=new TextField("男儿脸刻黄金印");
                                text4=new TextField("一笑心轻白虎堂");
                                text1.setBounds(160, 240, 120, 30);
                                text2.setBounds(160, 270, 120, 30);
                                text3.setBounds(160, 300, 120, 30);
                                text4.setBounds(160, 330, 120, 30);
                                frame.add(text1);
                                frame.add(text2);
                                frame.add(text3);
                                frame.add(text4);
                        } catch (MalformedURLException e) {
                                e.printStackTrace();
                        }
                }
        }
}



作者: wez924612    时间: 2014-11-20 19:49
不错啊! 收藏了。
作者: ls61532529    时间: 2014-11-21 00:04
楼主很厉害   我也收藏了  




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2