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();
}
}
}
}
}