Java swing jmf 模仿来信息时候的闪动及声音
2015-06-24 17:28
519 查看
Java swing jmf 模仿来信息时候的闪动及声音,需要jmf的jar包支持。点击托盘的时候显示信息,托盘不闪动且声音停止。
package com.ui; import java.awt.AWTException; import java.awt.Frame; import java.awt.Image; import java.awt.SystemTray; import java.awt.TextArea; import java.awt.TrayIcon; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.util.Date; import javax.media.CannotRealizeException; import javax.media.Manager; import javax.media.MediaLocator; import javax.media.NoPlayerException; import javax.media.Player; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.SwingUtilities; /** * * 模仿来信息时候的闪动及声音 * 点击托盘 音乐停止 闪动停止 * @author 崔耀强 * */ public class ShaveTray extends JFrame { /** */ private static final long serialVersionUID = 1L; private SystemTray sysTray;// 当前操作系统的托盘对象 private TrayIcon trayIcon;// 当前对象的托盘 private ImageIcon icon = null; private TextArea ta = null; private boolean flag=true; private Player player; public ShaveTray() { this.createTrayIcon();// 创建托盘对象 Image image = this.getToolkit().getImage("src/com/img/1.jpg"); this.setIconImage(image); init(); } /** * 初始化窗体的方法 */ public void init() { this.setTitle("消息盒子"); ta = new TextArea(); ta.setEditable(false); this.add(ta); this.setSize(400, 400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setExtendedState(Frame.ICONIFIED );//最xiao化 this.setVisible(true); } public void createTrayIcon() { sysTray = SystemTray.getSystemTray();// 获得当前操作系统的托盘对象 icon = new ImageIcon("src/com/img/f17.gif");// 托盘图标 ShaveTray.this.setExtendedState(JFrame.NORMAL); ShaveTray.this.toFront(); //显示窗口到最前端 trayIcon = new TrayIcon(icon.getImage(), "消息盒子"); /** 添加鼠标监听器,当鼠标在托盘图标上双击时, 默认显示窗口 */ trayIcon.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 1) { // 鼠标双击 ta.append(new Date()+":《通知》这是一个新的通知。 \n"); // 设置通知消息内容 ShaveTray.this.setExtendedState(JFrame.NORMAL); ShaveTray.this.setVisible(true); // 显示窗口 ShaveTray.this.toFront(); //停止闪烁 flag=false; //停止音乐播放 player.stop(); } } }); try { sysTray.add(trayIcon); new shave_munsic(trayIcon).start(); } catch (AWTException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } /** * main方法 */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new ShaveTray(); } }); } /** * 线程控制闪动 与播放声音 */ class shave_munsic extends Thread{ private TrayIcon trayIcon; public shave_munsic(TrayIcon trayIcon) { // TODO Auto-generated constructor stub this.trayIcon=trayIcon; } public void run() { // 播放消息提示音 try { File musicFile=new File("sound/msg.wav");//得到一个MP3文件,不加斜杠表示根目录 if(musicFile.exists()){ MediaLocator locator=new MediaLocator("file:"+musicFile.getAbsolutePath()); player = Manager.createRealizedPlayer(locator); player.prefetch();// 预读文件 }else{ System.err.println(musicFile+" 找不到"); } player.start();//播放 } catch (MalformedURLException e) { e.printStackTrace(); } catch (NoPlayerException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (CannotRealizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 闪动消息的空白时间 while(flag){ try { this.trayIcon.setImage(new ImageIcon("").getImage()); Thread.sleep(500); // 闪动消息的提示图片 this.trayIcon.setImage(icon.getImage()); Thread.sleep(500); } catch (Exception e) { e.printStackTrace(); } } } } }
相关文章推荐
- Java抽象类
- java代理
- eclipse打不开闪退解决方式
- Java 解析epub格式电子书,helloWorld程序,附带源程序和相关jar包
- 尚学堂Spring视频教程(三):Spring Core中的其他特性
- Spring整合JMS(三)——MessageConverter介绍
- Spring整合JMS(二)——三种消息监听器
- Java接口定义和实现
- Java中HashMap遍历的两种方式
- LinkedHashMap 根据PUT顺序排序Map
- (转)spring hibernate properties详解
- Java生成图片验证码(有点仿QQ验证码的意思)
- Spring整合JMS(一)——基于ActiveMQ实现
- os x 下jni小实验
- Spring InitializingBean/init-method和DisposableBean/destroy-method
- Java中的RTTI与反射
- java web中表单提交的方式
- java处理json的工具类(list,map和json的之间的转换)
- Spring aop基于注解
- LeetCode_48---Rotate Image