JDK6.0 支持直接对托盘的操作
我做了一个测试的例子 现在在程序中 怎么判断 目前是否在托盘中已经加了TrayIcon
这个方法是什么 我没有6.0的API 谁知道告诉我一下 谢谢
代码如下 参考一下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class TrayTest extends JFrame implements ActionListener{
private TrayIcon trayIcon;
private JPanel jpanel;
private JButton jb1,jb2;
public TrayTest() {
jb1=new JButton("托盘中显示");
jb2=new JButton("托盘中取消");
jpanel=new JPanel();
jpanel.add(jb1);
jpanel.add(jb2);
add(jpanel);
//下面时事件处理
jb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
SystemTray.getSystemTray().add(trayIcon);
//如果托盘一面已经有.. 则会抛出异常. 这里没做判断
}catch(Exception ex){
System.out.println(ex);
}
}
});
jb2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
SystemTray.getSystemTray().remove(trayIcon);
}
});
addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e){//关闭
SystemTray.getSystemTray().remove(trayIcon);
System.exit(0);
}
public void windowIconified(WindowEvent e){//最小化调用
if(SystemTray.isSupported())
{
setVisible(false);
}
else{
System.out.println("无法使用托盘");
}
}
public void windowDeiconified(WindowEvent e){//从最小化变为正常状态时调用
}
});
//事件处理完成.....
initTrayIcon();
setTitle("java托盘的例子");
setSize(300,300);
setVisible(true);
}
private void initTrayIcon(){
SystemTray tray=SystemTray.getSystemTray();
//装载托盘图象
Image image=Toolkit.getDefaultToolkit().getImage("Tray.jpg");
//添加菜单
PopupMenu popup=new PopupMenu();
MenuItem openItem=new MenuItem("open");
MenuItem closeItem=new MenuItem("close");
popup.add(openItem);
popup.add(closeItem);
//为托盘添加信息
trayIcon=new TrayIcon(image,"提示",popup);
try{
tray.add(trayIcon);
}catch(Exception ex){
System.out.println(ex);
}
ActionListener showListener=new ActionListener(){
public void actionPerformed(ActionEvent e){
setVisible(true);
}
};
ActionListener closeListener=new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
};
openItem.addActionListener(showListener);
closeItem.addActionListener(closeListener);
trayIcon.addActionListener(showListener);//同时监听默认事件(Windows下双击托盘图标会触发这个事件)
}
public void actionPerformed(ActionEvent ee){
//......
}
public static void main(String[] args){
TrayTest test=new TrayTest();
}
}
JDK6.0有关托盘的问题