Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Image;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class Main {
    public static void main(String[] args) throws Exception {
        JFrame frm = new JFrame("Main");
        Image im = Toolkit.getDefaultToolkit().getImage("c:\\icons\\icon1.png");
        TrayIcon tri = new TrayIcon(im);
        tri.addActionListener(e -> {
            frm.setVisible(true);
            try {
                SystemTray.getSystemTray().remove(tri);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });
        frm.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                try {
                    SystemTray.getSystemTray().add(tri);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                frm.setVisible(false);
            }
        });
        frm.setSize(100, 100);
        frm.setVisible(true);
    }
}