sun4love 发表于 2013-2-3 11:23:12

swing托盘图标、frame居中显示

 
 
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);}@Overrideprotected 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);}} 
 
 
 
 
 
 
 
 
 
 
 
 
页: [1]
查看完整版本: swing托盘图标、frame居中显示