package swingtest;

 import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Random; public class MainFrame extends JFrame implements ActionListener { private static final long serialVersionUID = -7078030311369039390L;
private JMenu menu;
private JMenuBar jmenuBar;
private String[] jmItemName = {"置于托盘", "系统退出"}; private MainFrame() throws UnsupportedEncodingException {
super("电话薄");
init();
this.setSize(500, 400);
this.setJMenuBar(jmenuBar);
this.setLocationRelativeTo(null); JButton jbutton = new JButton("test");
add(jbutton); systemTray(jbutton); //系统托盘
} /**
* 初始化界面
*/
private void init() {
menu = new JMenu("系统窗体");
for (String s : jmItemName) {
JMenuItem menuItem = new JMenuItem(s);
menuItem.addActionListener(this);
menu.add(menuItem);
}
this.jmenuBar = new JMenuBar();
this.jmenuBar.add(menu);
} @Override
public void actionPerformed(ActionEvent e) {
String actions = e.getActionCommand();
if ("置于托盘".equals(actions)) {
this.setVisible(false);
}
if ("系统退出".equals(actions)) {
System.exit(0);
} } /**
* 系统托盘图标处理.
*
* @param jbutton
*/
private void systemTray(JButton jbutton) throws UnsupportedEncodingException {
if (SystemTray.isSupported()) { //判断系统是否支持托盘功能.
// URL resource = this.getClass().getResource("car.jpg"); //获得图片路径 URL resource = getClass().getClassLoader().getResource("car.jpg");
// File targetFile = new File("files/8k.wav"); // ClassPathResource classPathResource = new ClassPathResource("static/something.txt");
//
// InputStream inputStream = classPathResource.getInputStream();
ImageIcon icon = new ImageIcon(resource); //创建图片对象
JPopupMenu popupMenu = new JPopupMenu(); //创建弹出菜单对象
JMenuItem itemExit = new JMenuItem("退出系统"); //创建弹出菜单中的退出项
JMenuItem itemShow = new JMenuItem("显示窗体"); //创建弹出菜单中的显示主窗体项. popupMenu.add(itemExit);
popupMenu.add(itemShow);
TrayIcon trayIcon = new TrayIcon(icon.getImage(), "电话薄系统");
SystemTray sysTray = SystemTray.getSystemTray(); try {
sysTray.add(trayIcon);
} catch (AWTException ignored) {
}
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
popupMenu.setLocation(e.getX(), e.getY());
popupMenu.setInvoker(popupMenu);
popupMenu.setVisible(true);
}
}
});
//给窗体最小化添加事件监听.
itemShow.addActionListener(e -> {
setVisible(true); });
//给退出像添加事件监听
itemExit.addActionListener(e -> {
// System.exit(0);
sysTray.remove(trayIcon);
dispose();
});
int count = 0;
jbutton.addActionListener(e -> {
TrayIcon trayIcon4 = new TrayIcon(icon.getImage(), "电话薄系统");
try {
sysTray.add(trayIcon4);
} catch (AWTException ex) {
ex.printStackTrace();
}
}); }
} /**
* 主方法
*
* @param args sdf
*/
public static void main(String[] args) throws UnsupportedEncodingException {
new MainFrame().setVisible(true);
}
}

最新文章

  1. ka/ks
  2. sqlserver如何关闭死锁进程.
  3. atom编辑markdown之上传图片
  4. oracle 11g的企业管理器
  5. jQuery获取cookie
  6. LTE Module User Documentation(翻译13)——频率复用算法(Frequency Reuse Algorithms)
  7. attempted to assign id from null one-to-one
  8. Thinkphp框架----微信公众测试号开发
  9. Python集成开发环境(Eclipse+Pydev)
  10. (step6.3.3)hdu 1150(Machine Schedule——二分图的最小点覆盖数)
  11. linux下查看进城(ps)的方法 与 杀死进程(kill)的N种方法
  12. python的operator.itemgetter('click')用于定义获取'click'项的函数
  13. (二十八)QQ好友列表的展开收缩
  14. mt6577驱动开发 笔记版
  15. mybatis自定义代码生成器(Generator)——自动生成model&dao代码
  16. cache2go源码最后一讲 - examples
  17. Python版本微信跳一跳,软件配置
  18. 解决table td里面长串数字或字母不换行的问题
  19. This seems to be a pre-built javascript file. webpack报这个警告怎么办?
  20. 《梦断代码》读书笔记 part3

热门文章

  1. Dom修改元素样式
  2. swoole 定时器 swoole_time_tick 和 swoole_time_after
  3. Python之concurrent.futures模块的使用
  4. java+实现上传文件夹
  5. FZU - 2103 Bin & Jing in wonderland
  6. vue-cli3中axios如何跨域请求以及axios封装
  7. 如何在main.js中改变vuex中的值?
  8. 20175215 2018-2019-2 第九周java课程学习总结
  9. JS基础_使用工厂方法创建对象
  10. 尚学堂requireJs课程---2、模块