首页 > 学院 > 开发设计 > 正文

如何在任务栏显示java程序图标

2019-11-17 04:09:12
字体:
来源:转载
供稿:网友
该代码实现了在系统右下角的任务栏中显示程序的图标,并且最小化程序后单击图标可以显示出来这个程序窗口。

view plaincopy to clipboardPRint?
import java.awt.Color;   
import java.awt.Image;   
import java.awt.MenuItem;   
import java.awt.PopupMenu;   
import java.awt.Toolkit;   
  
  
import javax.swing.JFrame;   
import javax.swing.JLabel;   
  
  
public class test extends JFrame    
{   
long setTime = 30*1000;   
  
JLabel jl = new JLabel("剩余时间:");   
  
JLabel jl1 = new JLabel();   
  
PopupMenu popupMenu1 = new PopupMenu();   
MenuItem menuItem1 = new MenuItem();   
  
public examTime(){   
   this.setLocation(200, 200);   
   this.setSize(300, 200);   
   isTray();   
   this.setVisible(true);   
   addWindowListener(new WindowAdapter()   
     {   
         public void windowIconified(WindowEvent evt)   
         {   
         unVisible();   
         }   
     });   
     
   popupMenu1.setLabel("PopupMenu");   
        menuItem1.setLabel("打开");   
        menuItem1.addActionListener(new ActionListener()   
        {   
            public void actionPerformed(ActionEvent evt)   
            {   
            showw();   
            }   
        });   
        popupMenu1.add(menuItem1);   
}   
  
public void unVisible(){   
   this.setVisible(false);   
}   
  
public void showw(){   
   this.setVisible(true);   
}   
public void isTray()   
    {   
        try  
        {   
          if (SystemTray.isSupported())   
          {// 判断当前平台是否支持系统托盘   
            SystemTray st = SystemTray.getSystemTray();   
            Image image = Toolkit.getDefaultToolkit().getImage(   
               "E:/eclipse/workspace/test/test.gif");//定义托盘图标的图片     
           TrayIcon ti = new TrayIcon( image);   
           ti.setToolTip ( "test ");   
           ti.setPopupMenu ( this.popupMenu1);   
           st.add(ti);            
          }   
        }   
        catch (Exception e)   
        {   
               
        }   
       
  
    }   
public static void main(String[] args)   
    {   
        new test();   
    }   
}  
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表