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 }
====================================================================
附件(源码):https://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
========================================================