java開發_鬧鍾


項目結構:

效果圖一:

效果圖二:

效果圖三:

關於鬧鍾:

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

 

 1 Alarm hongten v1.0
 2 
 3 使用說明:
 4 
 5     此文件夾中包括:
 6             src文件夾       存放圖片和聲音文件夾,使用程序的時候,請不要刪除此文件夾
 7             alarm.jar    主要的功能程序jar包(要求安裝jdk1.5或以上版本)
 8             ReadMe.txt     說明文檔
 9     
10 
11 
12  1.菜單功能說明
13 
14     文件—— >    
15           上傳鈴聲(還沒實現)
16           退出                     功能:退出程序
17     工具—— >    
18           截圖工具              功能:進行截圖
19           更改背景        功能:更改背景
20     幫助—— >    
21           關於程序        功能:查看程序說明
22 
23 
24  2.設置鬧表
25 
26     2.1.可以在響鈴時間里設置鬧表時間
27 
28     2.2.還可以用快速設置,+1表示在系統時間的基礎上增加一分鍾,-1表示在系統時間的基礎上減去一分鍾                (+5,+30,-5,-30同樣原理)
29 
30  3.提示鈴聲
31 
32     可以選擇鈴聲類型,進行試聽,試聽是時候可以停止(該功能還沒完善)
33 
34  4.系統托盤
35 
36     4.1.在主窗體隱藏的時候,單擊左鍵,主窗體可以顯示出來;
37     
38     4.2.在主窗體隱藏的時候,單擊右鍵,可以進行功能選擇:退出,恢復主窗體可見,截圖
39 
40     4.3.在設置的鬧表時間剩下30秒是時候,系統會自動提示信息(電腦右下角會彈出消息提示框)
41 
42     4.4.在設置更改背景的時候,同樣會彈出提示信息(所有的提示信息會自動隱退)
43 
44  5.更多信息
45 
46     更多信息請訪問:http://www.cnblogs.com/hongten
47     QQ:648719819
48     制作:hongten
49 
50 
51 
52 
53           

 

 

 

 

 

下面是代碼部分

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

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

 1 package com.b510.main;
 2 
 3 import com.b510.ui.main.MyAlarm;
 4 
 5 /**
 6  * 
 7  * @author Hongten
 8  */
 9 public class AlarmMain {
10 
11     public static void main(String[] args) {
12         new MyAlarm("Alarm hongten v-1.0");
13     }
14 }

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

 1 package com.b510.menu;
 2 
 3 /**
 4  * @author Hongten
 5  *
 6  * @time 2012-3-4  2012
 7  */
 8 public class AlarmBackground {
 9 
10     /**
11      * 默認的背景圖片
12      */
13     private static String BACKGROUND_DEFAULT="src/image/background.png";
14     /**
15      * 水果系列
16      */
17     private static String BACKGROUND_FRUIT="src/image/background1.png";
18     /**
19      * 自行車系列
20      */
21     private static String BACKGROUND_BICK="src/image/background2.png";
22     /**
23      * 背景圖片的list
24      */
25     private static String[] backgroundList={BACKGROUND_DEFAULT,BACKGROUND_FRUIT,BACKGROUND_BICK};
26     /**
27      * 獲取背景圖片的list的長度
28      * @return 返回背景圖片的list的長度
29      */
30     public static int getBackgroundListSize(){
31         return backgroundList.length;
32     }
33     public static String getBackground(int bg){
34         return backgroundList[bg];
35     }
36 }

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

 1 package com.b510.menu;
 2 
 3 /**
 4  *
 5  * @author Hongten
 6  * 菜單-文件
 7  */
 8 public class AlarmFile {
 9 
10     /**
11      * 退出程序
12      */
13     public static void exit(){
14         System.exit(0);
15     }
16 }

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

  1 /*
  2  * To change this template, choose Tools | Templates
  3  * and open the template in the editor.
  4  */
  5 package com.b510.menu;
  6 
  7 import java.awt.event.MouseAdapter;
  8 import java.awt.event.MouseEvent;
  9 import java.awt.event.WindowAdapter;
 10 import java.awt.event.WindowEvent;
 11 
 12 import javax.swing.ImageIcon;
 13 import javax.swing.JButton;
 14 import javax.swing.JFrame;
 15 
 16 import com.b510.ui.main.MyAlarm;
 17 
 18 /**
 19  * 
 20  * @author Hongten 菜單-幫助
 21  */
 22 public class AlarmHelp extends JFrame {
 23 
 24     /**
 25      * 版本號
 26      */
 27     private static final long serialVersionUID = 5248482602468160509L;
 28 
 29     public AlarmHelp(String title) {
 30         super(title);
 31         initComponents();
 32         addWindowListener(new WindowAdapter() {
 33             public void windowClosing(WindowEvent e) {
 34                 AlarmHelp.this.setVisible(false);
 35         }});
 36     }
 37 
 38     /**
 39      *關閉按鈕
 40      */
 41     private JButton closeButton = new JButton();
 42     /**
 43      * 應用程序名稱
 44      */
 45     javax.swing.JLabel appTitleLabel = new javax.swing.JLabel();
 46     /**
 47      * 版本號 前
 48      */
 49     javax.swing.JLabel versionLabel = new javax.swing.JLabel();
 50     /**
 51      * 版本號
 52      */
 53     javax.swing.JLabel appVersionLabel = new javax.swing.JLabel();
 54     /**
 55      * 主頁 前
 56      */
 57     javax.swing.JLabel homepageLabel = new javax.swing.JLabel();
 58     /**
 59      * Homepage
 60      */
 61     javax.swing.JLabel appHomepageLabel = new javax.swing.JLabel();
 62     /**
 63      * 說明
 64      */
 65     javax.swing.JLabel appDescLabel = new javax.swing.JLabel();
 66     /**
 67      * 圖片
 68      */
 69     javax.swing.JLabel imageLabel = new javax.swing.JLabel();
 70 
 71 
 72     private void initComponents() {
 73 
 74         this.setVisible(true);
 75         // 設置大小不能變
 76         setResizable(false);
 77         this.setLocation(MyAlarm.pointX+60, MyAlarm.pointY+190);// 設置窗體的初始位置
 78         
 79         closeButton.addMouseListener(new MouseAdapter() {
 80              public void mouseEntered(java.awt.event.MouseEvent evt) {
 81                    closeButton.setIcon(new ImageIcon("src/image/biao2.png"));
 82                 }
 83              public void mouseExited(MouseEvent evt){
 84                  closeButton.setIcon(new ImageIcon("src/image/biao.png"));
 85              }
 86         });
 87 
 88         appTitleLabel.setFont(appTitleLabel.getFont().deriveFont(
 89                 appTitleLabel.getFont().getStyle() | java.awt.Font.BOLD,
 90                 appTitleLabel.getFont().getSize() + 4));
 91         appTitleLabel.setText("應用程序名稱:"); // NOI18N
 92         appTitleLabel.setName("appTitleLabel"); // NOI18N
 93 
 94         versionLabel.setFont(versionLabel.getFont().deriveFont(
 95                 versionLabel.getFont().getStyle() | java.awt.Font.BOLD));
 96         versionLabel.setText("版本號:"); // NOI18N
 97         versionLabel.setName("versionLabel"); // NOI18N
 98 
 99         appVersionLabel.setText("1.0"); // NOI18N
100         appVersionLabel.setName("appVersionLabel"); // NOI18N
101 
102         homepageLabel.setFont(homepageLabel.getFont().deriveFont(
103                 homepageLabel.getFont().getStyle() | java.awt.Font.BOLD));
104         homepageLabel.setText("主頁:"); // NOI18N
105         homepageLabel.setName("homepageLabel"); // NOI18N
106 
107         appHomepageLabel.setText("http://www.cnblogs.com/hongten"); // NOI18N
108         appHomepageLabel.setName("appHomepageLabel"); // NOI18N
109 
110         appDescLabel.setText("這是一個小應用程序,鬧鍾"); // NOI18N
111         appDescLabel.setName("appDescLabel"); // NOI18N
112 
113         imageLabel
114                 .setIcon(new ImageIcon("src/image/about.png")); // NOI18N
115         imageLabel.setName("imageLabel"); // NOI18N
116 
117         closeButton
118                 .setIcon(new ImageIcon("src/image/biao.png"));
119         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
120                 getContentPane());
121         getContentPane().setLayout(layout);
122         layout
123                 .setHorizontalGroup(layout
124                         .createParallelGroup(
125                                 javax.swing.GroupLayout.Alignment.LEADING)
126                         .addGroup(
127                                 layout
128                                         .createSequentialGroup()
129                                         .addComponent(imageLabel)
130                                         .addGap(18, 18, 18)
131                                         .addGroup(
132                                                 layout
133                                                         .createParallelGroup(
134                                                                 javax.swing.GroupLayout.Alignment.TRAILING)
135                                                         .addGroup(
136                                                                 javax.swing.GroupLayout.Alignment.LEADING,
137                                                                 layout
138                                                                         .createSequentialGroup()
139                                                                         .addGroup(
140                                                                                 layout
141                                                                                         .createParallelGroup(
142                                                                                                 javax.swing.GroupLayout.Alignment.LEADING)
143                                                                                         .addComponent(
144                                                                                                 versionLabel)
145                                                                                         .addComponent(
146                                                                                                 homepageLabel))
147                                                                         .addPreferredGap(
148                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
149                                                                         .addGroup(
150                                                                                 layout
151                                                                                         .createParallelGroup(
152                                                                                                 javax.swing.GroupLayout.Alignment.LEADING)
153                                                                                         .addComponent(
154                                                                                                 appVersionLabel)
155                                                                                         .addComponent(
156                                                                                                 appHomepageLabel)))
157                                                         .addComponent(
158                                                                 appTitleLabel,
159                                                                 javax.swing.GroupLayout.Alignment.LEADING)
160                                                         .addComponent(
161                                                                 appDescLabel,
162                                                                 javax.swing.GroupLayout.Alignment.LEADING,
163                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
164                                                                 266,
165                                                                 Short.MAX_VALUE)
166                                                         .addComponent(
167                                                                 closeButton))
168                                         .addContainerGap()));
169         layout
170                 .setVerticalGroup(layout
171                         .createParallelGroup(
172                                 javax.swing.GroupLayout.Alignment.LEADING)
173                         .addComponent(imageLabel,
174                                 javax.swing.GroupLayout.PREFERRED_SIZE,
175                                 javax.swing.GroupLayout.DEFAULT_SIZE,
176                                 Short.MAX_VALUE)
177                         .addGroup(
178                                 layout
179                                         .createSequentialGroup()
180                                         .addContainerGap()
181                                         .addComponent(appTitleLabel)
182                                         .addPreferredGap(
183                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
184                                         .addComponent(
185                                                 appDescLabel,
186                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
187                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
188                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
189                                         .addPreferredGap(
190                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
191                                         .addGroup(
192                                                 layout
193                                                         .createParallelGroup(
194                                                                 javax.swing.GroupLayout.Alignment.BASELINE)
195                                                         .addComponent(
196                                                                 versionLabel)
197                                                         .addComponent(
198                                                                 appVersionLabel))
199                                         .addPreferredGap(
200                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
201                                         .addGroup(
202                                                 layout
203                                                         .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE))
204                                         .addPreferredGap(
205                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
206                                         .addGroup(
207                                                 layout
208                                                         .createParallelGroup(
209                                                                 javax.swing.GroupLayout.Alignment.BASELINE)
210                                                         .addComponent(
211                                                                 homepageLabel)
212                                                         .addComponent(
213                                                                 appHomepageLabel))
214                                         .addPreferredGap(
215                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED,
216                                                 19, Short.MAX_VALUE)
217                                         .addComponent(closeButton)
218                                         .addContainerGap()));
219         pack();
220     }
221 }

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

 1 /*
 2  * To change this template, choose Tools | Templates
 3  * and open the template in the editor.
 4  */
 5 
 6 package com.b510.menu;
 7 
 8 import mai.capture.DesktopCapture;
 9 
10 /**
11  *
12  * @author Hongten
13  * 菜單-工具
14  */
15 public class AlarmTools {
16 
17     /** 定義截圖功能 */
18     @SuppressWarnings("unused")
19     private static DesktopCapture deskTopCapture;
20     /**
21      * 截圖
22      */
23     public static void screenshot(){
24         deskTopCapture=new DesktopCapture();
25     }
26 }

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

   1 package com.b510.ui.main;
   2 
   3 import java.awt.Color;
   4 import java.awt.Graphics;
   5 import java.awt.Point;
   6 import java.awt.event.ActionEvent;
   7 import java.awt.event.ActionListener;
   8 import java.awt.event.WindowAdapter;
   9 import java.awt.event.WindowEvent;
  10 import java.io.File;
  11 import java.io.IOException;
  12 import java.util.Calendar;
  13 
  14 import javax.sound.sampled.AudioFormat;
  15 import javax.sound.sampled.AudioInputStream;
  16 import javax.sound.sampled.AudioSystem;
  17 import javax.sound.sampled.DataLine;
  18 import javax.sound.sampled.SourceDataLine;
  19 import javax.swing.ImageIcon;
  20 import javax.swing.JFrame;
  21 import javax.swing.JLabel;
  22 import javax.swing.JOptionPane;
  23 import javax.swing.JPanel;
  24 
  25 import com.b510.menu.AlarmBackground;
  26 import com.b510.menu.AlarmFile;
  27 import com.b510.menu.AlarmHelp;
  28 import com.b510.menu.AlarmTools;
  29 
  30 /**
  31  * 
  32  * @author Hongten
  33  */
  34 public class MyAlarm extends JFrame implements ActionListener {
  35 
  36     /**
  37      *+1
  38      */
  39     private javax.swing.JButton add1;
  40     /**
  41      * +30
  42      */
  43     private javax.swing.JButton add30;
  44     /**
  45      * +5
  46      */
  47     private javax.swing.JButton add5;
  48     /**
  49      * 時
  50      */
  51     private javax.swing.JComboBox dorp_down_hours;
  52     /**
  53      * 分
  54      */
  55     private javax.swing.JComboBox dorp_down_minute;
  56     /**
  57      * 秒
  58      */
  59     private javax.swing.JComboBox drop_down_second;
  60     /**
  61      * 試聽
  62      */
  63     private javax.swing.JButton listening_test;
  64     /**
  65      * 試聽-停止
  66      */
  67     private javax.swing.JButton listening_test_stop;
  68     /**
  69      * 主Panel
  70      */
  71     private javax.swing.JPanel mainPanel;
  72     /**
  73      * 菜單bar
  74      */
  75     private javax.swing.JMenuBar menuBar;
  76     /**
  77      * -1
  78      */
  79     private javax.swing.JButton minus1;
  80     /**
  81      * -30
  82      */
  83     private javax.swing.JButton minus30;
  84     /**
  85      * -5
  86      */
  87     private javax.swing.JButton minus5;
  88     /**
  89      * 當前時間
  90      */
  91     private javax.swing.JLabel now;
  92     /**
  93      * 鈴聲
  94      */
  95     private javax.swing.JComboBox ring_setup;
  96     /**
  97      * 截屏
  98      */
  99     private javax.swing.JMenuItem screenshot;
 100     /**
 101      * 更改背景圖片
 102      */
 103     private javax.swing.JMenuItem changeBackground;
 104     /**
 105      * 停止
 106      */
 107     private javax.swing.JButton stop;
 108     /**
 109      * 工具
 110      */
 111     private javax.swing.JMenu tools;
 112     /**
 113      * 幫助
 114      */
 115     private javax.swing.JMenu help;
 116     /**
 117      * 關於
 118      */
 119     private javax.swing.JMenuItem about;
 120     /**
 121      * 退出
 122      */
 123     private javax.swing.JMenuItem exit;
 124     /**
 125      * 上傳鈴聲
 126      */
 127     private javax.swing.JMenuItem uploadRing;
 128     /**
 129      * wenjian
 130      */
 131     private javax.swing.JMenu file;
 132     /**
 133      * 結果,即剩余時間
 134      */
 135     private JLabel result;
 136     /**
 137      * 分割線
 138      */
 139     private javax.swing.JSeparator line;
 140     /**
 141      * 變量-->小時
 142      */
 143     private String h;
 144     /**
 145      * 變量-->分鍾
 146      */
 147     private String m;
 148     /**
 149      * 變量-->秒
 150      */
 151     private String s;
 152     /**
 153      * 線程的一個標志
 154      */
 155     private boolean running = true;
 156     /**
 157      * 定義圖盤圖盤標志
 158      */
 159     public boolean iconed = false;
 160     /**
 161      * 背景圖片選擇標志
 162      */
 163     private int background = 0;
 164     /**
 165      * 獲取result的秒數
 166      */
 167     private int secondOfResult;
 168     /**
 169      * 更改背景圖片的標志
 170      */
 171     private boolean flagOfBackground = false;
 172     /**
 173      * MyAlarm的X坐標
 174      */
 175     public static int pointX = 0;
 176     /**
 177      * MyAlarm的Y坐標
 178      */
 179     public static int pointY = 0;
 180     /** 定義托盤 */
 181     MyTray myTray;
 182     /**
 183      * 版本號
 184      */
 185     private static final long serialVersionUID = -6601825053136983041L;
 186 
 187     public MyAlarm(String title) {
 188         this.setTitle(title);
 189         init();
 190     }
 191 
 192     /**
 193      * 初始化背景圖片
 194      */
 195     public void initMainPanel() {
 196         mainPanel = new JPanel() {
 197             private static final long serialVersionUID = 1L;
 198 
 199             protected void paintComponent(Graphics g) {
 200                 ImageIcon icon = new ImageIcon(AlarmBackground
 201                         .getBackground(background));
 202                 g.drawImage(icon.getImage(), 0, 0, 545, 463, null);
 203             }
 204         };
 205     }
 206 
 207     /**
 208      * 主界面初始化
 209      */
 210     public void init() {
 211         initMainPanel();
 212         now = new javax.swing.JLabel();
 213         stop = new javax.swing.JButton();
 214         add1 = new javax.swing.JButton();
 215         add5 = new javax.swing.JButton();
 216         add30 = new javax.swing.JButton();
 217         minus1 = new javax.swing.JButton();
 218         minus5 = new javax.swing.JButton();
 219         minus30 = new javax.swing.JButton();
 220         listening_test = new javax.swing.JButton();
 221         listening_test_stop = new javax.swing.JButton();
 222         dorp_down_hours = new javax.swing.JComboBox();
 223         dorp_down_minute = new javax.swing.JComboBox();
 224         drop_down_second = new javax.swing.JComboBox();
 225         ring_setup = new javax.swing.JComboBox();
 226         menuBar = new javax.swing.JMenuBar();
 227         file = new javax.swing.JMenu();
 228         uploadRing = new javax.swing.JMenuItem();
 229         exit = new javax.swing.JMenuItem();
 230         tools = new javax.swing.JMenu();
 231         screenshot = new javax.swing.JMenuItem();
 232         changeBackground = new javax.swing.JMenuItem();
 233         help = new javax.swing.JMenu();
 234         about = new javax.swing.JMenuItem();
 235         line = new javax.swing.JSeparator();
 236         result = new javax.swing.JLabel();
 237 
 238         mainPanel.setName("mainPanel"); // NOI18N
 239 
 240         result.setForeground(Color.RED);
 241         result.setName("result");
 242         result.setVisible(false);
 243 
 244         now.setFont(now.getFont().deriveFont(
 245                 now.getFont().getStyle() | java.awt.Font.BOLD,
 246                 now.getFont().getSize() + 70));
 247         now.setName("now"); // NOI18N
 248         // 時間監聽器,得到系統時間和設置好時間后,得到剩余時間
 249         timeListener();
 250 
 251         stop.setText("stop"); // NOI18N
 252         stop.setName("stop"); // NOI18N
 253         // 初始化的時候是不可見的
 254         stop.setVisible(false);
 255         stop.addActionListener(this);
 256 
 257         add1.setText("+1"); // NOI18N
 258         add1.setName("add1"); // NOI18N
 259         add1.addActionListener(this);
 260 
 261         add5.setText("+5"); // NOI18N
 262         add5.setName("add5"); // NOI18N
 263         add5.addActionListener(this);
 264 
 265         add30.setText("+30"); // NOI18N
 266         add30.setName("add30"); // NOI18N
 267         add30.addActionListener(this);
 268 
 269         minus1.setText("-1"); // NOI18N
 270         minus1.setName("minus1"); // NOI18N
 271         minus1.addActionListener(this);
 272 
 273         minus5.setText("-5"); // NOI18N
 274         minus5.setName("minus5"); // NOI18N
 275         minus5.addActionListener(this);
 276 
 277         minus30.setText("-30"); // NOI18N
 278         minus30.setName("minus30"); // NOI18N
 279         minus30.addActionListener(this);
 280 
 281         listening_test.setText("試聽"); // NOI18N
 282         listening_test.setName("listening_test"); // NOI18N
 283         listening_test.addActionListener(this);
 284 
 285         listening_test_stop.setText("停止"); // NOI18N
 286         listening_test_stop.setName("listening_test_stop"); // NOI18N
 287         listening_test_stop.addActionListener(this);
 288 
 289         dorp_down_hours.setModel(new javax.swing.DefaultComboBoxModel(
 290                 new String[] { "關閉", "00", "01", "02", "03", "04", "05", "06",
 291                         "07", "08", "09", "10", "11", "12", "13", "14", "15",
 292                         "16", "17", "18", "19", "20", "21", "22", "23" }));
 293         dorp_down_hours.setName("dorp_down_hours"); // NOI18N
 294         dorp_down_hours.addActionListener(this);
 295 
 296         dorp_down_minute.setModel(new javax.swing.DefaultComboBoxModel(
 297                 new String[] { "關閉", "00", "01", "02", "03", "04", "05", "06",
 298                         "07", "08", "09", "10", "11", "12", "13", "14", "15",
 299                         "16", "17", "18", "19", "20", "21", "22", "23", "24",
 300                         "25", "26", "27", "28", "29", "30", "31", "32", "33",
 301                         "34", "35", "36", "37", "38", "39", "40", "41", "42",
 302                         "43", "44", "45", "46", "47", "48", "49", "50", "51",
 303                         "52", "53", "54", "55", "56", "57", "58", "59" }));
 304         dorp_down_minute.setName("dorp_down_minute"); // NOI18N
 305         dorp_down_minute.addActionListener(this);
 306 
 307         drop_down_second.setModel(new javax.swing.DefaultComboBoxModel(
 308                 new String[] { "關閉", "00", "01", "02", "03", "04", "05", "06",
 309                         "07", "08", "09", "10", "11", "12", "13", "14", "15",
 310                         "16", "17", "18", "19", "20", "21", "22", "23", "24",
 311                         "25", "26", "27", "28", "29", "30", "31", "32", "33",
 312                         "34", "35", "36", "37", "38", "39", "40", "41", "42",
 313                         "43", "44", "45", "46", "47", "48", "49", "50", "51",
 314                         "52", "53", "54", "55", "56", "57", "58", "59" }));
 315         drop_down_second.setName("drop_down_second"); // NOI18N
 316         drop_down_second.addActionListener(this);
 317 
 318         ring_setup.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
 319                 "鈴聲一", "鈴聲二", "鈴聲三", "鈴聲四", "鈴聲五", "鈴聲六", "鈴聲七" }));
 320         ring_setup.setName("ring_setup"); // NOI18N
 321         // 主要的布局
 322         omponentLayout();
 323 
 324         menuBar.setName("menuBar"); // NOI18N
 325 
 326         file.setText("文件"); // NOI18N
 327         file.setName("file"); // NOI18N
 328 
 329         uploadRing.setText("上傳鈴聲"); // NOI18N
 330         uploadRing.setName("uploadRing"); // NOI18N
 331         uploadRing.addActionListener(this);
 332         file.add(uploadRing);
 333 
 334         file.add(line);
 335 
 336         exit.setText("退出");
 337         exit.setName("exit"); // NOI18N
 338         exit.addActionListener(this);
 339         file.add(exit);
 340 
 341         menuBar.add(file);
 342 
 343         tools.setText("工具"); // NOI18N
 344         tools.setName("tools"); // NOI18N
 345 
 346         screenshot.setText("截圖工具"); // NOI18N
 347         screenshot.setName("screenshot"); // NOI18N
 348         screenshot.addActionListener(this);
 349         tools.add(screenshot);
 350 
 351         changeBackground.setText("更改背景");
 352         changeBackground.setName("changeBackground");
 353         changeBackground.addActionListener(this);
 354         tools.add(changeBackground);
 355 
 356         menuBar.add(tools);
 357 
 358         help.setText("幫助"); // NOI18N
 359         help.setName("help"); // NOI18N
 360 
 361         about.setText("關於軟件");
 362         about.setName("about"); // NOI18N
 363         about.addActionListener(this);
 364         help.add(about);
 365 
 366         menuBar.add(help);
 367 
 368         this.add(mainPanel);
 369         setJMenuBar(menuBar);
 370 
 371         this.setVisible(true);
 372         this.setSize(550, 516);
 373         // this.pack();
 374         this.setLocationRelativeTo(null);
 375         this.setResizable(false);
 376         // this.setLocation(470, 250);
 377         // this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 378         // 當點擊"-"最小化按鈕時,系統會最小化到托盤
 379         addWindowListener(new WindowAdapter() {
 380             public void windowIconified(WindowEvent e) {
 381                 iconed = true;
 382                 setVisible(false);
 383             }
 384 
 385             public void windowClosing(WindowEvent e) {
 386                 // 當點擊"X"關閉窗口按鈕時,會詢問用戶是否要最小化到托盤
 387                 // 是,表示最小化到托盤,否,表示退出
 388                 int option = JOptionPane.showConfirmDialog(MyAlarm.this,
 389                         "是否最小化到托盤?", "提示:", JOptionPane.YES_NO_OPTION);
 390                 if (option == JOptionPane.YES_OPTION) {
 391                     iconed = true;
 392                     MyAlarm.this.setVisible(false);
 393                 } else {
 394                     AlarmFile.exit();
 395                 }
 396             }
 397         });
 398         // 初始化自定義托盤
 399         myTray = new MyTray(MyAlarm.this);
 400 
 401     }
 402 
 403     /**
 404      * 組件的布局,不要輕易動啊
 405      */
 406     private void omponentLayout() {
 407         javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(
 408                 mainPanel);
 409         mainPanel.setLayout(mainPanelLayout);
 410         mainPanelLayout
 411                 .setHorizontalGroup(mainPanelLayout
 412                         .createParallelGroup(
 413                                 javax.swing.GroupLayout.Alignment.LEADING)
 414                         .addGroup(
 415                                 javax.swing.GroupLayout.Alignment.TRAILING,
 416                                 mainPanelLayout
 417                                         .createSequentialGroup()
 418                                         .addContainerGap(170, Short.MAX_VALUE)
 419                                         .addGroup(
 420                                                 mainPanelLayout
 421                                                         .createParallelGroup(
 422                                                                 javax.swing.GroupLayout.Alignment.LEADING)
 423                                                         .addGroup(
 424                                                                 mainPanelLayout
 425                                                                         .createSequentialGroup()
 426                                                                         .addComponent(
 427                                                                                 result)
 428                                                                         .addContainerGap())
 429                                                         .addGroup(
 430                                                                 javax.swing.GroupLayout.Alignment.TRAILING,
 431                                                                 mainPanelLayout
 432                                                                         .createParallelGroup(
 433                                                                                 javax.swing.GroupLayout.Alignment.LEADING)
 434                                                                         .addGroup(
 435                                                                                 mainPanelLayout
 436                                                                                         .createSequentialGroup()
 437                                                                                         .addComponent(
 438                                                                                                 now,
 439                                                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
 440                                                                                                 483,
 441                                                                                                 Short.MAX_VALUE)
 442                                                                                         .addContainerGap())
 443                                                                         .addGroup(
 444                                                                                 javax.swing.GroupLayout.Alignment.TRAILING,
 445                                                                                 mainPanelLayout
 446                                                                                         .createSequentialGroup()
 447                                                                                         .addGroup(
 448                                                                                                 mainPanelLayout
 449                                                                                                         .createParallelGroup(
 450                                                                                                                 javax.swing.GroupLayout.Alignment.LEADING)
 451                                                                                                         .addGroup(
 452                                                                                                                 mainPanelLayout
 453                                                                                                                         .createSequentialGroup()
 454                                                                                                                         .addComponent(
 455                                                                                                                                 dorp_down_hours,
 456                                                                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
 457                                                                                                                                 74,
 458                                                                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
 459                                                                                                                         .addPreferredGap(
 460                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 461                                                                                                                         .addComponent(
 462                                                                                                                                 dorp_down_minute,
 463                                                                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
 464                                                                                                                                 65,
 465                                                                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
 466                                                                                                                         .addPreferredGap(
 467                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 468                                                                                                                         .addComponent(
 469                                                                                                                                 drop_down_second,
 470                                                                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
 471                                                                                                                                 62,
 472                                                                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
 473                                                                                                                         .addPreferredGap(
 474                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 475                                                                                                                         .addComponent(
 476                                                                                                                                 stop))
 477                                                                                                         .addGroup(
 478                                                                                                                 mainPanelLayout
 479                                                                                                                         .createSequentialGroup()
 480                                                                                                                         .addComponent(
 481                                                                                                                                 add1)
 482                                                                                                                         .addPreferredGap(
 483                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 484                                                                                                                         .addComponent(
 485                                                                                                                                 add5)
 486                                                                                                                         .addPreferredGap(
 487                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 488                                                                                                                         .addComponent(
 489                                                                                                                                 add30)
 490                                                                                                                         .addPreferredGap(
 491                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 492                                                                                                                         .addComponent(
 493                                                                                                                                 minus1)
 494                                                                                                                         .addPreferredGap(
 495                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 496                                                                                                                         .addComponent(
 497                                                                                                                                 minus5)
 498                                                                                                                         .addPreferredGap(
 499                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 500                                                                                                                         .addComponent(
 501                                                                                                                                 minus30))
 502                                                                                                         .addGroup(
 503                                                                                                                 mainPanelLayout
 504                                                                                                                         .createSequentialGroup()
 505                                                                                                                         .addComponent(
 506                                                                                                                                 ring_setup,
 507                                                                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
 508                                                                                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
 509                                                                                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
 510                                                                                                                         .addPreferredGap(
 511                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 512                                                                                                                         .addComponent(
 513                                                                                                                                 listening_test)
 514                                                                                                                         .addPreferredGap(
 515                                                                                                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 516                                                                                                                         .addComponent(
 517                                                                                                                                 listening_test_stop)))
 518                                                                                         .addGap(
 519                                                                                                 73,
 520                                                                                                 73,
 521                                                                                                 73))))));
 522         mainPanelLayout
 523                 .setVerticalGroup(mainPanelLayout
 524                         .createParallelGroup(
 525                                 javax.swing.GroupLayout.Alignment.LEADING)
 526                         .addGroup(
 527                                 javax.swing.GroupLayout.Alignment.TRAILING,
 528                                 mainPanelLayout
 529                                         .createSequentialGroup()
 530                                         .addGap(120, 120, 120)
 531                                         .addComponent(result)
 532                                         .addGap(24, 24, 24)
 533                                         .addComponent(
 534                                                 now,
 535                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
 536                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
 537                                                 Short.MAX_VALUE)
 538                                         .addGap(36, 36, 36)
 539                                         .addGroup(
 540                                                 mainPanelLayout
 541                                                         .createParallelGroup(
 542                                                                 javax.swing.GroupLayout.Alignment.BASELINE)
 543                                                         .addComponent(
 544                                                                 dorp_down_hours,
 545                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
 546                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
 547                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
 548                                                         .addComponent(
 549                                                                 dorp_down_minute,
 550                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
 551                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
 552                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
 553                                                         .addComponent(
 554                                                                 drop_down_second,
 555                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
 556                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
 557                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
 558                                                         .addComponent(stop))
 559                                         .addPreferredGap(
 560                                                 javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 561                                         .addGroup(
 562                                                 mainPanelLayout
 563                                                         .createParallelGroup(
 564                                                                 javax.swing.GroupLayout.Alignment.BASELINE)
 565                                                         .addComponent(add1)
 566                                                         .addComponent(add5)
 567                                                         .addComponent(add30)
 568                                                         .addComponent(minus1)
 569                                                         .addComponent(minus5)
 570                                                         .addComponent(minus30))
 571                                         .addGap(13, 13, 13)
 572                                         .addGroup(
 573                                                 mainPanelLayout
 574                                                         .createParallelGroup(
 575                                                                 javax.swing.GroupLayout.Alignment.BASELINE)
 576                                                         .addComponent(
 577                                                                 ring_setup,
 578                                                                 javax.swing.GroupLayout.PREFERRED_SIZE,
 579                                                                 javax.swing.GroupLayout.DEFAULT_SIZE,
 580                                                                 javax.swing.GroupLayout.PREFERRED_SIZE)
 581                                                         .addComponent(
 582                                                                 listening_test)
 583                                                         .addComponent(
 584                                                                 listening_test_stop))
 585                                         .addGap(58, 58, 58)));
 586     }
 587 
 588     /**
 589      * 時間監聽器,得到系統時間和設置好時間后,得到剩余時間
 590      */
 591     public void timeListener() {
 592         new Thread(new Runnable() {// 設置一個線程
 593                     public void run() {
 594                         while (true) {
 595                             try {
 596                                 Thread.sleep(1000);
 597                             } catch (Exception e) {
 598                                 e.printStackTrace();
 599                             }
 600                             initMainPanel();
 601                             now.setText(now());// 得到系統時間
 602                             result.setText(surplus_time());// 得到剩余時間
 603                         }
 604                     }
 605                 }).start();
 606     }
 607 
 608     /**
 609      * 播放聲音的監聽器
 610      */
 611     public void myListener() {
 612         new Thread(new Runnable() {// 設置一個線程
 613                     public void run() {
 614                         while (true) {
 615                             try {
 616                                 Thread.sleep(1000);
 617                             } catch (Exception e) {
 618                                 e.printStackTrace();
 619                             }
 620                             executeSound();// 播放聲音
 621                         }
 622                     }
 623                 }).start();
 624     }
 625 
 626     /**
 627      * 獲取返回結果
 628      * 
 629      * @return result值
 630      */
 631     public String surplus_time() {
 632         String r = null;
 633         // 獲取系統時,分,秒
 634         int h = getHour();
 635         int m = getMunite();
 636         int s = getSecond();
 637         // 獲取設置的響鈴時間
 638         int dh = 0;
 639         int dm = 0;
 640         int ds = 0;
 641         if (dorp_down_hours.getSelectedIndex() != 0) {
 642             dh = dorp_down_hours.getSelectedIndex() - 1;
 643         }
 644         if (dorp_down_minute.getSelectedIndex() != 0) {
 645             dm = dorp_down_minute.getSelectedIndex() - 1;
 646         }
 647         if (drop_down_second.getSelectedIndex() != 0) {
 648             ds = drop_down_second.getSelectedIndex() - 1;
 649         }
 650         int hour = dh - h;
 651         int min = dm - m;
 652         int sec = ds - s;
 653         if (hour == 0) {
 654             if (min == 0) {
 655                 if (sec == 0) {
 656                     r = "時間到了哦!";
 657                 }
 658                 if (sec < 0) {
 659                     hour += 23;
 660                     min += 59;
 661                     sec += 59;
 662                 }
 663             }
 664             if (min < 0) {
 665                 hour += 23;
 666                 if (sec < 0) {
 667                     min -= 1;
 668                     sec += 59;
 669                 }
 670                 min += 60;
 671             }
 672             if (min >= 0) {
 673                 if (sec < 0 || sec == 0) {
 674                     min -= 1;
 675                     sec += 59;
 676                 }
 677                 if (sec > 0) {
 678                     // sec=sec;
 679                 }
 680             }
 681         }
 682         if (hour < 0) {
 683             if (min <= 0) {
 684                 if (sec <= 0) {
 685                     hour -= 1;
 686                     min += 59;
 687                     sec += 59;
 688                 }
 689             }
 690             if (min > 0) {
 691                 if (sec <= 0) {
 692                     min -= 1;
 693                     sec += 59;
 694                 }
 695             }
 696             hour += 24;
 697         }
 698         if (hour > 0) {
 699             if (min == 0) {
 700                 if (sec <= 0) {
 701                     hour -= 1;
 702                     min += 59;
 703                     sec += 59;
 704                 }
 705             }
 706             if (min < 0) {
 707                 if (sec < 0) {
 708                     min -= 1;
 709                     sec += 59;
 710                 }
 711                 min += 60;
 712                 hour -= 1;
 713             }
 714             if (min > 0) {
 715                 if (sec < 0 || sec == 0) {
 716                     min -= 1;
 717                     sec += 59;
 718                 }
 719             }
 720         }
 721 
 722         if (sec == 30 && min == 0 && hour == 0) {
 723             setSecondOfResult(sec);
 724         }
 725         r = new String("剩:" + hour + "時" + min + "分" + sec + "秒");
 726         if (hour == 0 && min == 0 && sec < 0) {
 727             r = "時間到了哦!";
 728         }
 729         // result.setText(r);
 730         return r;
 731     }
 732 
 733     public int getSecondOfResult() {
 734         return secondOfResult;
 735     }
 736 
 737     public void setSecondOfResult(int sec) {
 738         this.secondOfResult = sec;
 739     }
 740 
 741     public boolean getFlagOfBackground() {
 742         return flagOfBackground;
 743     }
 744 
 745     public void setFlagOfBackground(boolean flag) {
 746         this.flagOfBackground = flag;
 747     }
 748 
 749     /**
 750      * 時間到了的時候就播放聲音
 751      */
 752     public void executeSound() {
 753         // 獲取系統時,分,秒
 754         int h = getHour();
 755         int m = getMunite();
 756         int s = getSecond();
 757         // 獲取設置的響鈴時間
 758         int dh = 0;
 759         int dm = 0;
 760         int ds = 0;
 761         if (dorp_down_hours.getSelectedIndex() != 0) {
 762             dh = dorp_down_hours.getSelectedIndex() - 1;
 763         }
 764         if (dorp_down_minute.getSelectedIndex() != 0) {
 765             dm = dorp_down_minute.getSelectedIndex() - 1;
 766         }
 767         if (drop_down_second.getSelectedIndex() != 0) {
 768             ds = drop_down_second.getSelectedIndex() - 1;
 769         }
 770         int hour = dh - h;
 771         int min = dm - m;
 772         int sec = ds - s;
 773         if (hour == 0 && min == 0 && sec == 0) {
 774             // 主窗體設置為可見
 775             setVisible(true);
 776             // 設置窗口前端顯示
 777             setExtendedState(JFrame.NORMAL);
 778             setAlwaysOnTop(true);
 779             // 播放聲音
 780             new Thread(new AlarmSound(ring_setup.getSelectedIndex())).start();
 781             // 震動
 782             shack();
 783         }
 784     }
 785 
 786     /**
 787      * 得到系統時間當前時間,並返回
 788      * 
 789      * @return 返回系統當前時間
 790      */
 791     public String now() {
 792         Calendar calendar = Calendar.getInstance();
 793         int hour = calendar.get(Calendar.HOUR_OF_DAY);
 794         int min = calendar.get(Calendar.MINUTE);
 795         int sec = calendar.get(Calendar.SECOND);
 796         // 小於10的時候,在前面加0
 797         String h = hour < 10 ? "0" : "";
 798         String m = min < 10 ? "0" : "";
 799         String s = sec < 10 ? "0" : "";
 800         String current = new String(h + hour + ":" + m + min + ":" + s + sec);
 801         return current;
 802     }
 803 
 804     /**
 805      * 布局下面的按鈕和下拉選項的監聽器
 806      */
 807     @SuppressWarnings("deprecation")
 808     @Override
 809     public void actionPerformed(ActionEvent e) {
 810 
 811         /**
 812          * 獲取dorp_down_hours,dorp_down_minute,drop_down_second的值
 813          */
 814         if (e.getSource() == dorp_down_hours) {
 815             valueJudgment();
 816         }
 817         if (e.getSource() == dorp_down_minute) {
 818             valueJudgment();
 819         }
 820         if (e.getSource() == drop_down_second) {
 821             valueJudgment();
 822         }
 823         if (e.getSource() == stop) {
 824             stopActionPerformed();
 825         }
 826         if (e.getSource() == add1) {
 827             add1Action();
 828             myListener();
 829         }
 830         if (e.getSource() == add5) {
 831             add5Action();
 832             myListener();
 833         }
 834         if (e.getSource() == add30) {
 835             add30Action();
 836             myListener();
 837         }
 838         if (e.getSource() == minus1) {
 839             minus1Action();
 840             myListener();
 841         }
 842         if (e.getSource() == minus5) {
 843             minus5Action();
 844             myListener();
 845         }
 846         if (e.getSource() == minus30) {
 847             minus30Action();
 848             myListener();
 849         }
 850         AlarmSound alarmSound = new AlarmSound(ring_setup.getSelectedIndex());
 851         Thread alarmThread = new Thread(alarmSound);
 852         if (e.getSource() == listening_test) {
 853             // 試聽
 854             if (!alarmThread.isAlive()) {
 855                 alarmThread.start();
 856             }
 857             if (running) {
 858                 alarmThread.resume();
 859             }
 860             listening_test.setEnabled(false);
 861         }
 862         if (e.getSource() == listening_test_stop) {
 863             // 停止試聽
 864             listening_test.setEnabled(true);
 865             if (running) {
 866                 alarmThread.suspend();
 867             }
 868             running = !running;
 869         }
 870         if (e.getSource() == uploadRing) {
 871             // 上傳文件
 872         }
 873         if (e.getSource() == exit) {
 874             // 退出程序
 875             AlarmFile.exit();
 876         }
 877         if (e.getSource() == screenshot) {
 878             // 主窗體隱藏
 879             iconed = true;
 880             MyAlarm.this.setVisible(false);
 881             // 開始截圖
 882             AlarmTools.screenshot();
 883         }
 884         if (e.getSource() == changeBackground) {
 885             changeBG();
 886         }
 887         if (e.getSource() == about) {
 888             pointX = getMyAlarmX();
 889             pointY = getMyAlarmY();
 890             new AlarmHelp("關於程序");
 891         }
 892     }
 893 
 894     /**
 895      * 更改背景圖片</br> 當點擊工具-- >更改背景,這里就可以處理更改背景圖片
 896      */
 897     private void changeBG() {
 898         setFlagOfBackground(true);
 899         if (background < AlarmBackground.getBackgroundListSize()) {
 900             iconed = true;
 901             setVisible(false);
 902             if (background != AlarmBackground.getBackgroundListSize() - 1) {
 903                 ++background;
 904             } else {
 905                 background = 0;
 906             }
 907             initMainPanel();
 908         }
 909     }
 910 
 911     /**
 912      * 點擊-30按鈕的時候執行的動作
 913      */
 914     private void minus30Action() {
 915         isHMSZero();
 916         if (dorp_down_minute.getSelectedIndex() - 30 < 0) {
 917             dorp_down_minute.setSelectedIndex(dorp_down_minute
 918                     .getSelectedIndex() + 30);// 60-30
 919             if (dorp_down_hours.getSelectedIndex() == 0) {
 920                 dorp_down_hours.setSelectedIndex(24);// 設置為23
 921             } else {
 922                 // 小時數-1
 923                 dorp_down_hours.setSelectedIndex(dorp_down_hours
 924                         .getSelectedIndex() - 1);
 925             }
 926         } else {
 927             // 分鍾數-30
 928             dorp_down_minute.setSelectedIndex(dorp_down_minute
 929                     .getSelectedIndex() - 30);
 930         }
 931         valueJudgment();
 932     }
 933 
 934     /**
 935      * 點擊-5按鈕的時候執行的動作
 936      */
 937     private void minus5Action() {
 938         isHMSZero();
 939         if (dorp_down_minute.getSelectedIndex() - 5 < 0) {
 940             dorp_down_minute.setSelectedIndex(dorp_down_minute
 941                     .getSelectedIndex() + 55);// 60-5
 942             if (dorp_down_hours.getSelectedIndex() == 0) {
 943                 dorp_down_hours.setSelectedIndex(24);// 設置為23
 944             } else {
 945                 // 小時數-1
 946                 dorp_down_hours.setSelectedIndex(dorp_down_hours
 947                         .getSelectedIndex() - 1);
 948             }
 949         } else {
 950             // 分鍾數-5
 951             dorp_down_minute.setSelectedIndex(dorp_down_minute
 952                     .getSelectedIndex() - 5);
 953         }
 954         valueJudgment();
 955     }
 956 
 957     /**
 958      * 點擊-1按鈕的時候執行的動作
 959      */
 960     private void minus1Action() {
 961         isHMSZero();
 962         if (dorp_down_minute.getSelectedIndex() - 1 == 0) {
 963             dorp_down_minute.setSelectedIndex(60);// 設置為59
 964             if (dorp_down_hours.getSelectedIndex() == 0) {
 965                 dorp_down_hours.setSelectedIndex(24);// 設置為23
 966             } else {
 967                 // 小時數-1
 968                 dorp_down_hours.setSelectedIndex(dorp_down_hours
 969                         .getSelectedIndex() - 1);
 970             }
 971         } else {
 972             // 分鍾數-1
 973             dorp_down_minute.setSelectedIndex(dorp_down_minute
 974                     .getSelectedIndex() - 1);
 975         }
 976         valueJudgment();
 977     }
 978 
 979     /**
 980      * 判斷dorp_down_hours,dorp_down_minute,drop_down_second當前是否為0,即:"關閉"
 981      */
 982     private void isHMSZero() {
 983         // 如果小時數還沒有設置,那么就設置為當前小時數
 984         if (dorp_down_hours.getSelectedIndex() == 0) {
 985             dorp_down_hours.setSelectedIndex(getHour() + 1);
 986         }
 987         // 如果分鍾數還沒有設置,那么就設置為當前分鍾數
 988         if (dorp_down_minute.getSelectedIndex() == 0) {
 989             dorp_down_minute.setSelectedIndex(getMunite() + 1);
 990         }
 991         // 如果秒鍾還沒有設置,那么就設置為當前秒鍾
 992         if (drop_down_second.getSelectedIndex() == 0) {
 993             drop_down_second.setSelectedIndex(getSecond());
 994         }
 995     }
 996 
 997     /**
 998      * 點擊+30按鈕的時候執行的動作
 999      */
1000     private void add30Action() {
1001         isHMSZero();
1002         if (dorp_down_minute.getSelectedIndex() + 30 > 60) {
1003             dorp_down_minute.setSelectedIndex(dorp_down_minute
1004                     .getSelectedIndex() - 30);// +30-60
1005             if (dorp_down_hours.getSelectedIndex() > 23) {
1006                 dorp_down_hours.setSelectedIndex(1);// 設置為00
1007             } else {
1008                 // 小時數+1
1009                 dorp_down_hours.setSelectedIndex(dorp_down_hours
1010                         .getSelectedIndex() + 1);
1011             }
1012         } else {
1013             // 分鍾數+30
1014             dorp_down_minute.setSelectedIndex(dorp_down_minute
1015                     .getSelectedIndex() + 30);
1016         }
1017         valueJudgment();
1018     }
1019 
1020     /**
1021      * 點擊+5按鈕的時候執行的動作
1022      */
1023     private void add5Action() {
1024         isHMSZero();
1025         if (dorp_down_minute.getSelectedIndex() + 5 > 60) {
1026             dorp_down_minute.setSelectedIndex(dorp_down_minute
1027                     .getSelectedIndex() - 55);// +5-60
1028             if (dorp_down_hours.getSelectedIndex() > 23) {
1029                 dorp_down_hours.setSelectedIndex(1);// 設置為00
1030             } else {
1031                 // 小時數+1
1032                 dorp_down_hours.setSelectedIndex(dorp_down_hours
1033                         .getSelectedIndex() + 1);
1034             }
1035         } else {
1036             // 分鍾數+5
1037             dorp_down_minute.setSelectedIndex(dorp_down_minute
1038                     .getSelectedIndex() + 5);
1039         }
1040         valueJudgment();
1041     }
1042 
1043     /**
1044      * 點擊+1按鈕的時候執行的動作
1045      */
1046     private void add1Action() {
1047         isHMSZero();
1048         if (dorp_down_minute.getSelectedIndex() + 1 > 60) {
1049             dorp_down_minute.setSelectedIndex(1);// 設置為00
1050             if (dorp_down_hours.getSelectedIndex() > 23) {
1051                 dorp_down_hours.setSelectedIndex(1);// 設置為00
1052             } else {
1053                 // 小時數+1
1054                 dorp_down_hours.setSelectedIndex(dorp_down_hours
1055                         .getSelectedIndex() + 1);
1056             }
1057         } else {
1058             // 分鍾數+1
1059             dorp_down_minute.setSelectedIndex(dorp_down_minute
1060                     .getSelectedIndex() + 1);
1061         }
1062         valueJudgment();
1063     }
1064 
1065     /**
1066      * 給h,m,s三個變量賦值,並判斷他們的值
1067      */
1068     private void valueJudgment() {
1069         h = dorp_down_hours.getSelectedItem().toString();
1070         m = dorp_down_minute.getSelectedItem().toString();
1071         s = drop_down_second.getSelectedItem().toString();
1072         hsmCheck();
1073     }
1074 
1075     /**
1076      * 檢查時,分,秒的值,如果都不是"關閉",那么</br>
1077      * 
1078      * <li>stop按鈕要顯示出來</li><br/>
1079      * <li>result要顯示出來剩余時間</li><br/>
1080      * <li>ring_setup要設置為不可用</li> <li>listening_test按鈕為不可用</li> <li>
1081      * listening_test_stop按鈕為不可用</li>
1082      */
1083     private void hsmCheck() {
1084         if (h != "關閉" && m != "關閉" && s != "關閉") {
1085             stop.setVisible(true);
1086             result.setVisible(true);
1087             ring_setup.setEnabled(false);
1088             listening_test.setEnabled(false);
1089             listening_test_stop.setEnabled(false);
1090         }
1091     }
1092 
1093     /**
1094      * stop按鈕的動作: <li>
1095      * 把dorp_down_hours,dorp_down_minute,drop_down_second的值設置為"關閉"</li> <li>
1096      * 隱藏result</li> <li>ring_setup設置為可用</li> <li>listening_test按鈕為可用</li> <li>
1097      * listening_test_stop按鈕為可用</li> <li>stop按鈕設置為不可見</li><li>停止聲音播放</li>
1098      */
1099     private void stopActionPerformed() {
1100         dorp_down_hours.setSelectedIndex(0);
1101         dorp_down_minute.setSelectedIndex(0);
1102         drop_down_second.setSelectedIndex(0);
1103         result.setVisible(false);
1104         ring_setup.setEnabled(true);
1105         listening_test.setEnabled(true);
1106         listening_test_stop.setEnabled(true);
1107         // 這里要停止聲音
1108         stop.setVisible(false);
1109     }
1110 
1111     /**
1112      * 獲取當前小時數
1113      * 
1114      * @return 返回當前小時數
1115      */
1116     private int getHour() {
1117         return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
1118     }
1119 
1120     /**
1121      * 獲取當前分鍾數
1122      * 
1123      * @return 返回當前分鍾數
1124      */
1125     private int getMunite() {
1126         return Calendar.getInstance().get(Calendar.MINUTE);
1127     }
1128 
1129     /**
1130      * 獲取當前秒鍾數
1131      * 
1132      * @return 返回當前秒鍾數
1133      */
1134     private int getSecond() {
1135         return Calendar.getInstance().get(Calendar.SECOND);
1136     }
1137 
1138     /**
1139      * 實現震動功能
1140      */
1141     public void shack() {
1142         Point p = MyAlarm.this.getLocation();
1143         int x = (int) p.getX();
1144         int y = (int) p.getY();
1145         for (int i = 0; i < 5000; i++) {
1146             MyAlarm.this.setLocation(x - 5, y);
1147             MyAlarm.this.setLocation(x, y + 5);
1148             MyAlarm.this.setLocation(x + 5, y);
1149             MyAlarm.this.setLocation(x, y - 5);
1150         }
1151         MyAlarm.this.setLocation(x, y);
1152     }
1153 
1154     /**
1155      * 獲取MyAlarm的X坐標
1156      * 
1157      * @return 返回MyAlarm的X坐標
1158      */
1159     public int getMyAlarmX() {
1160         return (int) getLocation().getX();
1161     }
1162 
1163     /**
1164      * 獲取MyAlarm的Y坐標
1165      * 
1166      * @return 返回MyAlarm的Y坐標
1167      */
1168     public int getMyAlarmY() {
1169         return (int) MyAlarm.this.getLocation().getY();
1170     }
1171 
1172     /**
1173      * 鬧鈴類
1174      * 
1175      * @author Hongten
1176      * 
1177      * @time 2012-3-2 2012
1178      */
1179     class AlarmSound implements Runnable {
1180         private String temAlarm;
1181         private String alarm0Path = "src/image/sound/alarm0.wav";//
1182         private String alarm1Path = "src/image/sound/alarm1.wav";// 
1183         private String alarm2Path = "src/image/sound/alarm2.wav";// 
1184         private String alarm3Path = "src/image/sound/alarm3.wav";// 
1185         private String alarm4Path = "src/image/sound/alarm4.wav";// 
1186         private String alarm5Path = "src/image/sound/alarm5.wav";// 
1187         private String alarm6Path = "src/image/sound/alarm6.wav";// 
1188         private String alarm7Path = "src/image/sound/alarm7.wav";// 
1189         private String alarm8Path = "src/image/sound/alarm8.wav";// 
1190 
1191         public AlarmSound(int a) {
1192             switch (a) {
1193             case 0:
1194                 temAlarm = alarm0Path;
1195                 break;
1196             case 1:
1197                 temAlarm = alarm1Path;
1198                 break;
1199             case 2:
1200                 temAlarm = alarm2Path;
1201                 break;
1202             case 3:
1203                 temAlarm = alarm3Path;
1204                 break;
1205             case 4:
1206                 temAlarm = alarm4Path;
1207                 break;
1208             case 5:
1209                 temAlarm = alarm5Path;
1210                 break;
1211             case 6:
1212                 temAlarm = alarm6Path;
1213                 break;
1214             case 7:
1215                 temAlarm = alarm7Path;
1216                 break;
1217             case 8:
1218                 temAlarm = alarm8Path;
1219                 break;
1220             }
1221         }
1222 
1223         // 讀取聲音文件,並且播放出來
1224         public void run() {
1225             File soundFile = new File(temAlarm);
1226             AudioInputStream audioInputStream = null;
1227             try {
1228                 audioInputStream = AudioSystem.getAudioInputStream(soundFile);
1229             } catch (Exception e1) {
1230                 e1.printStackTrace();
1231                 return;
1232             }
1233             AudioFormat format = audioInputStream.getFormat();
1234             SourceDataLine auline = null;
1235             DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
1236             try {
1237                 auline = (SourceDataLine) AudioSystem.getLine(info);
1238                 auline.open(format);
1239             } catch (Exception e) {
1240                 e.printStackTrace();
1241                 return;
1242             }
1243             auline.start();
1244             int nBytesRead = 0;
1245             byte[] abData = new byte[512];
1246             try {
1247                 while (nBytesRead != -1) {
1248                     nBytesRead = audioInputStream
1249                             .read(abData, 0, abData.length);
1250                     if (nBytesRead >= 0)
1251                         auline.write(abData, 0, nBytesRead);
1252                 }
1253             } catch (IOException e) {
1254                 e.printStackTrace();
1255                 return;
1256             } finally {
1257                 auline.drain();
1258                 auline.close();
1259             }
1260 
1261         }
1262     }
1263 }

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

  1 package com.b510.ui.main;
  2 
  3 import java.awt.AWTException;
  4 import java.awt.Image;
  5 import java.awt.MenuItem;
  6 import java.awt.PopupMenu;
  7 import java.awt.SystemTray;
  8 import java.awt.TrayIcon;
  9 import java.awt.event.ActionEvent;
 10 import java.awt.event.ActionListener;
 11 import java.awt.event.MouseEvent;
 12 import java.awt.event.MouseListener;
 13 
 14 import javax.swing.ImageIcon;
 15 import javax.swing.JFrame;
 16 
 17 import com.b510.menu.AlarmFile;
 18 import com.b510.menu.AlarmTools;
 19 
 20 public class MyTray implements ActionListener, MouseListener {
 21     private Image icon;// 圖標
 22     private TrayIcon trayIcon;
 23     private SystemTray systemTray;// 系統托盤
 24 
 25     private MyAlarm myAlarm; // 托盤所屬主窗體
 26     private PopupMenu pop = new PopupMenu(); // 彈出菜單
 27     // 菜單選項
 28     /** 截圖 */
 29     private MenuItem screenshot = new MenuItem("ScreenShot");
 30     /** 還原 */
 31     private MenuItem open = new MenuItem("Restore");
 32     /** 退出*/
 33     private MenuItem exit =new MenuItem("Exit");
 34     public MyTray(MyAlarm myAlarm) {
 35         this.myAlarm = myAlarm;
 36         // 得到托盤的圖標
 37         icon = new ImageIcon(this.getClass().getClassLoader().getResource(
 38                 "image/mytray.png")).getImage();
 39 
 40         if (SystemTray.isSupported()) {
 41             systemTray = SystemTray.getSystemTray();
 42             // 設置鼠標經過圖標時,顯示的內容
 43             trayIcon = new TrayIcon(icon, "open Alarm", pop);
 44             pop.add(screenshot);
 45             pop.add(open);
 46             pop.add(exit);
 47             // 添加系統托盤
 48             try {
 49                 systemTray.add(trayIcon);
 50             } catch (AWTException e1) {
 51                 e1.printStackTrace();
 52                 trayIcon.addMouseListener(this);
 53             }
 54         }
 55         displayInfoListener();
 56         trayIcon.addMouseListener(this);
 57         exit.addActionListener(this);
 58         open.addActionListener(this);
 59         screenshot.addActionListener(this);
 60     }
 61 
 62     @Override
 63     public void actionPerformed(ActionEvent e) {
 64         if(e.getSource()==exit){
 65             //退出系統
 66             AlarmFile.exit();
 67         }else if (e.getSource() == open) {
 68             // 單點擊菜單中的"還原"選項時,還原窗口
 69             //displayInfo();
 70             //trayIcon.displayMessage("溫馨提示", "hello,world", TrayIcon.MessageType.INFO);
 71             myAlarm.iconed = false;
 72             friendListSet(true);
 73             
 74         } else if (e.getSource() == screenshot) {
 75             // 但點擊“截圖”選項時,進行截圖
 76             AlarmTools.screenshot();
 77         } 
 78     }
 79 
 80     @Override
 81     public void mouseClicked(MouseEvent e) {
 82         // 但鼠標點擊一次的時候,進行彈出窗口
 83         if (e.getClickCount() == 1 && e.getButton() != MouseEvent.BUTTON3) {
 84             if (!myAlarm.isVisible()) {
 85                 friendListSet(true);
 86             } else {
 87                 friendListSet(false);
 88             }
 89         }
 90         // 但鼠標點擊兩次的時候,進行彈出窗口
 91         // 如果窗口有顯示,則隱藏窗口,否則顯示窗口
 92         if (e.getClickCount() == 2 && e.getButton() != MouseEvent.BUTTON3) {
 93             if (!myAlarm.isVisible()) {
 94                 friendListSet(true);
 95             } else {
 96                 friendListSet(false);
 97             }
 98         }
 99     }
100 
101     /**
102      * 設置friendList的可見性
103      */
104     private void friendListSet(boolean flag) {
105         myAlarm.setVisible(true);
106         myAlarm.setExtendedState(JFrame.NORMAL);
107     }
108     
109     
110     public void displayInfoListener() {
111         new Thread(new Runnable() {// 設置一個線程
112                     public void run() {
113                         while (true) {
114                             try {
115                                 Thread.sleep(1000);
116                             } catch (Exception e) {
117                                 e.printStackTrace();
118                             }
119                             if(myAlarm.getSecondOfResult()==30){
120                                  trayIcon.displayMessage("溫馨提示","距鬧鍾設置時間還剩:\n00 時 00 分 " +myAlarm.getSecondOfResult() + " 秒",TrayIcon.MessageType.INFO);
121                                  myAlarm.setSecondOfResult(29);
122                             }
123                             if(myAlarm.getFlagOfBackground()){
124                                 trayIcon.displayMessage("溫馨提示","您已經更改了背景,點擊托盤圖標\n可以看到效果,右鍵托盤圖標\n可以進行功能選擇。",TrayIcon.MessageType.INFO);
125                                 myAlarm.setFlagOfBackground(false);
126                             }
127                         }
128                     }
129                 }).start();
130     }
131 
132     @Override
133     public void mouseEntered(MouseEvent arg0) {
134         // TODO Auto-generated method stub
135 
136     }
137 
138     @Override
139     public void mouseExited(MouseEvent arg0) {
140         // TODO Auto-generated method stub
141 
142     }
143 
144     @Override
145     public void mousePressed(MouseEvent arg0) {
146         // TODO Auto-generated method stub
147 
148     }
149 
150     @Override
151     public void mouseReleased(MouseEvent arg0) {
152         // TODO Auto-generated method stub
153 
154     }
155 }

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

 1 package com.b510.ui.up;
 2 
 3 import java.util.Calendar;
 4 
 5 /**
 6  *
 7  * @author Hongten
 8  * 主界面區-顯示現在的時間
 9  */
10 public class AlarmShowNow {
11 
12     /**
13      * 獲得系統的當前時間,並返回
14      * @return 返回系統當前時間
15      */
16     public static String now() {
17         Calendar cal = Calendar.getInstance();
18         int hour = cal.get(Calendar.HOUR_OF_DAY);
19         int min = cal.get(Calendar.MINUTE);
20         int sec = cal.get(Calendar.SECOND);
21         String current = new String(hour+ ":" + min + ":" + sec);
22         return current;
23     }
24    public static void main(String args[]){
25        System.out.println(now());
26    }
27 }

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

附件(源碼):http://files.cnblogs.com/hongten/alarm.zip

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

 

 

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

 

More reading,and english is important.

 

I'm Hongten

 

 

 

大哥哥大姐姐,覺得有用打賞點哦!多多少少沒關系,一分也是對我的支持和鼓勵。謝謝。
Hongten博客排名在100名以內。粉絲過千。
Hongten出品,必是精品。

 

E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM