闹钟的实现代码

来源:爱站网时间:2019-04-22编辑:网友分享
本文主要是介绍在Java中如何实现闹钟的代码,还不了解的朋友们可以参考一下。

       本文主要是介绍在Java中如何实现闹钟的代码,还不了解的朋友们可以参考一下。

       项目结构:

效果图一:

效果图二:

效果图三:

关于闹钟:


 

 

Alarm hongten v1.0

 使用说明:

     此文件夹中包括:
             src文件夹       存放图片和声音文件夹,使用程序的时候,请不要删除此文件夹
             alarm.jar    主要的功能程序jar包(要求安装jdk1.5或以上版本)
             ReadMe.txt     说明文档

 

  1.菜单功能说明

     文件—— >   
           上传铃声(还没实现)
           退出                     功能:退出程序
     工具—— >   
           截图工具              功能:进行截图
           更改背景        功能:更改背景
     帮助—— >   
           关于程序        功能:查看程序说明

 
  2.设置闹表

     2.1.可以在响铃时间里设置闹表时间

     2.2.还可以用快速设置,+1表示在系统时间的基础上增加一分钟,-1表示在系统时间的基础上减去一分钟                (+5,+30,-5,-30同样原理)

  3.提示铃声

     可以选择铃声类型,进行试听,试听是时候可以停止(该功能还没完善)

  4.系统托盘

     4.1.在主窗体隐藏的时候,单击左键,主窗体可以显示出来;

     4.2.在主窗体隐藏的时候,单击右键,可以进行功能选择:退出,恢复主窗体可见,截图

     4.3.在设置的闹表时间剩下30秒是时候,系统会自动提示信息(电脑右下角会弹出消息提示框)

     4.4.在设置更改背景的时候,同样会弹出提示信息(所有的提示信息会自动隐退)


下面是代码部分

 

==========================================================

/Alarm/src/com/b510/main/AlarmMain.java

 

package com.b510.main;

 import com.b510.ui.main.MyAlarm;

 /**
  *
  * @author Hongten
  */
 public class AlarmMain {

     public static void main(String[] args) {
         new MyAlarm("Alarm hongten v-1.0");
     }
 }


/Alarm/src/com/b510/menu/AlarmBackground.java

 

 

 


package com.b510.menu;

 /**
  * @author Hongten
  *
  * @time 2012-3-4  2012
  */
 public class AlarmBackground {

     /**
      * 默认的背景图片
      */
     private static String BACKGROUND_DEFAULT="src/image/background.png";
     /**
      * 水果系列
      */
     private static String BACKGROUND_FRUIT="src/image/background1.png";
     /**
      * 自行车系列
      */
     private static String BACKGROUND_BICK="src/image/background2.png";
     /**
      * 背景图片的list
      */
     private static String[] backgroundList={BACKGROUND_DEFAULT,BACKGROUND_FRUIT,BACKGROUND_BICK};
     /**
      * 获取背景图片的list的长度
      * @return 返回背景图片的list的长度
      */
     public static int getBackgroundListSize(){
         return backgroundList.length;
     }
     public static String getBackground(int bg){
         return backgroundList[bg];
     }
 }


/Alarm/src/com/b510/menu/AlarmFile.java

 

 

 


package com.b510.menu;

 /**
  *
  * @author Hongten
  * 菜单-文件
  */
 public class AlarmFile {

     /**
      * 退出程序
      */
     public static void exit(){
         System.exit(0);
     }
 }


/Alarm/src/com/b510/menu/AlarmHelp.java

 

 

 


/*
  * To change this template, choose Tools | Templates
  * and open the template in the editor.
  */
 package com.b510.menu;

 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;

 import javax.swing.ImageIcon;
 import javax.swing.JButton;
 import javax.swing.JFrame;

 import com.b510.ui.main.MyAlarm;

 /**
  *
  * @author Hongten 菜单-帮助
  */
 public class AlarmHelp extends JFrame {

     /**
      * 版本号
      */
     private static final long serialVersionUID = 5248482602468160509L;

     public AlarmHelp(String title) {
         super(title);
         initComponents();
         addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent e) {
                 AlarmHelp.this.setVisible(false);
         }});
     }

     /**
      *关闭按钮
      */
     private JButton closeButton = new JButton();
     /**
      * 应用程序名称
      */
     javax.swing.JLabel appTitleLabel = new javax.swing.JLabel();
     /**
      * 版本号 前
      */
     javax.swing.JLabel versionLabel = new javax.swing.JLabel();
     /**
      * 版本号
      */
     javax.swing.JLabel appVersionLabel = new javax.swing.JLabel();
     /**
      * 主页 前
      */
     javax.swing.JLabel homepageLabel = new javax.swing.JLabel();
     /**
      * Homepage
      */
     javax.swing.JLabel appHomepageLabel = new javax.swing.JLabel();
     /**
      * 说明
      */
     javax.swing.JLabel appDescLabel = new javax.swing.JLabel();
     /**
      * 图片
      */
     javax.swing.JLabel imageLabel = new javax.swing.JLabel();

 
     private void initComponents() {

         this.setVisible(true);
         // 设置大小不能变
         setResizable(false);
         this.setLocation(MyAlarm.pointX+60, MyAlarm.pointY+190);// 设置窗体的初始位置

         closeButton.addMouseListener(new MouseAdapter() {
              public void mouseEntered(java.awt.event.MouseEvent evt) {
                    closeButton.setIcon(new ImageIcon("src/image/biao2.png"));
                 }
              public void mouseExited(MouseEvent evt){
                  closeButton.setIcon(new ImageIcon("src/image/biao.png"));
              }
         });

         appTitleLabel.setFont(appTitleLabel.getFont().deriveFont(
                 appTitleLabel.getFont().getStyle() | java.awt.Font.BOLD,
                 appTitleLabel.getFont().getSize() + 4));
         appTitleLabel.setText("应用程序名称:"); // NOI18N
         appTitleLabel.setName("appTitleLabel"); // NOI18N

         versionLabel.setFont(versionLabel.getFont().deriveFont(
                 versionLabel.getFont().getStyle() | java.awt.Font.BOLD));
         versionLabel.setText("版本号:"); // NOI18N
         versionLabel.setName("versionLabel"); // NOI18N

         appVersionLabel.setText("1.0"); // NOI18N
         appVersionLabel.setName("appVersionLabel"); // NOI18N

         homepageLabel.setFont(homepageLabel.getFont().deriveFont(
                 homepageLabel.getFont().getStyle() | java.awt.Font.BOLD));
         homepageLabel.setText("主页:"); // NOI18N
         homepageLabel.setName("homepageLabel"); // NOI18N

         appHomepageLabel.setText("http://www.cnblogs.com/hongten"); // NOI18N
         appHomepageLabel.setName("appHomepageLabel"); // NOI18N

         appDescLabel.setText("这是一个小应用程序,闹钟"); // NOI18N
         appDescLabel.setName("appDescLabel"); // NOI18N

         imageLabel
                 .setIcon(new ImageIcon("src/image/about.png")); // NOI18N
         imageLabel.setName("imageLabel"); // NOI18N

         closeButton
                 .setIcon(new ImageIcon("src/image/biao.png"));
         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
                 getContentPane());
         getContentPane().setLayout(layout);
         layout
                 .setHorizontalGroup(layout
                         .createParallelGroup(
                                 javax.swing.GroupLayout.Alignment.LEADING)
                         .addGroup(
                                 layout
                                         .createSequentialGroup()
                                         .addComponent(imageLabel)
                                         .addGap(18, 18, 18)
                                         .addGroup(
                                                 layout
                                                         .createParallelGroup(
                                                                 javax.swing.GroupLayout.Alignment.TRAILING)
                                                         .addGroup(
                                                                 javax.swing.GroupLayout.Alignment.LEADING,
                                                                 layout
                                                                         .createSequentialGroup()
                                                                         .addGroup(
                                                                                 layout
                                                                                         .createParallelGroup(
                                                                                                 javax.swing.GroupLayout.Alignment.LEADING)
                                                                                         .addComponent(
                                                                                                 versionLabel)
                                                                                         .addComponent(
                                                                                                 homepageLabel))
                                                                         .addPreferredGap(
                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                         .addGroup(
                                                                                 layout
                                                                                         .createParallelGroup(
                                                                                                 javax.swing.GroupLayout.Alignment.LEADING)
                                                                                         .addComponent(
                                                                                                 appVersionLabel)
                                                                                         .addComponent(
                                                                                                 appHomepageLabel)))
                                                         .addComponent(
                                                                 appTitleLabel,
                                                                 javax.swing.GroupLayout.Alignment.LEADING)
                                                         .addComponent(
                                                                 appDescLabel,
                                                                 javax.swing.GroupLayout.Alignment.LEADING,
                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                 266,
                                                                 Short.MAX_VALUE)
                                                         .addComponent(
                                                                 closeButton))
                                         .addContainerGap()));
         layout
                 .setVerticalGroup(layout
                         .createParallelGroup(
                                 javax.swing.GroupLayout.Alignment.LEADING)
                         .addComponent(imageLabel,
                                 javax.swing.GroupLayout.PREFERRED_SIZE,
                                 javax.swing.GroupLayout.DEFAULT_SIZE,
                                 Short.MAX_VALUE)
                         .addGroup(
                                 layout
                                         .createSequentialGroup()
                                         .addContainerGap()
                                         .addComponent(appTitleLabel)
                                         .addPreferredGap(
                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                         .addComponent(
                                                 appDescLabel,
                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
                                         .addPreferredGap(
                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                         .addGroup(
                                                 layout
                                                         .createParallelGroup(
                                                                 javax.swing.GroupLayout.Alignment.BASELINE)
                                                         .addComponent(
                                                                 versionLabel)
                                                         .addComponent(
                                                                 appVersionLabel))
                                         .addPreferredGap(
                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                         .addGroup(
                                                 layout
                                                         .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE))
                                         .addPreferredGap(
                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                         .addGroup(
                                                 layout
                                                         .createParallelGroup(
                                                                 javax.swing.GroupLayout.Alignment.BASELINE)
                                                         .addComponent(
                                                                 homepageLabel)
                                                         .addComponent(
                                                                 appHomepageLabel))
                                         .addPreferredGap(
                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                 19, Short.MAX_VALUE)
                                         .addComponent(closeButton)
                                         .addContainerGap()));
         pack();
     }
 }


/Alarm/src/com/b510/menu/AlarmTools.java

 

 

 


/*
  * To change this template, choose Tools | Templates
  * and open the template in the editor.
  */

 package com.b510.menu;

 import mai.capture.DesktopCapture;

 /**
  *
  * @author Hongten
  * 菜单-工具
  */
 public class AlarmTools {

     /** 定义截图功能 */
     @SuppressWarnings("unused")
     private static DesktopCapture deskTopCapture;
     /**
      * 截图
      */
     public static void screenshot(){
         deskTopCapture=new DesktopCapture();
     }
 }


/Alarm/src/com/b510/ui/main/MyAlarm.java

 

 

 


package com.b510.ui.main;

 import java.awt.Color;
 import java.awt.Graphics;
 import java.awt.Point;
 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.io.IOException;
 import java.util.Calendar;

 import javax.sound.sampled.AudioFormat;
 import javax.sound.sampled.AudioInputStream;
 import javax.sound.sampled.AudioSystem;
 import javax.sound.sampled.DataLine;
 import javax.sound.sampled.SourceDataLine;
 import javax.swing.ImageIcon;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;

 import com.b510.menu.AlarmBackground;
 import com.b510.menu.AlarmFile;
 import com.b510.menu.AlarmHelp;
 import com.b510.menu.AlarmTools;

 /**
  *
  * @author Hongten
  */
 public class MyAlarm extends JFrame implements ActionListener {

     /**
      *+1
      */
     private javax.swing.JButton add1;
     /**
      * +30
      */
     private javax.swing.JButton add30;
     /**
      * +5
      */
     private javax.swing.JButton add5;
     /**
      * 时
      */
     private javax.swing.JComboBox dorp_down_hours;
     /**
      * 分
      */
     private javax.swing.JComboBox dorp_down_minute;
     /**
      * 秒
      */
     private javax.swing.JComboBox drop_down_second;
     /**
      * 试听
      */
     private javax.swing.JButton listening_test;
     /**
      * 试听-停止
      */
     private javax.swing.JButton listening_test_stop;
     /**
      * 主Panel
      */
     private javax.swing.JPanel mainPanel;
     /**
      * 菜单bar
      */
     private javax.swing.JMenuBar menuBar;
     /**
      * -1
      */
     private javax.swing.JButton minus1;
     /**
      * -30
      */
     private javax.swing.JButton minus30;
     /**
      * -5
      */
     private javax.swing.JButton minus5;
     /**
      * 当前时间
      */
     private javax.swing.JLabel now;
     /**
      * 铃声
      */
     private javax.swing.JComboBox ring_setup;
     /**
      * 截屏
      */
     private javax.swing.JMenuItem screenshot;
     /**
      * 更改背景图片
      */
     private javax.swing.JMenuItem changeBackground;
     /**
      * 停止
      */
     private javax.swing.JButton stop;
     /**
      * 工具
      */
     private javax.swing.JMenu tools;
     /**
      * 帮助
      */
     private javax.swing.JMenu help;
     /**
      * 关于
      */
     private javax.swing.JMenuItem about;
     /**
      * 退出
      */
     private javax.swing.JMenuItem exit;
     /**
      * 上传铃声
      */
     private javax.swing.JMenuItem uploadRing;
     /**
      * wenjian
      */
     private javax.swing.JMenu file;
     /**
      * 结果,即剩余时间
      */
     private JLabel result;
     /**
      * 分割线
      */
     private javax.swing.JSeparator line;
     /**
      * 变量-->小时
      */
     private String h;
     /**
      * 变量-->分钟
      */
     private String m;
     /**
      * 变量-->秒
      */
     private String s;
     /**
      * 线程的一个标志
      */
     private boolean running = true;
     /**
      * 定义图盘图盘标志
      */
     public boolean iconed = false;
     /**
      * 背景图片选择标志
      */
     private int background = 0;
     /**
      * 获取result的秒数
      */
     private int secondOfResult;
     /**
      * 更改背景图片的标志
      */
     private boolean flagOfBackground = false;
     /**
      * MyAlarm的X坐标
      */
     public static int pointX = 0;
     /**
      * MyAlarm的Y坐标
      */
     public static int pointY = 0;
     /** 定义托盘 */
     MyTray myTray;
     /**
      * 版本号
      */
     private static final long serialVersionUID = -6601825053136983041L;

     public MyAlarm(String title) {
         this.setTitle(title);
         init();
     }

     /**
      * 初始化背景图片
      */
     public void initMainPanel() {
         mainPanel = new JPanel() {
             private static final long serialVersionUID = 1L;

             protected void paintComponent(Graphics g) {
                 ImageIcon icon = new ImageIcon(AlarmBackground
                         .getBackground(background));
                 g.drawImage(icon.getImage(), 0, 0, 545, 463, null);
             }
         };
     }

     /**
      * 主界面初始化
      */
     public void init() {
         initMainPanel();
         now = new javax.swing.JLabel();
         stop = new javax.swing.JButton();
         add1 = new javax.swing.JButton();
         add5 = new javax.swing.JButton();
         add30 = new javax.swing.JButton();
         minus1 = new javax.swing.JButton();
         minus5 = new javax.swing.JButton();
         minus30 = new javax.swing.JButton();
         listening_test = new javax.swing.JButton();
         listening_test_stop = new javax.swing.JButton();
         dorp_down_hours = new javax.swing.JComboBox();
         dorp_down_minute = new javax.swing.JComboBox();
         drop_down_second = new javax.swing.JComboBox();
         ring_setup = new javax.swing.JComboBox();
         menuBar = new javax.swing.JMenuBar();
         file = new javax.swing.JMenu();
         uploadRing = new javax.swing.JMenuItem();
         exit = new javax.swing.JMenuItem();
         tools = new javax.swing.JMenu();
         screenshot = new javax.swing.JMenuItem();
         changeBackground = new javax.swing.JMenuItem();
         help = new javax.swing.JMenu();
         about = new javax.swing.JMenuItem();
         line = new javax.swing.JSeparator();
         result = new javax.swing.JLabel();

         mainPanel.setName("mainPanel"); // NOI18N

         result.setForeground(Color.RED);
         result.setName("result");
         result.setVisible(false);

         now.setFont(now.getFont().deriveFont(
                 now.getFont().getStyle() | java.awt.Font.BOLD,
                 now.getFont().getSize() + 70));
         now.setName("now"); // NOI18N
         // 时间监听器,得到系统时间和设置好时间后,得到剩余时间
         timeListener();

         stop.setText("stop"); // NOI18N
         stop.setName("stop"); // NOI18N
         // 初始化的时候是不可见的
         stop.setVisible(false);
         stop.addActionListener(this);

         add1.setText("+1"); // NOI18N
         add1.setName("add1"); // NOI18N
         add1.addActionListener(this);

         add5.setText("+5"); // NOI18N
         add5.setName("add5"); // NOI18N
         add5.addActionListener(this);

         add30.setText("+30"); // NOI18N
         add30.setName("add30"); // NOI18N
         add30.addActionListener(this);

         minus1.setText("-1"); // NOI18N
         minus1.setName("minus1"); // NOI18N
         minus1.addActionListener(this);

         minus5.setText("-5"); // NOI18N
         minus5.setName("minus5"); // NOI18N
         minus5.addActionListener(this);

         minus30.setText("-30"); // NOI18N
         minus30.setName("minus30"); // NOI18N
         minus30.addActionListener(this);

         listening_test.setText("试听"); // NOI18N
         listening_test.setName("listening_test"); // NOI18N
         listening_test.addActionListener(this);

         listening_test_stop.setText("停止"); // NOI18N
         listening_test_stop.setName("listening_test_stop"); // NOI18N
         listening_test_stop.addActionListener(this);

         dorp_down_hours.setModel(new javax.swing.DefaultComboBoxModel(
                 new String[] { "关闭", "00", "01", "02", "03", "04", "05", "06",
                         "07", "08", "09", "10", "11", "12", "13", "14", "15",
                         "16", "17", "18", "19", "20", "21", "22", "23" }));
         dorp_down_hours.setName("dorp_down_hours"); // NOI18N
         dorp_down_hours.addActionListener(this);

         dorp_down_minute.setModel(new javax.swing.DefaultComboBoxModel(
                 new String[] { "关闭", "00", "01", "02", "03", "04", "05", "06",
                         "07", "08", "09", "10", "11", "12", "13", "14", "15",
                         "16", "17", "18", "19", "20", "21", "22", "23", "24",
                         "25", "26", "27", "28", "29", "30", "31", "32", "33",
                         "34", "35", "36", "37", "38", "39", "40", "41", "42",
                         "43", "44", "45", "46", "47", "48", "49", "50", "51",
                         "52", "53", "54", "55", "56", "57", "58", "59" }));
         dorp_down_minute.setName("dorp_down_minute"); // NOI18N
         dorp_down_minute.addActionListener(this);

         drop_down_second.setModel(new javax.swing.DefaultComboBoxModel(
                 new String[] { "关闭", "00", "01", "02", "03", "04", "05", "06",
                         "07", "08", "09", "10", "11", "12", "13", "14", "15",
                         "16", "17", "18", "19", "20", "21", "22", "23", "24",
                         "25", "26", "27", "28", "29", "30", "31", "32", "33",
                         "34", "35", "36", "37", "38", "39", "40", "41", "42",
                         "43", "44", "45", "46", "47", "48", "49", "50", "51",
                         "52", "53", "54", "55", "56", "57", "58", "59" }));
         drop_down_second.setName("drop_down_second"); // NOI18N
         drop_down_second.addActionListener(this);

         ring_setup.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
                 "铃声一", "铃声二", "铃声三", "铃声四", "铃声五", "铃声六", "铃声七" }));
         ring_setup.setName("ring_setup"); // NOI18N
         // 主要的布局
         omponentLayout();

         menuBar.setName("menuBar"); // NOI18N

         file.setText("文件"); // NOI18N
         file.setName("file"); // NOI18N

         uploadRing.setText("上传铃声"); // NOI18N
         uploadRing.setName("uploadRing"); // NOI18N
         uploadRing.addActionListener(this);
         file.add(uploadRing);

         file.add(line);

         exit.setText("退出");
         exit.setName("exit"); // NOI18N
         exit.addActionListener(this);
         file.add(exit);

         menuBar.add(file);

         tools.setText("工具"); // NOI18N
         tools.setName("tools"); // NOI18N

         screenshot.setText("截图工具"); // NOI18N
         screenshot.setName("screenshot"); // NOI18N
         screenshot.addActionListener(this);
         tools.add(screenshot);

         changeBackground.setText("更改背景");
         changeBackground.setName("changeBackground");
         changeBackground.addActionListener(this);
         tools.add(changeBackground);

         menuBar.add(tools);

         help.setText("帮助"); // NOI18N
         help.setName("help"); // NOI18N

         about.setText("关于软件");
         about.setName("about"); // NOI18N
         about.addActionListener(this);
         help.add(about);

         menuBar.add(help);

         this.add(mainPanel);
         setJMenuBar(menuBar);

         this.setVisible(true);
         this.setSize(550, 516);
         // this.pack();
         this.setLocationRelativeTo(null);
         this.setResizable(false);
         // this.setLocation(470, 250);
         // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         // 当点击"-"最小化按钮时,系统会最小化到托盘
         addWindowListener(new WindowAdapter() {
             public void windowIconified(WindowEvent e) {
                 iconed = true;
                 setVisible(false);
             }

             public void windowClosing(WindowEvent e) {
                 // 当点击"X"关闭窗口按钮时,会询问用户是否要最小化到托盘
                 // 是,表示最小化到托盘,否,表示退出
                 int option = JOptionPane.showConfirmDialog(MyAlarm.this,
                         "是否最小化到托盘?", "提示:", JOptionPane.YES_NO_OPTION);
                 if (option == JOptionPane.YES_OPTION) {
                     iconed = true;
                     MyAlarm.this.setVisible(false);
                 } else {
                     AlarmFile.exit();
                 }
             }
         });
         // 初始化自定义托盘
         myTray = new MyTray(MyAlarm.this);

     }

     /**
      * 组件的布局,不要轻易动啊
      */
     private void omponentLayout() {
         javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(
                 mainPanel);
         mainPanel.setLayout(mainPanelLayout);
         mainPanelLayout
                 .setHorizontalGroup(mainPanelLayout
                         .createParallelGroup(
                                 javax.swing.GroupLayout.Alignment.LEADING)
                         .addGroup(
                                 javax.swing.GroupLayout.Alignment.TRAILING,
                                 mainPanelLayout
                                         .createSequentialGroup()
                                         .addContainerGap(170, Short.MAX_VALUE)
                                         .addGroup(
                                                 mainPanelLayout
                                                         .createParallelGroup(
                                                                 javax.swing.GroupLayout.Alignment.LEADING)
                                                         .addGroup(
                                                                 mainPanelLayout
                                                                         .createSequentialGroup()
                                                                         .addComponent(
                                                                                 result)
                                                                         .addContainerGap())
                                                         .addGroup(
                                                                 javax.swing.GroupLayout.Alignment.TRAILING,
                                                                 mainPanelLayout
                                                                         .createParallelGroup(
                                                                                 javax.swing.GroupLayout.Alignment.LEADING)
                                                                         .addGroup(
                                                                                 mainPanelLayout
                                                                                         .createSequentialGroup()
                                                                                         .addComponent(
                                                                                                 now,
                                                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                 483,
                                                                                                 Short.MAX_VALUE)
                                                                                         .addContainerGap())
                                                                         .addGroup(
                                                                                 javax.swing.GroupLayout.Alignment.TRAILING,
                                                                                 mainPanelLayout
                                                                                         .createSequentialGroup()
                                                                                         .addGroup(
                                                                                                 mainPanelLayout
                                                                                                         .createParallelGroup(
                                                                                                                 javax.swing.GroupLayout.Alignment.LEADING)
                                                                                                         .addGroup(
                                                                                                                 mainPanelLayout
                                                                                                                         .createSequentialGroup()
                                                                                                                         .addComponent(
                                                                                                                                 dorp_down_hours,
                                                                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                                                 74,
                                                                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                                         .addPreferredGap(
                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                         .addComponent(
                                                                                                                                 dorp_down_minute,
                                                                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                                                 65,
                                                                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                                         .addPreferredGap(
                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                         .addComponent(
                                                                                                                                 drop_down_second,
                                                                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                                                 62,
                                                                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                                         .addPreferredGap(
                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                         .addComponent(
                                                                                                                                 stop))
                                                                                                         .addGroup(
                                                                                                                 mainPanelLayout
                                                                                                                         .createSequentialGroup()
                                                                                                                         .addComponent(
                                                                                                                                 add1)
                                                                                                                         .addPreferredGap(
                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                         .addComponent(
                                                                                                                                 add5)
                                                                                                                         .addPreferredGap(
                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                         .addComponent(
                                                                                                                                 add30)
                                                                                                                         .addPreferredGap(
                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                         .addComponent(
                                                                                                                                 minus1)
                                                                                                                         .addPreferredGap(
                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                         .addComponent(
                                                                                                                                 minus5)
                                                                                                                         .addPreferredGap(
                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                         .addComponent(
                                                                                                                                 minus30))
                                                                                                         .addGroup(
                                                                                                                 mainPanelLayout
                                                                                                                         .createSequentialGroup()
                                                                                                                         .addComponent(
                                                                                                                                 ring_setup,
                                                                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                                         .addPreferredGap(
                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                         .addComponent(
                                                                                                                                 listening_test)
                                                                                                                         .addPreferredGap(
                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                                         .addComponent(
                                                                                                                                 listening_test_stop)))
                                                                                         .addGap(
                                                                                                 73,
                                                                                                 73,
                                                                                                 73))))));
         mainPanelLayout
                 .setVerticalGroup(mainPanelLayout
                         .createParallelGroup(
                                 javax.swing.GroupLayout.Alignment.LEADING)
                         .addGroup(
                                 javax.swing.GroupLayout.Alignment.TRAILING,
                                 mainPanelLayout
                                         .createSequentialGroup()
                                         .addGap(120, 120, 120)
                                         .addComponent(result)
                                         .addGap(24, 24, 24)
                                         .addComponent(
                                                 now,
                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
                                                 Short.MAX_VALUE)
                                         .addGap(36, 36, 36)
                                         .addGroup(
                                                 mainPanelLayout
                                                         .createParallelGroup(
                                                                 javax.swing.GroupLayout.Alignment.BASELINE)
                                                         .addComponent(
                                                                 dorp_down_hours,
                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
                                                         .addComponent(
                                                                 dorp_down_minute,
                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
                                                         .addComponent(
                                                                 drop_down_second,
                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
                                                         .addComponent(stop))
                                         .addPreferredGap(
                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                         .addGroup(
                                                 mainPanelLayout
                                                         .createParallelGroup(
                                                                 javax.swing.GroupLayout.Alignment.BASELINE)
                                                         .addComponent(add1)
                                                         .addComponent(add5)
                                                         .addComponent(add30)
                                                         .addComponent(minus1)
                                                         .addComponent(minus5)
                                                         .addComponent(minus30))
                                         .addGap(13, 13, 13)
                                         .addGroup(
                                                 mainPanelLayout
                                                         .createParallelGroup(
                                                                 javax.swing.GroupLayout.Alignment.BASELINE)
                                                         .addComponent(
                                                                 ring_setup,
                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
                                                         .addComponent(
                                                                 listening_test)
                                                         .addComponent(
                                                                 listening_test_stop))
                                         .addGap(58, 58, 58)));
     }

     /**
      * 时间监听器,得到系统时间和设置好时间后,得到剩余时间
      */
     public void timeListener() {
         new Thread(new Runnable() {// 设置一个线程
                     public void run() {
                         while (true) {
                             try {
                                 Thread.sleep(1000);
                             } catch (Exception e) {
                                 e.printStackTrace();
                             }
                             initMainPanel();
                             now.setText(now());// 得到系统时间
                             result.setText(surplus_time());// 得到剩余时间
                         }
                     }
                 }).start();
     }

     /**
      * 播放声音的监听器
      */
     public void myListener() {
         new Thread(new Runnable() {// 设置一个线程
                     public void run() {
                         while (true) {
                             try {
                                 Thread.sleep(1000);
                             } catch (Exception e) {
                                 e.printStackTrace();
                             }
                             executeSound();// 播放声音
                         }
                     }
                 }).start();
     }

     /**
      * 获取返回结果
      *
      * @return result值
      */
     public String surplus_time() {
         String r = null;
         // 获取系统时,分,秒
         int h = getHour();
         int m = getMunite();
         int s = getSecond();
         // 获取设置的响铃时间
         int dh = 0;
         int dm = 0;
         int ds = 0;
         if (dorp_down_hours.getSelectedIndex() != 0) {
             dh = dorp_down_hours.getSelectedIndex() - 1;
         }
         if (dorp_down_minute.getSelectedIndex() != 0) {
             dm = dorp_down_minute.getSelectedIndex() - 1;
         }
         if (drop_down_second.getSelectedIndex() != 0) {
             ds = drop_down_second.getSelectedIndex() - 1;
         }
         int hour = dh - h;
         int min = dm - m;
         int sec = ds - s;
         if (hour == 0) {
             if (min == 0) {
                 if (sec == 0) {
                     r = "时间到了哦!";
                 }
                 if (sec                      hour += 23;
                     min += 59;
                     sec += 59;
                 }
             }
             if (min                  hour += 23;
                 if (sec                      min -= 1;
                     sec += 59;
                 }
                 min += 60;
             }
             if (min >= 0) {
                 if (sec                      min -= 1;
                     sec += 59;
                 }
                 if (sec > 0) {
                     // sec=sec;
                 }
             }
         }
         if (hour              if (min                  if (sec                      hour -= 1;
                     min += 59;
                     sec += 59;
                 }
             }
             if (min > 0) {
                 if (sec                      min -= 1;
                     sec += 59;
                 }
             }
             hour += 24;
         }
         if (hour > 0) {
             if (min == 0) {
                 if (sec                      hour -= 1;
                     min += 59;
                     sec += 59;
                 }
             }
             if (min                  if (sec                      min -= 1;
                     sec += 59;
                 }
                 min += 60;
                 hour -= 1;
             }
             if (min > 0) {
                 if (sec                      min -= 1;
                     sec += 59;
                 }
             }
         }

         if (sec == 30 && min == 0 && hour == 0) {
             setSecondOfResult(sec);
         }
         r = new String("剩:" + hour + "时" + min + "分" + sec + "秒");
         if (hour == 0 && min == 0 && sec              r = "时间到了哦!";
         }
         // result.setText(r);
         return r;
     }

     public int getSecondOfResult() {
         return secondOfResult;
     }

     public void setSecondOfResult(int sec) {
         this.secondOfResult = sec;
     }

     public boolean getFlagOfBackground() {
         return flagOfBackground;
     }

     public void setFlagOfBackground(boolean flag) {
         this.flagOfBackground = flag;
     }

     /**
      * 时间到了的时候就播放声音
      */
     public void executeSound() {
         // 获取系统时,分,秒
         int h = getHour();
         int m = getMunite();
         int s = getSecond();
         // 获取设置的响铃时间
         int dh = 0;
         int dm = 0;
         int ds = 0;
         if (dorp_down_hours.getSelectedIndex() != 0) {
             dh = dorp_down_hours.getSelectedIndex() - 1;
         }
         if (dorp_down_minute.getSelectedIndex() != 0) {
             dm = dorp_down_minute.getSelectedIndex() - 1;
         }
         if (drop_down_second.getSelectedIndex() != 0) {
             ds = drop_down_second.getSelectedIndex() - 1;
         }
         int hour = dh - h;
         int min = dm - m;
         int sec = ds - s;
         if (hour == 0 && min == 0 && sec == 0) {
             // 主窗体设置为可见
             setVisible(true);
             // 设置窗口前端显示
             setExtendedState(JFrame.NORMAL);
             setAlwaysOnTop(true);
             // 播放声音
             new Thread(new AlarmSound(ring_setup.getSelectedIndex())).start();
             // 震动
             shack();
         }
     }

     /**
      * 得到系统时间当前时间,并返回
      *
      * @return 返回系统当前时间
      */
     public String now() {
         Calendar calendar = Calendar.getInstance();
         int hour = calendar.get(Calendar.HOUR_OF_DAY);
         int min = calendar.get(Calendar.MINUTE);
         int sec = calendar.get(Calendar.SECOND);
         // 小于10的时候,在前面加0
         String h = hour          String m = min          String s = sec          String current = new String(h + hour + ":" + m + min + ":" + s + sec);
         return current;
     }

     /**
      * 布局下面的按钮和下拉选项的监听器
      */
     @SuppressWarnings("deprecation")
     @Override
     public void actionPerformed(ActionEvent e) {

         /**
          * 获取dorp_down_hours,dorp_down_minute,drop_down_second的值
          */
         if (e.getSource() == dorp_down_hours) {
             valueJudgment();
         }
         if (e.getSource() == dorp_down_minute) {
             valueJudgment();
         }
         if (e.getSource() == drop_down_second) {
             valueJudgment();
         }
         if (e.getSource() == stop) {
             stopActionPerformed();
         }
         if (e.getSource() == add1) {
             add1Action();
             myListener();
         }
         if (e.getSource() == add5) {
             add5Action();
             myListener();
         }
         if (e.getSource() == add30) {
             add30Action();
             myListener();
         }
         if (e.getSource() == minus1) {
             minus1Action();
             myListener();
         }
         if (e.getSource() == minus5) {
             minus5Action();
             myListener();
         }
         if (e.getSource() == minus30) {
             minus30Action();
             myListener();
         }
         AlarmSound alarmSound = new AlarmSound(ring_setup.getSelectedIndex());
         Thread alarmThread = new Thread(alarmSound);
         if (e.getSource() == listening_test) {
             // 试听
             if (!alarmThread.isAlive()) {
                 alarmThread.start();
             }
             if (running) {
                 alarmThread.resume();
             }
             listening_test.setEnabled(false);
         }
         if (e.getSource() == listening_test_stop) {
             // 停止试听
             listening_test.setEnabled(true);
             if (running) {
                 alarmThread.suspend();
             }
             running = !running;
         }
         if (e.getSource() == uploadRing) {
             // 上传文件
         }
         if (e.getSource() == exit) {
             // 退出程序
             AlarmFile.exit();
         }
         if (e.getSource() == screenshot) {
             // 主窗体隐藏
             iconed = true;
             MyAlarm.this.setVisible(false);
             // 开始截图
             AlarmTools.screenshot();
         }
         if (e.getSource() == changeBackground) {
             changeBG();
         }
         if (e.getSource() == about) {
             pointX = getMyAlarmX();
             pointY = getMyAlarmY();
             new AlarmHelp("关于程序");
         }
     }

     /**
      * 更改背景图片 当点击工具-- >更改背景,这里就可以处理更改背景图片
      */
     private void changeBG() {
         setFlagOfBackground(true);
         if (background              iconed = true;
             setVisible(false);
             if (background != AlarmBackground.getBackgroundListSize() - 1) {
                 ++background;
             } else {
                 background = 0;
             }
             initMainPanel();
         }
     }

     /**
      * 点击-30按钮的时候执行的动作
      */
     private void minus30Action() {
         isHMSZero();
         if (dorp_down_minute.getSelectedIndex() - 30              dorp_down_minute.setSelectedIndex(dorp_down_minute
                     .getSelectedIndex() + 30);// 60-30
             if (dorp_down_hours.getSelectedIndex() == 0) {
                 dorp_down_hours.setSelectedIndex(24);// 设置为23
             } else {
                 // 小时数-1
                 dorp_down_hours.setSelectedIndex(dorp_down_hours
                         .getSelectedIndex() - 1);
             }
         } else {
             // 分钟数-30
             dorp_down_minute.setSelectedIndex(dorp_down_minute
                     .getSelectedIndex() - 30);
         }
         valueJudgment();
     }

     /**
      * 点击-5按钮的时候执行的动作
      */
     private void minus5Action() {
         isHMSZero();
         if (dorp_down_minute.getSelectedIndex() - 5              dorp_down_minute.setSelectedIndex(dorp_down_minute
                     .getSelectedIndex() + 55);// 60-5
             if (dorp_down_hours.getSelectedIndex() == 0) {
                 dorp_down_hours.setSelectedIndex(24);// 设置为23
             } else {
                 // 小时数-1
                 dorp_down_hours.setSelectedIndex(dorp_down_hours
                         .getSelectedIndex() - 1);
             }
         } else {
             // 分钟数-5
             dorp_down_minute.setSelectedIndex(dorp_down_minute
                     .getSelectedIndex() - 5);
         }
         valueJudgment();
     }

     /**
      * 点击-1按钮的时候执行的动作
      */
     private void minus1Action() {
         isHMSZero();
         if (dorp_down_minute.getSelectedIndex() - 1 == 0) {
             dorp_down_minute.setSelectedIndex(60);// 设置为59
             if (dorp_down_hours.getSelectedIndex() == 0) {
                 dorp_down_hours.setSelectedIndex(24);// 设置为23
             } else {
                 // 小时数-1
                 dorp_down_hours.setSelectedIndex(dorp_down_hours
                         .getSelectedIndex() - 1);
             }
         } else {
             // 分钟数-1
             dorp_down_minute.setSelectedIndex(dorp_down_minute
                     .getSelectedIndex() - 1);
         }
         valueJudgment();
     }

     /**
      * 判断dorp_down_hours,dorp_down_minute,drop_down_second当前是否为0,即:"关闭"
      */
     private void isHMSZero() {
         // 如果小时数还没有设置,那么就设置为当前小时数
         if (dorp_down_hours.getSelectedIndex() == 0) {
             dorp_down_hours.setSelectedIndex(getHour() + 1);
         }
         // 如果分钟数还没有设置,那么就设置为当前分钟数
         if (dorp_down_minute.getSelectedIndex() == 0) {
             dorp_down_minute.setSelectedIndex(getMunite() + 1);
         }
         // 如果秒钟还没有设置,那么就设置为当前秒钟
         if (drop_down_second.getSelectedIndex() == 0) {
             drop_down_second.setSelectedIndex(getSecond());
         }
     }

     /**
      * 点击+30按钮的时候执行的动作
      */
     private void add30Action() {
         isHMSZero();
         if (dorp_down_minute.getSelectedIndex() + 30 > 60) {
             dorp_down_minute.setSelectedIndex(dorp_down_minute
                     .getSelectedIndex() - 30);// +30-60
             if (dorp_down_hours.getSelectedIndex() > 23) {
                 dorp_down_hours.setSelectedIndex(1);// 设置为00
             } else {
                 // 小时数+1
                 dorp_down_hours.setSelectedIndex(dorp_down_hours
                         .getSelectedIndex() + 1);
             }
         } else {
             // 分钟数+30
             dorp_down_minute.setSelectedIndex(dorp_down_minute
                     .getSelectedIndex() + 30);
         }
         valueJudgment();
     }

     /**
      * 点击+5按钮的时候执行的动作
      */
     private void add5Action() {
         isHMSZero();
         if (dorp_down_minute.getSelectedIndex() + 5 > 60) {
             dorp_down_minute.setSelectedIndex(dorp_down_minute
                     .getSelectedIndex() - 55);// +5-60
             if (dorp_down_hours.getSelectedIndex() > 23) {
                 dorp_down_hours.setSelectedIndex(1);// 设置为00
             } else {
                 // 小时数+1
                 dorp_down_hours.setSelectedIndex(dorp_down_hours
                         .getSelectedIndex() + 1);
             }
         } else {
             // 分钟数+5
             dorp_down_minute.setSelectedIndex(dorp_down_minute
                     .getSelectedIndex() + 5);
         }
         valueJudgment();
     }

     /**
      * 点击+1按钮的时候执行的动作
      */
     private void add1Action() {
         isHMSZero();
         if (dorp_down_minute.getSelectedIndex() + 1 > 60) {
             dorp_down_minute.setSelectedIndex(1);// 设置为00
             if (dorp_down_hours.getSelectedIndex() > 23) {
                 dorp_down_hours.setSelectedIndex(1);// 设置为00
             } else {
                 // 小时数+1
                 dorp_down_hours.setSelectedIndex(dorp_down_hours
                         .getSelectedIndex() + 1);
             }
         } else {
             // 分钟数+1
             dorp_down_minute.setSelectedIndex(dorp_down_minute
                     .getSelectedIndex() + 1);
         }
         valueJudgment();
     }

     /**
      * 给h,m,s三个变量赋值,并判断他们的值
      */
     private void valueJudgment() {
         h = dorp_down_hours.getSelectedItem().toString();
         m = dorp_down_minute.getSelectedItem().toString();
         s = drop_down_second.getSelectedItem().toString();
         hsmCheck();
     }

     /**
      * 检查时,分,秒的值,如果都不是"关闭",那么
      *
      *
  • stop按钮要显示出来


  •       *
  • result要显示出来剩余时间


  •       *
  • ring_setup要设置为不可用
  • listening_test按钮为不可用

  •       * listening_test_stop按钮为不可用

  •       */
         private void hsmCheck() {
             if (h != "关闭" && m != "关闭" && s != "关闭") {
                 stop.setVisible(true);
                 result.setVisible(true);
                 ring_setup.setEnabled(false);
                 listening_test.setEnabled(false);
                 listening_test_stop.setEnabled(false);
             }
         }

         /**
          * stop按钮的动作:

  •       * 把dorp_down_hours,dorp_down_minute,drop_down_second的值设置为"关闭"

  •       * 隐藏result
  • ring_setup设置为可用
  • listening_test按钮为可用

  •       * listening_test_stop按钮为可用
  • stop按钮设置为不可见
  • 停止声音播放

  •       */
         private void stopActionPerformed() {
             dorp_down_hours.setSelectedIndex(0);
             dorp_down_minute.setSelectedIndex(0);
             drop_down_second.setSelectedIndex(0);
             result.setVisible(false);
             ring_setup.setEnabled(true);
             listening_test.setEnabled(true);
             listening_test_stop.setEnabled(true);
             // 这里要停止声音
             stop.setVisible(false);
         }

         /**
          * 获取当前小时数
          *
          * @return 返回当前小时数
          */
         private int getHour() {
             return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
         }

         /**
          * 获取当前分钟数
          *
          * @return 返回当前分钟数
          */
         private int getMunite() {
             return Calendar.getInstance().get(Calendar.MINUTE);
         }

         /**
          * 获取当前秒钟数
          *
          * @return 返回当前秒钟数
          */
         private int getSecond() {
             return Calendar.getInstance().get(Calendar.SECOND);
         }

         /**
          * 实现震动功能
          */
         public void shack() {
             Point p = MyAlarm.this.getLocation();
             int x = (int) p.getX();
             int y = (int) p.getY();
             for (int i = 0; i              MyAlarm.this.setLocation(x - 5, y);
                 MyAlarm.this.setLocation(x, y + 5);
                 MyAlarm.this.setLocation(x + 5, y);
                 MyAlarm.this.setLocation(x, y - 5);
             }
             MyAlarm.this.setLocation(x, y);
         }

         /**
          * 获取MyAlarm的X坐标
          *
          * @return 返回MyAlarm的X坐标
          */
         public int getMyAlarmX() {
             return (int) getLocation().getX();
         }

         /**
          * 获取MyAlarm的Y坐标
          *
          * @return 返回MyAlarm的Y坐标
          */
         public int getMyAlarmY() {
             return (int) MyAlarm.this.getLocation().getY();
         }

         /**
          * 闹铃类
          *
          * @author Hongten
          *
          * @time 2012-3-2 2012
          */
         class AlarmSound implements Runnable {
             private String temAlarm;
             private String alarm0Path = "src/image/sound/alarm0.wav";//
             private String alarm1Path = "src/image/sound/alarm1.wav";//
             private String alarm2Path = "src/image/sound/alarm2.wav";//
             private String alarm3Path = "src/image/sound/alarm3.wav";//
             private String alarm4Path = "src/image/sound/alarm4.wav";//
             private String alarm5Path = "src/image/sound/alarm5.wav";//
             private String alarm6Path = "src/image/sound/alarm6.wav";//
             private String alarm7Path = "src/image/sound/alarm7.wav";//
             private String alarm8Path = "src/image/sound/alarm8.wav";//

             public AlarmSound(int a) {
                 switch (a) {
                 case 0:
                     temAlarm = alarm0Path;
                     break;
                 case 1:
                     temAlarm = alarm1Path;
                     break;
                 case 2:
                     temAlarm = alarm2Path;
                     break;
                 case 3:
                     temAlarm = alarm3Path;
                     break;
                 case 4:
                     temAlarm = alarm4Path;
                     break;
                 case 5:
                     temAlarm = alarm5Path;
                     break;
                 case 6:
                     temAlarm = alarm6Path;
                     break;
                 case 7:
                     temAlarm = alarm7Path;
                     break;
                 case 8:
                     temAlarm = alarm8Path;
                     break;
                 }
             }

             // 读取声音文件,并且播放出来
             public void run() {
                 File soundFile = new File(temAlarm);
                 AudioInputStream audioInputStream = null;
                 try {
                     audioInputStream = AudioSystem.getAudioInputStream(soundFile);
                 } catch (Exception e1) {
                     e1.printStackTrace();
                     return;
                 }
                 AudioFormat format = audioInputStream.getFormat();
                 SourceDataLine auline = null;
                 DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
                 try {
                     auline = (SourceDataLine) AudioSystem.getLine(info);
                     auline.open(format);
                 } catch (Exception e) {
                     e.printStackTrace();
                     return;
                 }
                 auline.start();
                 int nBytesRead = 0;
                 byte[] abData = new byte[512];
                 try {
                     while (nBytesRead != -1) {
                         nBytesRead = audioInputStream
                                 .read(abData, 0, abData.length);
                         if (nBytesRead >= 0)
                             auline.write(abData, 0, nBytesRead);
                     }
                 } catch (IOException e) {
                     e.printStackTrace();
                     return;
                 } finally {
                     auline.drain();
                     auline.close();
                 }

             }
         }
     }


    /Alarm/src/com/b510/ui/main/MyTray.java

     

     

     


    package com.b510.ui.main;

     import java.awt.AWTException;
     import java.awt.Image;
     import java.awt.MenuItem;
     import java.awt.PopupMenu;
     import java.awt.SystemTray;
     import java.awt.TrayIcon;
     import java.awt.event.ActionEvent;
     import java.awt.event.ActionListener;
     import java.awt.event.MouseEvent;
     import java.awt.event.MouseListener;

     import javax.swing.ImageIcon;
     import javax.swing.JFrame;

     import com.b510.menu.AlarmFile;
     import com.b510.menu.AlarmTools;

     public class MyTray implements ActionListener, MouseListener {
         private Image icon;// 图标
         private TrayIcon trayIcon;
         private SystemTray systemTray;// 系统托盘

         private MyAlarm myAlarm; // 托盘所属主窗体
         private PopupMenu pop = new PopupMenu(); // 弹出菜单
         // 菜单选项
         /** 截图 */
         private MenuItem screenshot = new MenuItem("ScreenShot");
         /** 还原 */
         private MenuItem open = new MenuItem("Restore");
         /** 退出*/
         private MenuItem exit =new MenuItem("Exit");
         public MyTray(MyAlarm myAlarm) {
             this.myAlarm = myAlarm;
             // 得到托盘的图标
             icon = new ImageIcon(this.getClass().getClassLoader().getResource(
                     "image/mytray.png")).getImage();

             if (SystemTray.isSupported()) {
                 systemTray = SystemTray.getSystemTray();
                 // 设置鼠标经过图标时,显示的内容
                 trayIcon = new TrayIcon(icon, "open Alarm", pop);
                 pop.add(screenshot);
                 pop.add(open);
                 pop.add(exit);
                 // 添加系统托盘
                 try {
                     systemTray.add(trayIcon);
                 } catch (AWTException e1) {
                     e1.printStackTrace();
                     trayIcon.addMouseListener(this);
                 }
             }
             displayInfoListener();
             trayIcon.addMouseListener(this);
             exit.addActionListener(this);
             open.addActionListener(this);
             screenshot.addActionListener(this);
         }

         @Override
         public void actionPerformed(ActionEvent e) {
             if(e.getSource()==exit){
                 //退出系统
                 AlarmFile.exit();
             }else if (e.getSource() == open) {
                 // 单点击菜单中的"还原"选项时,还原窗口
                 //displayInfo();
                 //trayIcon.displayMessage("温馨提示", "hello,world", TrayIcon.MessageType.INFO);
                 myAlarm.iconed = false;
                 friendListSet(true);

             } else if (e.getSource() == screenshot) {
                 // 但点击“截图”选项时,进行截图
                 AlarmTools.screenshot();
             }
         }

         @Override
         public void mouseClicked(MouseEvent e) {
             // 但鼠标点击一次的时候,进行弹出窗口
             if (e.getClickCount() == 1 && e.getButton() != MouseEvent.BUTTON3) {
                 if (!myAlarm.isVisible()) {
                     friendListSet(true);
                 } else {
                     friendListSet(false);
                 }
             }
             // 但鼠标点击两次的时候,进行弹出窗口
             // 如果窗口有显示,则隐藏窗口,否则显示窗口
             if (e.getClickCount() == 2 && e.getButton() != MouseEvent.BUTTON3) {
                 if (!myAlarm.isVisible()) {
                     friendListSet(true);
                 } else {
                     friendListSet(false);
                 }
             }
         }

         /**
          * 设置friendList的可见性
          */
         private void friendListSet(boolean flag) {
             myAlarm.setVisible(true);
             myAlarm.setExtendedState(JFrame.NORMAL);
         }

        
         public void displayInfoListener() {
             new Thread(new Runnable() {// 设置一个线程
                         public void run() {
                             while (true) {
                                 try {
                                     Thread.sleep(1000);
                                 } catch (Exception e) {
                                     e.printStackTrace();
                                 }
                                 if(myAlarm.getSecondOfResult()==30){
                                      trayIcon.displayMessage("温馨提示","距闹钟设置时间还剩:\n00 时 00 分 " +myAlarm.getSecondOfResult() + " 秒",TrayIcon.MessageType.INFO);
                                      myAlarm.setSecondOfResult(29);
                                 }
                                 if(myAlarm.getFlagOfBackground()){
                                     trayIcon.displayMessage("温馨提示","您已经更改了背景,点击托盘图标\n可以看到效果,右键托盘图标\n可以进行功能选择。",TrayIcon.MessageType.INFO);
                                     myAlarm.setFlagOfBackground(false);
                                 }
                             }
                         }
                     }).start();
         }

         @Override
         public void mouseEntered(MouseEvent arg0) {
             // TODO Auto-generated method stub

         }

         @Override
         public void mouseExited(MouseEvent arg0) {
             // TODO Auto-generated method stub

         }

         @Override
         public void mousePressed(MouseEvent arg0) {
             // TODO Auto-generated method stub

         }

         @Override
         public void mouseReleased(MouseEvent arg0) {
             // TODO Auto-generated method stub

         }
     }


    /Alarm/src/com/b510/ui/up/AlarmShowNow.java
    package com.b510.ui.up;

     import java.util.Calendar;

     /**
      *
      * @author Hongten
      * 主界面区-显示现在的时间
      */
     public class AlarmShowNow {

         /**
          * 获得系统的当前时间,并返回
          * @return 返回系统当前时间
          */
         public static String now() {
             Calendar cal = Calendar.getInstance();
             int hour = cal.get(Calendar.HOUR_OF_DAY);
             int min = cal.get(Calendar.MINUTE);
             int sec = cal.get(Calendar.SECOND);
             String current = new String(hour+ ":" + min + ":" + sec);
             return current;
         }
        public static void main(String args[]){
            System.out.println(now());
        }
     }

           以上就是闹钟的实现代码,希望大家喜欢,更多相关内容请继续关注爱站技术频道。

    上一篇:Java事务的解析

    下一篇:java Collection 之Set使用说明

    您可能感兴趣的文章

    相关阅读

    热门软件源码

    最新软件源码下载