在java中如何在任务栏下显示一个托盘
托盘就是平时我们在下面看到的任务栏下看到的图标,
?
public class MacBar extends JFrame{
??? public static void main(String[] args) {
??? ??? new MacBar();
??? }
??? public MacBar()
??? {
??? ??? setAlwaysOnTop(true);
??? ??? setSize(400,110);
??? ??? this.setLocation(90,150);
??? ??? this.setVisible(true);
??? ??? createTray();//这条语句就是要设置一个托盘的效果
??? }
??? //设置状态栏下的图标
??? public void createTray()
??? {
??? ??? TrayIcon tray=new TrayIcon(new ImageIcon("image/tray.png").getImage());
??? ??? tray.setToolTip("请点击");//提示信息
??? ??? tray.setImageAutoSize(true);//让图片自动调整大小,主要是防止图片太大的问题
??? ??? tray.addActionListener(new ActionListener() {
??? ??? ???
??? ??? ??? @Override
??? ??? ??? public void actionPerformed(ActionEvent e) {
??? ??? ??? ???
??? ??? ??? ??? MacBar.this.setVisible(true);//点击时显示出来
??? ??? ??? }
??? ??? });
??????? //这部分表示的是在托盘上点击鼠标右键时弹出的菜单
??? ??? PopupMenu pm=new PopupMenu();
??? ??? MenuItem mi=new MenuItem("Exit");
??? ??? mi.addActionListener(new ActionListener() {
??? ??? ???
??? ??? ??? @Override
??? ??? ??? public void actionPerformed(ActionEvent e) {
??? ??? ??? ??? // TODO Auto-generated method stub
??? ??? ??? ??? System.exit(0);
??? ??? ??? }
??? ??? });
??????? pm.add(mi);
??????? tray.setPopupMenu(pm);
??? ??? if(SystemTray.isSupported())
??? ??? {
??? ??? ??? SystemTray systemtray=SystemTray.getSystemTray();
??? ??? ??? try {
??? ??? ??? ??? systemtray.add(tray);//要有这句话,不然就看不到图标了
??? ??? ??? } catch (AWTException e1) {
??? ??? ??? ??? // TODO Auto-generated catch block
??? ??? ??? ??? e1.printStackTrace();
??? ??? ??? }
??? ??? }
??? }
}