简易闹铃java源码

package 娱乐;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;

public class 闹铃 extends JFrame implements Runnable {
JLabel ri, shi, fen, miao, dangqian, guanji;

JButton queding, dakai;

JTextField music, RI, SHI, FEN, MIAO;

int h = 0, f = 0, m = 0, r = 0;

boolean fo = false, foo = false;

public AudioClip soumd1;

int riqi, shizhong, fenzhong, miaozhong;

public 闹铃() {
   Container c = getContentPane();
   c.setLayout(new GridLayout(4, 1));
   JPanel jp = new JPanel();
   dangqian = new JLabel();
   jp.add(dangqian);
   c.add(jp);
   JPanel jp3 = new JPanel();
   guanji = new JLabel("离闹铃时间:0日0时0分0秒");
   jp3.add(guanji);
   c.add(jp3);
   JPanel jp1 = new JPanel();
   music = new JTextField(20);
   dakai = new JButton("选择闹铃音乐");
   jp1.add(music);
   jp1.add(dakai);
   c.add(jp1);
   ri = new JLabel("日");
   RI = new JTextField(4);
   shi = new JLabel("时");
   SHI = new JTextField(4);
   fen = new JLabel("分");
   FEN = new JTextField(4);
   miao = new JLabel("秒");
   MIAO = new JTextField(4);
   JPanel jp2 = new JPanel();
   jp2.add(ri);
   jp2.add(RI);
   jp2.add(shi);
   jp2.add(SHI);
   jp2.add(fen);
   jp2.add(FEN);
   jp2.add(miao);
   jp2.add(MIAO);
   queding = new JButton("确定");
   jp2.add(queding);
   c.add(jp2);
   setSize(400, 160);
   setVisible(true);
   dakai.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
     JFileChooser fileChooser = new JFileChooser(); // 实例化文件选择器
     fileChooser
       .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 设置文件选择模式,此处为文件和目录均可
     fileChooser.setCurrentDirectory(new File(".")); // 设置文件选择器当前目录
     fileChooser
       .setFileFilter(new javax.swing.filechooser.FileFilter() {
        public boolean accept(File file) { // 可接受的文件类型
         String name = file.getName().toLowerCase();
         return name.endsWith(".wav")
           || name.endsWith(".au")
           || file.isDirectory();
        }

        public String getDescription() { // 文件描述
         return "音乐文件(*.wav,*.au)";
        }
       });
     if (fileChooser.showOpenDialog(闹铃.this) == JFileChooser.APPROVE_OPTION) { // 弹出文件选择器,并判断是否点击了打开按钮
      String fileName = fileChooser.getSelectedFile()
        .getAbsolutePath(); // 得到选择文件或目录的绝对路径
      music.setText(fileName);
     }
    }
   });
   queding.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
     if (queding.getText().equals("确定")) {
      try {
       h = Integer.parseInt(SHI.getText());
       f = Integer.parseInt(FEN.getText());
       m = Integer.parseInt(MIAO.getText());
       if (1 <= h && h <= 31 && 0 <= h && h <= 23 && 0 <= f
         && f <= 59 && 0 <= m && m <= 59) {
        Date now = new Date();
        SimpleDateFormat ri = new SimpleDateFormat("dd");
        if (RI.getText() == null || RI.getText().equals("")) {
         r = Integer.parseInt(ri.format(now));
         RI.setText(ri.format(now));
        } else
         r = Integer.parseInt(RI.getText());
        fo = true;
       } else
        JOptionPane.showMessageDialog(null, "输入时间错误");
      } catch (Exception e) {
       JOptionPane.showMessageDialog(null, "请输入正确的时间");
      }

     } else {
      try{
      fo = false;
      RI.setEditable(true);
      SHI.setEditable(true);
      FEN.setEditable(true);
      MIAO.setEditable(true);
      queding.setText("确定");
      soumd1.stop();
      }catch(Exception E)
      {
       System.out.println("关闭");
      }
     }
    }
   });

}

public static void main(String agrs[]) {
   闹铃 s = new 闹铃();
   Thread t1 = new Thread(s);
   t1.start();
   s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void run() {
   while (true) {
    Date now = new Date();
    dangqian.setText("当前时间: " + now.toString());
    if (foo) {
     if (h == 0||h - shizhong<0) {
      h += 24;
      r = r - 1;
     }
     if (f == 0||f - fenzhong<0) {
      f += 60;
      h -= 1;
     }
     if (m == 0||m - miaozhong<0) {
      m += 60;
      f -= 1;
     }
     guanji.setText("离闹铃时间: " + (r - riqi) + "日" + (h - shizhong)
       + "时" + (f - fenzhong) + "分" + (m - miaozhong-1) + "秒");
     if(r<riqi)
     {
      foo=false;
      guanji.setText("离闹铃时间:0日0时0分0秒");
     }else if(r<=riqi&&h<shizhong)
     {
      foo=false;
      guanji.setText("离闹铃时间:0日0时0分0秒");
     }else if(r<=riqi&&h<=shizhong&&f<fenzhong)
     {
      foo=false;
      guanji.setText("离闹铃时间:0日0时0分0秒");
     }else if(r<=riqi&&h<=shizhong&&f<=fenzhong&&m<miaozhong)
     {
      foo=false;
      guanji.setText("离闹铃时间:0日0时0分0秒");
     }
    }
    if (fo) {
     foo = true;
     RI.setEditable(false);
     SHI.setEditable(false);
     FEN.setEditable(false);
     MIAO.setEditable(false);
     queding.setText("关闭");
     SimpleDateFormat ri = new SimpleDateFormat("dd"); // 封装 为了获取日期
     SimpleDateFormat shi = new SimpleDateFormat("HH"); // 封装 为了获取小时
     SimpleDateFormat fen = new SimpleDateFormat("mm"); // 封装 为了获取分钟
     SimpleDateFormat miao = new SimpleDateFormat("ss"); // 封装 为了获取秒钟
     riqi = Integer.parseInt(ri.format(now)); // 获取日期
     shizhong = Integer.parseInt(shi.format(now)); // 获取小时
     fenzhong = Integer.parseInt(fen.format(now)); // 获取分钟
     miaozhong = Integer.parseInt(miao.format(now)); // 获取秒钟
     if (riqi == r && shizhong == h && fenzhong == f
       && miaozhong == m) // 判断条件
     {
      try {
       soumd1 = Applet.newAudioClip(new File(music.getText())
         .toURL()); // 播放音乐
       soumd1.loop(); // 我设置的是循环播放..这样不起床都不行..
       fo = false;
       foo = false;
      } catch (MalformedURLException e) {
       e.printStackTrace();
      }
     }
    }
    try {
     Thread.sleep(1000);
    } catch (InterruptedException ie) {
    }

   }
}
}

你可能感兴趣的:(简易闹铃java源码)