在托盘图标中运行应用程序(而不是在任务栏中)

huangapple go评论65阅读模式
英文:

Run Application in tray icon(not in taskbar)

问题

我想将我的应用程序在托盘中运行,但是我不想在任务栏中看到图标。
我在使用 Hide_On_Close 操作时使用了我的 JFrame。
我还在使用托盘图标来进行通知,但是在我的 GUI 应用程序运行时如何摆脱任务栏中的图标。

英文:

I want to run my app in the tray but I don't the icon in the taskbar.

I using my JFrame in Hide_On_Close operation

and I using a tray icon for notifications but how to get rid of the icon on the taskbar while my GUI application running.

答案1

得分: 1

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
public class SysTrayFrame {

    public static void main(String[] args) throws Exception{
        JFrame frame = new JFrame("systray test");
        frame.setUndecorated(true);
        BufferedImage img = new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB);
        Graphics g = img.getGraphics();
        g.setColor(Color.RED);
        g.fillOval(0, 0, 64, 64);
        g.dispose();
        JButton b = new JButton("click to hide");
        frame.add(b);
        b.addActionListener(evt -> frame.setVisible(false));
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        TrayIcon icon = new TrayIcon(img);
        icon.addActionListener(evt -> {
            System.out.println("doing it");
            frame.setVisible(true);
        });
        SystemTray.getSystemTray().add(icon);
    }

}
英文:

Does this work for you? You might need to add what os you're working on.

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
public class SysTrayFrame{

    public static void main(String[] args) throws Exception{
        JFrame frame = new JFrame("systray test");
        frame.setUndecorated(true);
        BufferedImage img =  new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB);
        Graphics g = img.getGraphics();
        g.setColor(Color.RED);
        g.fillOval(0, 0, 64, 64);
        g.dispose();
        JButton b = new JButton("click to hide");
        frame.add(b);
        b.addActionListener(evt-> frame.setVisible(false) );
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        TrayIcon icon = new TrayIcon(img);
        icon.addActionListener( evt->{
            System.out.println("doing it");
            frame.setVisible(true);
        });
        SystemTray.getSystemTray().add(icon);
    }

}

huangapple
  • 本文由 发表于 2020年9月18日 20:20:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63955681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定