在 Windows 的系统托盘中隐藏我的程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6563141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:17:02  来源:igfitidea点击:

Hiding my program in the System Tray in Windows

javawindowsswingsystem-tray

提问by Adam Smith

I'd like my program to display an icon in the TaskBar Status Area near the clock in Windows and found a way to do so.

我希望我的程序在 Windows 时钟附近的任务栏状态区域中显示一个图标,并找到了一种方法。

The thing is, I'd like my program to stay open in the Status Area if the "X" is pressed on the window, but not in the System Tray, but I have no idea how to do so and searching on Google didn't help (I'm probably not searching the right terms).

问题是,如果在窗口上按下“X”而不是在系统托盘中,我希望我的程序在状态区域中保持打开状态,但我不知道如何这样做并且在 Google 上搜索没有t帮助(我可能没有搜索正确的术语)。

Edit:I think I used the wrong terms. I know how to have my program's icon in the notification area, what I'd like is to hide it in the area where it is normally displayed when you minimize a window.

编辑:我想我使用了错误的术语。我知道如何在通知区域中显示我的程序图标,我想要的是将它隐藏在最小化窗口时通常显示的区域中。

采纳答案by camickr

I know how to have my program's icon in the notification area, what I'd like is to hide it in the area where it is normally displayed when you minimize a window.

我知道如何在通知区域中显示我的程序图标,我想要的是将它隐藏在最小化窗口时通常显示的区域中。

Then don't use the system tray.

然后不要使用系统托盘。

The thing is, I'd like my program to stay open in the Status Area if the "X" is pressed on the window,

问题是,如果在窗口上按下“X”,我希望我的程序在状态区域中保持打开状态,

frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

回答by Vivek Goel

System Tray support was added in JavaSE v 6.

JavaSE v 6 中添加了系统托盘支持。

you can see example here http://download.oracle.com/javase/7/docs/api/java/awt/SystemTray.html

你可以在这里看到例子 http://download.oracle.com/javase/7/docs/api/java/awt/SystemTray.html

回答by sadipan

import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;

public class HideToSystemTray extends JFrame {
    TrayIcon trayIcon;
    SystemTray tray;
    JButton button;

    HideToSystemTray() {
        super("SystemTray test");
        button = new JButton("Press");
        button.setBounds(10, 10, 40, 40);
        setUndecorated(true);
        getContentPane().add(button);
        System.out.println("creating instance");
        try {
            System.out.println("setting look and feel");
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.out.println("Unable to set LookAndFeel");
        }
        if (SystemTray.isSupported()) {
            System.out.println("system tray supported");
            tray = SystemTray.getSystemTray();

            Image image = Toolkit.getDefaultToolkit().getImage("C:\Users\Sandipan\Desktop\cutter.png");
            ActionListener exitListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Exiting....");
                    System.exit(0);
                }
            };
            PopupMenu popup = new PopupMenu();
            MenuItem defaultItem = new MenuItem("Exit");
            defaultItem.addActionListener(exitListener);
            popup.add(defaultItem);
            defaultItem = new MenuItem("Open");
            defaultItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    tray.remove(trayIcon);
                    setVisible(true);
                    System.out.println("Tray icon removed");
                }
            });
            popup.add(defaultItem);
            trayIcon = new TrayIcon(image, "SystemTray Demo", popup);
            trayIcon.setImageAutoSize(true);
        } else {
            System.out.println("system tray not supported");
        }

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //Execute when button is pressed
                System.out.println("You clicked the button");
                try {
                    tray.add(trayIcon);
                    setVisible(false);
                    System.out.println("added to SystemTray");
                } catch (AWTException ex) {
                    System.out.println("unable to add to tray");
                }
            }
        });


    /*  addWindowStateListener(new WindowStateListener() { 
    public void windowStateChanged(WindowEvent e) { 
        if(e.getNewState()==ICONIFIED){ 
            try { 
                tray.add(trayIcon); 
                setVisible(false); 
                System.out.println("added to SystemTray"); 
            } catch (AWTException ex) { 
                System.out.println("unable to add to tray"); 
            } 
        } 
        if(e.getNewState()==7){ 
            try{ 
                tray.add(trayIcon); 
                setVisible(false); 
                System.out.println("added to SystemTray"); 
            }catch(AWTException ex){ 
                System.out.println("unable to add to system tray"); 
            } 
        } 
        if(e.getNewState()==MAXIMIZED_BOTH){ 
            tray.remove(trayIcon); 
            setVisible(true); 
            System.out.println("Tray icon removed"); 
        } 
        if(e.getNewState()==NORMAL){ 
            tray.remove(trayIcon); 
            setVisible(true); 
            System.out.println("Tray icon removed"); 
        } 
    } 
    }); */
    setIconImage(Toolkit.getDefaultToolkit().getImage("C:\Users\Sandipan\Desktop\cutter.png"));

    setVisible(true);
    setSize(300, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

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

回答by David Heffernan

I guess you want the taskbar button to be removed when you minimise the main form. You achieve this by petting its visible property to false, however you do that with your Java framework.

我猜您希望在最小化主窗体时删除任务栏按钮。您可以通过将其可见属性设置为 false 来实现这一点,但是您可以使用 Java 框架来实现这一点。