1 packagecom.b510.ui.main;2
3 importjava.awt.Color;4 importjava.awt.Graphics;5 importjava.awt.Point;6 importjava.awt.event.ActionEvent;7 importjava.awt.event.ActionListener;8 importjava.awt.event.WindowAdapter;9 importjava.awt.event.WindowEvent;10 importjava.io.File;11 importjava.io.IOException;12 importjava.util.Calendar;13
14 importjavax.sound.sampled.AudioFormat;15 importjavax.sound.sampled.AudioInputStream;16 importjavax.sound.sampled.AudioSystem;17 importjavax.sound.sampled.DataLine;18 importjavax.sound.sampled.SourceDataLine;19 importjavax.swing.ImageIcon;20 importjavax.swing.JFrame;21 importjavax.swing.JLabel;22 importjavax.swing.JOptionPane;23 importjavax.swing.JPanel;24
25 importcom.b510.menu.AlarmBackground;26 importcom.b510.menu.AlarmFile;27 importcom.b510.menu.AlarmHelp;28 importcom.b510.menu.AlarmTools;29
30 /**
31 *32 *@authorHongten33 */
34 public class MyAlarm extends JFrame implementsActionListener {35
36 /**
37 *+138 */
39 privatejavax.swing.JButton add1;40 /**
41 * +3042 */
43 privatejavax.swing.JButton add30;44 /**
45 * +546 */
47 privatejavax.swing.JButton add5;48 /**
49 * 时50 */
51 privatejavax.swing.JComboBox dorp_down_hours;52 /**
53 * 分54 */
55 privatejavax.swing.JComboBox dorp_down_minute;56 /**
57 * 秒58 */
59 privatejavax.swing.JComboBox drop_down_second;60 /**
61 * 试听62 */
63 privatejavax.swing.JButton listening_test;64 /**
65 * 试听-停止66 */
67 privatejavax.swing.JButton listening_test_stop;68 /**
69 * 主Panel70 */
71 privatejavax.swing.JPanel mainPanel;72 /**
73 * 菜单bar74 */
75 privatejavax.swing.JMenuBar menuBar;76 /**
77 * -178 */
79 privatejavax.swing.JButton minus1;80 /**
81 * -3082 */
83 privatejavax.swing.JButton minus30;84 /**
85 * -586 */
87 privatejavax.swing.JButton minus5;88 /**
89 * 当前时间90 */
91 privatejavax.swing.JLabel now;92 /**
93 * 铃声94 */
95 privatejavax.swing.JComboBox ring_setup;96 /**
97 * 截屏98 */
99 privatejavax.swing.JMenuItem screenshot;100 /**
101 * 更改背景图片102 */
103 privatejavax.swing.JMenuItem changeBackground;104 /**
105 * 停止106 */
107 privatejavax.swing.JButton stop;108 /**
109 * 工具110 */
111 privatejavax.swing.JMenu tools;112 /**
113 * 帮助114 */
115 privatejavax.swing.JMenu help;116 /**
117 * 关于118 */
119 privatejavax.swing.JMenuItem about;120 /**
121 * 退出122 */
123 privatejavax.swing.JMenuItem exit;124 /**
125 * 上传铃声126 */
127 privatejavax.swing.JMenuItem uploadRing;128 /**
129 * wenjian130 */
131 privatejavax.swing.JMenu file;132 /**
133 * 结果,即剩余时间134 */
135 privateJLabel result;136 /**
137 * 分割线138 */
139 privatejavax.swing.JSeparator line;140 /**
141 * 变量-->小时142 */
143 privateString h;144 /**
145 * 变量-->分钟146 */
147 privateString m;148 /**
149 * 变量-->秒150 */
151 privateString 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 intsecondOfResult;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 publicMyAlarm(String title) {188 this.setTitle(title);189 init();190 }191
192 /**
193 * 初始化背景图片194 */
195 public voidinitMainPanel() {196 mainPanel = newJPanel() {197 private static final long serialVersionUID = 1L;198
199 protected voidpaintComponent(Graphics g) {200 ImageIcon icon = newImageIcon(AlarmBackground201 .getBackground(background));202 g.drawImage(icon.getImage(), 0, 0, 545, 463, null);203 }204 };205 }206
207 /**
208 * 主界面初始化209 */
210 public voidinit() {211 initMainPanel();212 now = newjavax.swing.JLabel();213 stop = newjavax.swing.JButton();214 add1 = newjavax.swing.JButton();215 add5 = newjavax.swing.JButton();216 add30 = newjavax.swing.JButton();217 minus1 = newjavax.swing.JButton();218 minus5 = newjavax.swing.JButton();219 minus30 = newjavax.swing.JButton();220 listening_test = newjavax.swing.JButton();221 listening_test_stop = newjavax.swing.JButton();222 dorp_down_hours = newjavax.swing.JComboBox();223 dorp_down_minute = newjavax.swing.JComboBox();224 drop_down_second = newjavax.swing.JComboBox();225 ring_setup = newjavax.swing.JComboBox();226 menuBar = newjavax.swing.JMenuBar();227 file = newjavax.swing.JMenu();228 uploadRing = newjavax.swing.JMenuItem();229 exit = newjavax.swing.JMenuItem();230 tools = newjavax.swing.JMenu();231 screenshot = newjavax.swing.JMenuItem();232 changeBackground = newjavax.swing.JMenuItem();233 help = newjavax.swing.JMenu();234 about = newjavax.swing.JMenuItem();235 line = newjavax.swing.JSeparator();236 result = newjavax.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"); //NOI18N248 //时间监听器,得到系统时间和设置好时间后,得到剩余时间
249 timeListener();250
251 stop.setText("stop"); //NOI18N
252 stop.setName("stop"); //NOI18N253 //初始化的时候是不可见的
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(newjavax.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(newjavax.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(newjavax.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(newString[] {319 "铃声一", "铃声二", "铃声三", "铃声四", "铃声五", "铃声六", "铃声七"}));320 ring_setup.setName("ring_setup"); //NOI18N321 //主要的布局
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(newWindowAdapter() {380 public voidwindowIconified(WindowEvent e) {381 iconed = true;382 setVisible(false);383 }384
385 public voidwindowClosing(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 voidomponentLayout() {407 javax.swing.GroupLayout mainPanelLayout = newjavax.swing.GroupLayout(408 mainPanel);409 mainPanel.setLayout(mainPanelLayout);410 mainPanelLayout411 .setHorizontalGroup(mainPanelLayout412 .createParallelGroup(413 javax.swing.GroupLayout.Alignment.LEADING)414 .addGroup(415 javax.swing.GroupLayout.Alignment.TRAILING,416 mainPanelLayout417 .createSequentialGroup()418 .addContainerGap(170, Short.MAX_VALUE)419 .addGroup(420 mainPanelLayout421 .createParallelGroup(422 javax.swing.GroupLayout.Alignment.LEADING)423 .addGroup(424 mainPanelLayout425 .createSequentialGroup()426 .addComponent(427 result)428 .addContainerGap())429 .addGroup(430 javax.swing.GroupLayout.Alignment.TRAILING,431 mainPanelLayout432 .createParallelGroup(433 javax.swing.GroupLayout.Alignment.LEADING)434 .addGroup(435 mainPanelLayout436 .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 mainPanelLayout446 .createSequentialGroup()447 .addGroup(448 mainPanelLayout449 .createParallelGroup(450 javax.swing.GroupLayout.Alignment.LEADING)451 .addGroup(452 mainPanelLayout453 .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 mainPanelLayout479 .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 mainPanelLayout504 .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 mainPanelLayout523 .setVerticalGroup(mainPanelLayout524 .createParallelGroup(525 javax.swing.GroupLayout.Alignment.LEADING)526 .addGroup(527 javax.swing.GroupLayout.Alignment.TRAILING,528 mainPanelLayout529 .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 mainPanelLayout541 .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 mainPanelLayout563 .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 mainPanelLayout574 .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 voidtimeListener() {592 new Thread(new Runnable() {//设置一个线程
593 public voidrun() {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 voidmyListener() {612 new Thread(new Runnable() {//设置一个线程
613 public voidrun() {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 *@returnresult值630 */
631 publicString 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 returnr;731 }732
733 public intgetSecondOfResult() {734 returnsecondOfResult;735 }736
737 public void setSecondOfResult(intsec) {738 this.secondOfResult =sec;739 }740
741 public booleangetFlagOfBackground() {742 returnflagOfBackground;743 }744
745 public void setFlagOfBackground(booleanflag) {746 this.flagOfBackground =flag;747 }748
749 /**
750 * 时间到了的时候就播放声音751 */
752 public voidexecuteSound() {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(newAlarmSound(ring_setup.getSelectedIndex())).start();781 //震动
782 shack();783 }784 }785
786 /**
787 * 得到系统时间当前时间,并返回788 *789 *@return返回系统当前时间790 */
791 publicString 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 returncurrent;802 }803
804 /**
805 * 布局下面的按钮和下拉选项的监听器806 */
807 @SuppressWarnings("deprecation")808 @Override809 public voidactionPerformed(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 = newAlarmSound(ring_setup.getSelectedIndex());851 Thread alarmThread = newThread(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 * 更改背景图片 当点击工具-- >更改背景,这里就可以处理更改背景图片896 */
897 private voidchangeBG() {898 setFlagOfBackground(true);899 if (background
911 /**
912 * 点击-30按钮的时候执行的动作913 */
914 private voidminus30Action() {915 isHMSZero();916 if (dorp_down_minute.getSelectedIndex() - 30 < 0) {917 dorp_down_minute.setSelectedIndex(dorp_down_minute918 .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_hours924 .getSelectedIndex() - 1);925 }926 } else{927 //分钟数-30
928 dorp_down_minute.setSelectedIndex(dorp_down_minute929 .getSelectedIndex() - 30);930 }931 valueJudgment();932 }933
934 /**
935 * 点击-5按钮的时候执行的动作936 */
937 private voidminus5Action() {938 isHMSZero();939 if (dorp_down_minute.getSelectedIndex() - 5 < 0) {940 dorp_down_minute.setSelectedIndex(dorp_down_minute941 .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_hours947 .getSelectedIndex() - 1);948 }949 } else{950 //分钟数-5
951 dorp_down_minute.setSelectedIndex(dorp_down_minute952 .getSelectedIndex() - 5);953 }954 valueJudgment();955 }956
957 /**
958 * 点击-1按钮的时候执行的动作959 */
960 private voidminus1Action() {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_hours969 .getSelectedIndex() - 1);970 }971 } else{972 //分钟数-1
973 dorp_down_minute.setSelectedIndex(dorp_down_minute974 .getSelectedIndex() - 1);975 }976 valueJudgment();977 }978
979 /**
980 * 判断dorp_down_hours,dorp_down_minute,drop_down_second当前是否为0,即:"关闭"981 */
982 private voidisHMSZero() {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 voidadd30Action() {1001 isHMSZero();1002 if (dorp_down_minute.getSelectedIndex() + 30 > 60) {1003 dorp_down_minute.setSelectedIndex(dorp_down_minute1004 .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_hours1010 .getSelectedIndex() + 1);1011 }1012 } else{1013 //分钟数+30
1014 dorp_down_minute.setSelectedIndex(dorp_down_minute1015 .getSelectedIndex() + 30);1016 }1017 valueJudgment();1018 }1019
1020 /**
1021 * 点击+5按钮的时候执行的动作1022 */
1023 private voidadd5Action() {1024 isHMSZero();1025 if (dorp_down_minute.getSelectedIndex() + 5 > 60) {1026 dorp_down_minute.setSelectedIndex(dorp_down_minute1027 .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_hours1033 .getSelectedIndex() + 1);1034 }1035 } else{1036 //分钟数+5
1037 dorp_down_minute.setSelectedIndex(dorp_down_minute1038 .getSelectedIndex() + 5);1039 }1040 valueJudgment();1041 }1042
1043 /**
1044 * 点击+1按钮的时候执行的动作1045 */
1046 private voidadd1Action() {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_hours1055 .getSelectedIndex() + 1);1056 }1057 } else{1058 //分钟数+1
1059 dorp_down_minute.setSelectedIndex(dorp_down_minute1060 .getSelectedIndex() + 1);1061 }1062 valueJudgment();1063 }1064
1065 /**
1066 * 给h,m,s三个变量赋值,并判断他们的值1067 */
1068 private voidvalueJudgment() {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 * 检查时,分,秒的值,如果都不是"关闭",那么1077 *1078 *
stop按钮要显示出来1079 * result要显示出来剩余时间
1080 * ring_setup要设置为不可用 listening_test按钮为不可用 1081 * listening_test_stop按钮为不可用1082 */
1083 private voidhsmCheck() {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按钮的动作:
1095 * 把dorp_down_hours,dorp_down_minute,drop_down_second的值设置为"关闭" 1096 * 隐藏result ring_setup设置为可用 listening_test按钮为可用 1097 * listening_test_stop按钮为可用 stop按钮设置为不可见停止声音播放1098 */1099 private voidstopActionPerformed() {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 intgetHour() {1117 returnCalendar.getInstance().get(Calendar.HOUR_OF_DAY);1118 }1119
1120 /**
1121 * 获取当前分钟数1122 *1123 *@return返回当前分钟数1124 */
1125 private intgetMunite() {1126 returnCalendar.getInstance().get(Calendar.MINUTE);1127 }1128
1129 /**
1130 * 获取当前秒钟数1131 *1132 *@return返回当前秒钟数1133 */
1134 private intgetSecond() {1135 returnCalendar.getInstance().get(Calendar.SECOND);1136 }1137
1138 /**
1139 * 实现震动功能1140 */
1141 public voidshack() {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 intgetMyAlarmX() {1160 return (int) getLocation().getX();1161 }1162
1163 /**
1164 * 获取MyAlarm的Y坐标1165 *1166 *@return返回MyAlarm的Y坐标1167 */
1168 public intgetMyAlarmY() {1169 return (int) MyAlarm.this.getLocation().getY();1170 }1171
1172 /**
1173 * 闹铃类1174 *1175 *@authorHongten1176 *1177 * @time 2012-3-2 20121178 */
1179 class AlarmSound implementsRunnable {1180 privateString 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(inta) {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 voidrun() {1225 File soundFile = newFile(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 =audioInputStream1249 .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 }