zoukankan      html  css  js  c++  java
  • Java单击图标实现显示Frame界面

    如何实现在Java中实现把图片作为图标并显示在系统托盘中?下面展示做法:

    首先我们应该把图片导入工程你的存储路径:

     

    在这之后我们可以直接复制文件名并导入到程序语句中: private Image image = Toolkit.getDefaultToolkit().createImage("Chrysanthemum.jpg");

    下面贴出代码:

    package Test1;
    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.awt.SystemTray;
    import java.awt.TrayIcon;
    import javax.swing.*;
    public class ActionEvent4 extends JFrame implements ActionListener
    {
      private Image image = Toolkit.getDefaultToolkit().createImage("Chrysanthemum.jpg");//这里你得//改一下
      private TrayIcon tl = new TrayIcon(image);
        public ActionEvent4() throws AWTException 
        {
         super( "单击了托盘图标" );
         SystemTray.getSystemTray().add(tl);
         tl.addActionListener(this);
         tl.addMouseListener(new MouseAdapter() {
             @Override
             public void mouseClicked(MouseEvent e) {
                    showView();
             }
         });
        }
        private void showView(){    
           ActionEvent4.this.setVisible(true);
            ActionEvent4.this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            ActionEvent4.this.setSize(400, 300);
      }
      public static void main(String[] args) throws AWTException {
       // TODO Auto-generated method stub
       new ActionEvent4();
      }
    @Override
      public void actionPerformed(ActionEvent e) {
      // TODO Auto-generated method stub
       if ( e.getSource() == tl ){
         this.setVisible(true);
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         this.setSize(400,300);
         }
       }
    }
  • 相关阅读:
    Spring_7_AOP之Advice应用
    JAVA豆知识
    SPRING事务_2
    JSP_5_JavaBean
    Spring事务_1
    java基本类型和包装类型
    SVN使用教程总结
    通过反射来创建对象?getConstructor()和getDeclaredConstructor()区别?
    Java泛型中extends和super的区别?
    数字签名、数字证书、对称加密算法、非对称加密算法、单向加密(散列算法)
  • 原文地址:https://www.cnblogs.com/khbcsu/p/4199047.html
Copyright © 2011-2022 走看看