main class
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class AstCenterBootstrap extends JFrame {
private static final long serialVersionUID = -5102211705496305088L;
public AstCenterBootstrap() throws HeadlessException {
super();
initComponents();
}
public AstCenterBootstrap(GraphicsConfiguration gc) {
super(gc);
initComponents();
}
public AstCenterBootstrap(String title, GraphicsConfiguration gc) {
super(title, gc);
initComponents();
}
public AstCenterBootstrap(String title) throws HeadlessException {
super(title);
initComponents();
}
public void initComponents() {
setBackground(Color.lightGray);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
new AstCenterTrayIcon(this);
setResizable(false);
Image image = Toolkit.getDefaultToolkit().getImage(
this.getClass().getResource("astTray.png"));
this.setIconImage(image);
}
@Override
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
int option = JOptionPane.showConfirmDialog(null, "您确认要关闭系统吗?",
"确认提示", JOptionPane.OK_OPTION | JOptionPane.CANCEL_OPTION);
if (option != JOptionPane.OK_OPTION) {
return;
} else {
}
} else if (e.getID() == WindowEvent.WINDOW_ICONIFIED) {//最小化时隐藏iframe,模拟隐藏任务栏图标
setVisible(false);
}
}
public static void main(String[] args) throws IOException {
AstCenterBootstrap astCenter = new AstCenterBootstrap("AstCenter系统");
astCenter.pack();
Dimension local_size = new Dimension(300, 200);
astCenter.setSize(local_size);
astCenter.setLocationRelativeTo(null);
astCenter.setVisible(true);
}
}
tray icon class
import java.awt.AWTException;
import java.awt.Frame;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class AstCenterTrayIcon implements ActionListener {
private PopupMenu pop;
private MenuItem open, close;
private TrayIcon trayicon;
private JFrame jframe;
public AstCenterTrayIcon(JFrame jframe) {
this.jframe = jframe;
initComponents();
}
private void initComponents() {
pop = new PopupMenu();
open = new MenuItem("Open");
open.addActionListener(this);
close = new MenuItem("Close");
close.addActionListener(this);
pop.add(open);
pop.add(close);
Image image = Toolkit.getDefaultToolkit().getImage(
this.getClass().getResource("astTray.png"));
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
trayicon = new TrayIcon(image, "AstCenter系统", pop);
trayicon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
showFrame();
}
}
});
try {
tray.add(trayicon);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == open) {
showFrame();
}
if (e.getSource() == close) {
jframe.dispatchEvent(new WindowEvent(jframe,
WindowEvent.WINDOW_CLOSING));
}
}
//状态切换效果
public void showFrame() {
jframe.setState(Frame.ICONIFIED);
jframe.setVisible(true);
jframe.setState(Frame.NORMAL);
}
}
分享到:
相关推荐
java_swing设置窗口居中的最佳方法
主要介绍了Java Swing实现让窗体居中显示的方法,结合实例形式分析了swing使用setBounds方法控制窗口布局的相关操作技巧,需要的朋友可以参考下
java 编写 的基于 swing 的桌面托盘
修改Javaswing窗体图标.pdf
检查当前系统是否支持系统托盘 使用的图标被缩放到合适的比例
Java Swing实现添加数据到数据库(数据库使用Mysql)并查询数据显示在界面中。
中级SWING-J中级中级SWING-JTable属性SWING-JTable属性Table属性
SWING应用程序.jar居中呈现,自动大小,及关闭设置
用Java程序读取Excel表格里的内容并显示在Swing界面里。
利用swing组件来显示系统文件的目录树,不使用JFileChooser类。
java swing GUI界面显示IE网页 根据网址跳转到相应网站
用swing 显示当前时间.利用了多线程和swing容器
使用的是Swing组件和io流; 使用上传功能获取路径(我这边的是直接指定的,一般是从页面层获取的),然后传到dao层 然后通过dao层将二进制流写到数据库中(附上数据库); 点击下载功能,从数据库中获取二...
在Swing中显示Html网页 如有疑问和问题 建议 都可以发邮件 谢谢支持。 解压密码:www.panying520.cn
swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3swing-layout-1.0.3...
swing计算器 swing计算器swing计算器 swing计算器swing计算器 swing计算器swing计算器 swing计算器swing计算器 swing计算器 简易计算器
frame.getContentPane().setLayout(null); frame.add(mediaPlayerComponent); frame.setLocation(100, 100); frame.setSize(1440, 1024); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame....
swing 教程swing 教程swing 教程swing 教程swing 教程swing 教程swing 教程swing 教程swing 教程
使用Java Swing开发的仿QQ功能,由于开发时间比较短,所以该代码作为初学者可以提供一些帮助与参考。 1、客户端展示和接收数据,服务器端与数据库进行交互处理。 2、使用Tcp的Socket。...3、实现新消息托盘图标闪烁。
Java Swing制作多行滚动歌词显示控件