Java程序托盘
java怎么写托盘程序啊?要求托盘能支持一些事件,比如说双击启动程序和右击弹出一个菜单等。最好能贴出代码谢谢。...
java怎么写托盘程序啊?要求托盘能支持一些事件,比如说双击启动程序和右击弹出一个菜单等。最好能贴出代码谢谢。
展开
1个回答
展开全部
import java.awt.Color;
import java.awt.Graphics2D;
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.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.JOptionPane;
public class TrayIconTest {
public static void main(String[] args) throws Exception{
BufferedImage img=new BufferedImage(32,32,BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g=img.createGraphics();
g.setColor(Color.yellow);
g.setClip(new java.awt.geom.Ellipse2D.Double(1,1,30,30));
g.fillRect(0, 0, 32,32);
g.dispose();
// BufferedImage img=javax.imageio.ImageIO.read(new java.net.URL("file:///d:/logo.gif"));
TrayIcon ti=new TrayIcon(img);
ti.setToolTip("TrayIcon test");
ti.setImageAutoSize(true);
SystemTray.getSystemTray().add(ti);
ActionListener al=new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
if("exit".equals(e.getActionCommand()))
System.exit(0);
}
};
PopupMenu pm=new PopupMenu();
MenuItem mi=new MenuItem("a");
mi.addActionListener(al);
pm.add(mi);
mi=new MenuItem("exit");
mi.addActionListener(al);
pm.add(mi);
ti.setPopupMenu(pm);
ti.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2){
System.out.println("doubleClicked.");
JOptionPane.showMessageDialog(null,"Hi!");
}
}
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed");
}
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased");
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
});
}
}
import java.awt.Graphics2D;
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.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.JOptionPane;
public class TrayIconTest {
public static void main(String[] args) throws Exception{
BufferedImage img=new BufferedImage(32,32,BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g=img.createGraphics();
g.setColor(Color.yellow);
g.setClip(new java.awt.geom.Ellipse2D.Double(1,1,30,30));
g.fillRect(0, 0, 32,32);
g.dispose();
// BufferedImage img=javax.imageio.ImageIO.read(new java.net.URL("file:///d:/logo.gif"));
TrayIcon ti=new TrayIcon(img);
ti.setToolTip("TrayIcon test");
ti.setImageAutoSize(true);
SystemTray.getSystemTray().add(ti);
ActionListener al=new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
if("exit".equals(e.getActionCommand()))
System.exit(0);
}
};
PopupMenu pm=new PopupMenu();
MenuItem mi=new MenuItem("a");
mi.addActionListener(al);
pm.add(mi);
mi=new MenuItem("exit");
mi.addActionListener(al);
pm.add(mi);
ti.setPopupMenu(pm);
ti.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2){
System.out.println("doubleClicked.");
JOptionPane.showMessageDialog(null,"Hi!");
}
}
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed");
}
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased");
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
});
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询