java系统托盘(代码)

该例子共有2个类文件和一个图片文件夹

MainFrame类:定义程序界面
SetTrayIcon类:定义程序系统托盘功能

使用规则:
SetTrayIcon类可以不用修改,直接使用.
只需要在程序构造函数中插入两句即可:
第一句:new SetTrayIcon(this); //调用SetTrayIcon(JFrame)类,设置程序系统托盘功能
第二句:this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); //关键.设置窗口关闭处理,与关闭窗体事件搭配

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MainFrame extends JFrame { //以下两标签为窗口界面图片 JLabel l_img; JLabel l_img2; public MainFrame() { l_img=new JLabel(new ImageIcon(this.getClass().getClassLoader().getResource("images//netbean1.png"))); l_img2=new JLabel(new ImageIcon(this.getClass().getClassLoader().getResource("images//netbean2.png"))); l_img.setBounds(15,15,l_img.getIcon().getIconWidth(),l_img.getIcon().getIconHeight()); l_img2.setBounds(15,15+l_img.getIcon().getIconHeight()+20,l_img2.getIcon().getIconWidth(),l_img2.getIcon().getIconHeight()); add(l_img); add(l_img2); this.setTitle("Java托盘测试"); this.setSize(500,350); this.setResizable(false); this.setLayout(null); center(); //调用窗体于屏幕居中函数 new SetTrayIcon(this); //调用SetTrayIcon(JFrame)类,设置程序系统托盘功能 this.setVisible(true); this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); //关键.设置窗口关闭处理,与关闭窗体事件搭配 } // 设置窗体于屏幕居中函数 private void center() { Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize(); //获得屏幕的尺寸 Dimension jframeSize=this.getSize(); //获得程序尺寸 setLocation((screenSize.width-jframeSize.width)/2,(screenSize.height-jframeSize.height)/2); } public static void main(String[] args) { new MainFrame(); } }

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SetTrayIcon implements ActionListener { PopupMenu pop; MenuItem open,close; TrayIcon trayicon; //任务栏程序托盘图标 JFrame jframe; public SetTrayIcon(JFrame jframe) { this.jframe=jframe; initComponents(); //初始化程序托盘组件 } //设置编译阶段禁用警告提示 @SuppressWarnings("unchecked") //初始化程序托盘组件 private void initComponents() { pop=new PopupMenu(); open=new MenuItem("打开"); open.addActionListener(this); close=new MenuItem("退出"); close.addActionListener(this); pop.add(open); pop.add(close); //检查平台是否受支持系统托盘 if(SystemTray.isSupported()) { SystemTray tray=SystemTray.getSystemTray(); ImageIcon imageIcon=new ImageIcon("images/user_edit.png"); //获得托盘显示图标 Image icon=imageIcon.getImage(); //获得Image对象 //Image icon=this.getToolkit().getImage(this.getClass().getClassLoader().getResource("images/user_edit.png")); trayicon=new TrayIcon(icon,"java系统托盘",pop); //为托盘添加鼠标事件,双击则打开程序界面 trayicon.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e) { if(e.getClickCount()==2) { openFrame(); } } }); try{ tray.add(trayicon); }catch(AWTException e) { e.printStackTrace(); } } //为程序添加窗体事件 jframe.addWindowListener(new WindowListener(){ public void windowActivated(WindowEvent arg0) { // TODO Auto-generated method stub } //隐藏程序于系统托盘函数 public void windowClosed(WindowEvent arg0) { // TODO Auto-generated method stub jframe.setVisible(false); jframe.dispose(); } public void windowClosing(WindowEvent arg0) { // TODO Auto-generated method stub int a=JOptionPane.showConfirmDialog(jframe,"按 [是] 退出,[否] 隐藏?","提示",0); if(a==0){System.exit(0);} else if(a==1){jframe.setVisible(false);} } public void windowDeactivated(WindowEvent arg0) { // TODO Auto-generated method stub } public void windowDeiconified(WindowEvent arg0) { // TODO Auto-generated method stub } //窗口最小化 public void windowIconified(WindowEvent arg0) { // TODO Auto-generated method stub jframe.setVisible(false); //jframe.dispose(); } public void windowOpened(WindowEvent arg0) { // TODO Auto-generated method stub } }); } //为右击托盘菜单添加事件 public void actionPerformed(ActionEvent e) { if(e.getSource()==open) { openFrame(); } if(e.getSource()==close) { System.exit(-1); } } //显示程序界面函数 public void openFrame() { jframe.setVisible(true); jframe.setAlwaysOnTop(true); } }

 

你可能感兴趣的:(java,image,String,Class,import,任务)