java 托盘程序_java程序加到系统托盘的方法

1 packageswingtest;2

3

4 import javax.swing.*;5 import java.awt.*;6 importjava.awt.event.ActionEvent;7 importjava.awt.event.ActionListener;8 importjava.awt.event.MouseAdapter;9 importjava.awt.event.MouseEvent;10 importjava.io.UnsupportedEncodingException;11 importjava.net.URL;12 importjava.util.Random;13

14

15 public class MainFrame extends JFrame implementsActionListener {16

17 private static final long serialVersionUID = -7078030311369039390L;18 privateJMenu menu;19 privateJMenuBar jmenuBar;20 private String[] jmItemName = {"置于托盘", "系统退出"};21

22 private MainFrame() throwsUnsupportedEncodingException {23 super("电话薄");24 init();25 this.setSize(500, 400);26 this.setJMenuBar(jmenuBar);27 this.setLocationRelativeTo(null);28

29 JButton jbutton = new JButton("test");30 add(jbutton);31

32

33 systemTray(jbutton); //系统托盘

34 }35

36 /**

37 * 初始化界面38 */

39 private voidinit() {40 menu = new JMenu("系统窗体");41 for(String s : jmItemName) {42 JMenuItem menuItem = newJMenuItem(s);43 menuItem.addActionListener(this);44 menu.add(menuItem);45 }46 this.jmenuBar = newJMenuBar();47 this.jmenuBar.add(menu);48 }49

50 @Override51 public voidactionPerformed(ActionEvent e) {52 String actions =e.getActionCommand();53 if ("置于托盘".equals(actions)) {54 this.setVisible(false);55 }56 if ("系统退出".equals(actions)) {57 System.exit(0);58 }59

60 }61

62 /**

63 * 系统托盘图标处理.64 *65 *@paramjbutton66 */

67 private void systemTray(JButton jbutton) throwsUnsupportedEncodingException {68 if (SystemTray.isSupported()) { //判断系统是否支持托盘功能.69 //URL resource = this.getClass().getResource("car.jpg");//获得图片路径

70

71 URL resource = getClass().getClassLoader().getResource("car.jpg");72 //File targetFile = new File("files/8k.wav");73

74 //ClassPathResource classPathResource = new ClassPathResource("static/something.txt");75 //

76 //InputStream inputStream = classPathResource.getInputStream();

77 ImageIcon icon = new ImageIcon(resource); //创建图片对象

78 JPopupMenu popupMenu = new JPopupMenu(); //创建弹出菜单对象

79 JMenuItem itemExit = new JMenuItem("退出系统"); //创建弹出菜单中的退出项

80 JMenuItem itemShow = new JMenuItem("显示窗体"); //创建弹出菜单中的显示主窗体项.

81

82

83 popupMenu.add(itemExit);84 popupMenu.add(itemShow);85 TrayIcon trayIcon = new TrayIcon(icon.getImage(), "电话薄系统");86 SystemTray sysTray =SystemTray.getSystemTray();87

88 try{89 sysTray.add(trayIcon);90 } catch(AWTException ignored) {91 }92 trayIcon.addMouseListener(newMouseAdapter() {93 @Override94 public voidmouseReleased(MouseEvent e) {95 if(e.isPopupTrigger()) {96 popupMenu.setLocation(e.getX(), e.getY());97 popupMenu.setInvoker(popupMenu);98 popupMenu.setVisible(true);99 }100 }101 });102 //给窗体最小化添加事件监听.

103 itemShow.addActionListener(e ->{104 setVisible(true);105

106 });107 //给退出像添加事件监听

108 itemExit.addActionListener(e ->{109 //System.exit(0);

110 sysTray.remove(trayIcon);111 dispose();112 });113 int count = 0;114 jbutton.addActionListener(e ->{115 TrayIcon trayIcon4 = new TrayIcon(icon.getImage(), "电话薄系统");116 try{117 sysTray.add(trayIcon4);118 } catch(AWTException ex) {119 ex.printStackTrace();120 }121 });122

123 }124 }125

126 /**

127 * 主方法128 *129 *@paramargs sdf130 */

131 public static void main(String[] args) throwsUnsupportedEncodingException {132 new MainFrame().setVisible(true);133 }134 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值