자바 생 성 시스템 트 레이 구현(swt 와 jdk 6 각각 구현)

jdk 6 이전 에는 지원 되 지 않 았 기 때 문 입 니 다.
그래서 swt 를 사용 합 니 다.
jdk 6 는 jdk 6 버 전 을 사용 하 는 것 을 권장 합 니 다.그것 이 야 말로 진정한 크로스 플랫폼 이기 때 문 입 니 다.
swt

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

public class TrayBySwt {

	public static void main(String[] args) {
		Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setText("    BySwt");
		shell.setLayout (new FillLayout());
		Button button = new Button(shell, SWT.PUSH);
		button.setText("          ");
		
		ImageData imageData = new ImageData("E:\\workspace\\myeclipse66\\ra\\Tray\\src\\16.gif");
		Image image = new Image(display, imageData);
		//      
		final Tray tray = display.getSystemTray();
		if (tray == null) {
			System.out.println("             ");
		} else {
			final TrayItem item = new TrayItem(tray, SWT.NONE);
			
			item.setToolTipText("      ");
			item.addListener (SWT.Show, new Listener () {
			public void handleEvent (Event event) {
				System.out.println("show");
			}
			});
			item.addListener (SWT.Hide, new Listener () {
				public void handleEvent (Event event) {
					System.out.println("hide");
				}
			});
			item.addListener (SWT.Selection, new Listener () {
				public void handleEvent (Event event) {
					System.out.println("selection");
				}
			});
		
			final Menu menu = new Menu (shell, SWT.POP_UP);
			for (int i = 0; i < 8; i++) {
				MenuItem mi = new MenuItem (menu, SWT.PUSH);
				mi.setText ("Item" + i);
				mi.addListener (SWT.Selection, new Listener () {
					public void handleEvent (Event event) {
						System.out.println("selection " + event.widget);
					}
				});
				if (i == 0) menu.setDefaultItem(mi);
			}
			item.addListener (SWT.MenuDetect, new Listener () {
				public void handleEvent (Event event) {
					menu.setVisible (true);
				}
			});

			item.addListener(SWT.DefaultSelection, new Listener() {
				public void handleEvent(Event event) {
					shell.setVisible(true);
					item.setVisible(false);
				}
			});
			item.setImage(image);
			button.addListener(SWT.Selection, new Listener(){
				public void handleEvent(Event event) {
					shell.setVisible(false);
					item.setVisible(true);
				}
			});
			item.setVisible(false);
		}
		
		shell.setBounds(50, 50, 300, 200);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		image.dispose();
		display.dispose();
	}
}


jdk6


import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
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 javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

/**
 *
 * @author Mr.LiuTao
 */
public class TrayByJdk extends JFrame {

    private JPanel pane = null;
    private JButton button = null; //          
    private JLabel label = null; //                
    private TrayIcon trayIcon = null; //     
    private SystemTray tray = null; //           
    //  jdk1.6              
    public TrayByJdk() {
        super("      ");
        try {
            //  LookAndFeel   Windows  
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        pane = new JPanel();
        button = new JButton("     ");
        button.setEnabled(false);
        label = new JLabel("          ");
        pane.add(label);
        pane.add(button);
        //        
        if (SystemTray.isSupported()) {
            this.tray();
        }
        this.getContentPane().add(pane);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(300, 200);
        this.setVisible(true);
    }

    /**
     *       
     */
    private void tray() {
        label.setText("         ");
        button.setEnabled(true);
        tray = SystemTray.getSystemTray(); //             
        //ImageIcon icon = new ImageIcon("tray.gif"); //            
        ImageIcon icon = new ImageIcon("E:\\workspace\\myeclipse66\\ra\\Tray\\src\\16.gif"); //            
        PopupMenu pop = new PopupMenu(); //            
        MenuItem show = new MenuItem("    ");
        MenuItem exit = new MenuItem("    ");
        MenuItem author = new MenuItem("Author");
        /**
         * TrayIcon     
         * TrayIcon(Image image)  “  ”   
         * TrayIcon(Image image, String tooltip)  “  ” “ToolTip”  
         * TrayIcon(Image image, String tooltip, PopupMenu popup)  “  ”,“ToolTip”,“    ”         
         */
        trayIcon = new TrayIcon(icon.getImage(), "      ", pop);
        //            ,              
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    tray.add(trayIcon); //                 
                    setVisible(false); //       
                } catch (AWTException ex) {
                    ex.printStackTrace();
                }
            }
        });
        /**
         *        ,            ,      
         */
        trayIcon.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) { //     
                    tray.remove(trayIcon); //                
                    setVisible(true); //     
                }
            }
        });
        show.addActionListener(new ActionListener() { //   “    ”          

            public void actionPerformed(ActionEvent e) {
                tray.remove(trayIcon); //                
                setVisible(true); //     
            }
        });
        exit.addActionListener(new ActionListener() { //   “    ”       

            public void actionPerformed(ActionEvent e) {
                System.exit(0); //     
            }
        });
        author.addActionListener(new ActionListener() { //   “    ”       

            public void actionPerformed(ActionEvent e) {
                showMessage();
            }
        });
        pop.add(show);
        pop.add(exit);
        pop.add(author);
    }

    /**
     *     
     */
    private void showMessage() {
        JOptionPane.showMessageDialog(this, new JLabel("        "), "  ", JOptionPane.INFORMATION_MESSAGE);
    }

    public static void main(String[] args) {
        new TrayByJdk();
    }
}


좋은 웹페이지 즐겨찾기