当前位置: 首页 > 面试题库 >

闪烁托盘图标

鱼志诚
2023-03-14
问题内容

我知道如何使用java在窗口的系统托盘中放置图标,但是执行系统托盘图标 闪烁
的最佳方法是什么?或者如果我可以不时替换图标或在某个事件(应用程序运行时)替换图标,请提前分享您的经验,谢谢


问题答案:
  • 在某些情况下,以输出闪烁的图标更改图标没有问题

  • 创建一个Arrays of BufferedImage或队列

  • 在所需事件上启动Swing计时器,并在一段时间内更改BufferedImage或图标

  • 并停止Swing Timer一段时间后残留或添加ActionListenerMessage,另一种方式可以是确定LEFTRIGHT从鼠标按钮MouseListener(用于IconSystem Tray)一个用于停止Timer,对于第二JPopup

编辑

例如,正如我提到的,您可以添加ActionListener或MouseListener,在那里可以停止Swing
Timer

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
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.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;

public class ActiveTray {

    private SystemTray tray;
    private TrayIcon trayIcon;
    private Icon icon, icon1;
    private Image image, image1;
    private Timer timer;

    public ActiveTray() {
        if (SystemTray.isSupported() == false) {
            System.err.println("No system tray available");
            return;
        }
        tray = SystemTray.getSystemTray();
        PropertyChangeListener propListener = new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent evt) {
                TrayIcon oldTray[] = (TrayIcon[]) evt.getOldValue();
                TrayIcon newTray[] = (TrayIcon[]) evt.getNewValue();
                System.out.println(oldTray.length + " / " + newTray.length);
            }
        };
        tray.addPropertyChangeListener("trayIcons", propListener);
        icon = new BevelArrowIcon(BevelArrowIcon.UP, false, false);
        image = iconToImage(icon);
        icon1 = new BevelArrowIcon(BevelArrowIcon.DOWN, false, false);
        image1 = iconToImage(icon1);
        PopupMenu popup = new PopupMenu();
        MenuItem item = new MenuItem("Hello, World");
        trayIcon = new TrayIcon(image, "Tip Text", popup);
        ActionListener menuActionListener = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                trayIcon.displayMessage("Good-bye", "Cruel World",
                        TrayIcon.MessageType.WARNING);
            }
        };
        item.addActionListener(menuActionListener);
        popup.add(item);
        ActionListener actionListener = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                tray.remove(trayIcon);
            }
        };
        trayIcon.addActionListener(actionListener);
        try {
            tray.add(trayIcon);
            start();
        } catch (AWTException ex) {
            Logger.getLogger(ActiveTray.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void start() {
        timer = new javax.swing.Timer(125, updateCol());
        timer.start();
        trayIcon.displayMessage(null, "  Aplication Loaded  ", TrayIcon.MessageType.NONE);
    }

    private Action updateCol() {
        return new AbstractAction("Icon load action") {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                Runnable doRun = new Runnable() {

                    @Override
                    public void run() {
                        Image img = trayIcon.getImage();
                        if (img == image) {
                            trayIcon.setImage(image1);
                        } else {
                            trayIcon.setImage(image);
                        }
                    }
                };
                SwingUtilities.invokeLater(doRun);
            }
        };
    }

    public static void main(String args[]) {
        ActiveTray activeTray = new ActiveTray();
    }

    static Image iconToImage(Icon icon) {
        if (icon instanceof ImageIcon) {
            return ((ImageIcon) icon).getImage();
        } else {
            int w = icon.getIconWidth();
            int h = icon.getIconHeight();
            GraphicsEnvironment ge =
                    GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gd = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            BufferedImage image = gc.createCompatibleImage(w, h);
            Graphics2D g = image.createGraphics();
            icon.paintIcon(null, g, 0, 0);
            g.dispose();
            return image;
        }
    }

    static class BevelArrowIcon implements Icon {

        public static final int UP = 0;         // direction
        public static final int DOWN = 1;
        private static final int DEFAULT_SIZE = 16;
        private Color edge1;
        private Color edge2;
        private Color fill;
        private int size;
        private int direction;

        public BevelArrowIcon(int direction, boolean isRaisedView,
                boolean isPressedView) {
            if (isRaisedView) {
                if (isPressedView) {
                    init(UIManager.getColor("controlLtHighlight"),
                            UIManager.getColor("controlDkShadow"),
                            UIManager.getColor("controlShadow"),
                            DEFAULT_SIZE, direction);
                } else {
                    init(UIManager.getColor("controlHighlight"),
                            UIManager.getColor("controlShadow"),
                            UIManager.getColor("control"),
                            DEFAULT_SIZE, direction);
                }
            } else {
                if (isPressedView) {
                    init(UIManager.getColor("controlDkShadow"),
                            UIManager.getColor("controlLtHighlight"),
                            UIManager.getColor("controlShadow"),
                            DEFAULT_SIZE, direction);
                } else {
                    init(UIManager.getColor("controlShadow"),
                            UIManager.getColor("controlHighlight"),
                            UIManager.getColor("control"),
                            DEFAULT_SIZE, direction);
                }
            }
        }

        public BevelArrowIcon(Color edge1, Color edge2, Color fill,
                int size, int direction) {
            init(edge1, edge2, fill, size, direction);
        }

        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            switch (direction) {
                case DOWN:
                    drawDownArrow(g, x, y);
                    break;
                case UP:
                    drawUpArrow(g, x, y);
                    break;
            }
        }

        @Override
        public int getIconWidth() {
            return size;
        }

        @Override
        public int getIconHeight() {
            return size;
        }

        private void init(Color edge1, Color edge2, Color fill,
                int size, int direction) {
            edge1 = Color.red;
            edge2 = Color.blue;
            this.edge1 = edge1;
            this.edge2 = edge2;
            this.fill = fill;
            this.size = size;
            this.direction = direction;
        }

        private void drawDownArrow(Graphics g, int xo, int yo) {
            g.setColor(edge1);
            g.drawLine(xo, yo, xo + size - 1, yo);
            g.drawLine(xo, yo + 1, xo + size - 3, yo + 1);
            g.setColor(edge2);
            g.drawLine(xo + size - 2, yo + 1, xo + size - 1, yo + 1);
            int x = xo + 1;
            int y = yo + 2;
            int dx = size - 6;
            while (y + 1 < yo + size) {
                g.setColor(edge1);
                g.drawLine(x, y, x + 1, y);
                g.drawLine(x, y + 1, x + 1, y + 1);
                if (0 < dx) {
                    g.setColor(fill);
                    g.drawLine(x + 2, y, x + 1 + dx, y);
                    g.drawLine(x + 2, y + 1, x + 1 + dx, y + 1);
                }
                g.setColor(edge2);
                g.drawLine(x + dx + 2, y, x + dx + 3, y);
                g.drawLine(x + dx + 2, y + 1, x + dx + 3, y + 1);
                x += 1;
                y += 2;
                dx -= 2;
            }
            g.setColor(edge1);
            g.drawLine(xo + (size / 2), yo + size - 1, xo
                    + (size / 2), yo + size - 1);
        }

        private void drawUpArrow(Graphics g, int xo, int yo) {
            g.setColor(edge1);
            int x = xo + (size / 2);
            g.drawLine(x, yo, x, yo);
            x--;
            int y = yo + 1;
            int dx = 0;
            while (y + 3 < yo + size) {
                g.setColor(edge1);
                g.drawLine(x, y, x + 1, y);
                g.drawLine(x, y + 1, x + 1, y + 1);
                if (0 < dx) {
                    g.setColor(fill);
                    g.drawLine(x + 2, y, x + 1 + dx, y);
                    g.drawLine(x + 2, y + 1, x + 1 + dx, y + 1);
                }
                g.setColor(edge2);
                g.drawLine(x + dx + 2, y, x + dx + 3, y);
                g.drawLine(x + dx + 2, y + 1, x + dx + 3, y + 1);
                x -= 1;
                y += 2;
                dx += 2;
            }
            g.setColor(edge1);
            g.drawLine(xo, yo + size - 3, xo + 1, yo + size - 3);
            g.setColor(edge2);
            g.drawLine(xo + 2, yo + size - 2, xo + size - 1, yo + size - 2);
            g.drawLine(xo, yo + size - 1, xo + size, yo + size - 1);
        }
    }
}


 类似资料:
  • 这是因为在开发模式下,为了通过 Webpack 实现热加载,CSS代码是打包在 JavaScript 代码中,并动态打到页面中去,从而元素重绘引起了闪烁。 不用担心,在生产模式下,CSS代码会单独打包至独立的文件并置于head标签内,不会出现页面闪烁的现象。

  • 我在Lollipop上的共享元素转换中看到了奇怪的事情。共享元素在开始动画之前闪烁(请看视频https://www.youtube.com/watch?v=DCoyyC_S-9A) 我不知道为什么会这样。但是,当我添加

  • 我试图用pyplay制作一个游戏,我几乎完成了,但我想让被画在墙上的盒子不闪烁,这些红色的盒子在整个游戏中闪烁,我不想让它们闪烁,最后,我在一个if条件下调用player碰撞函数,在这里每当我制作新的碰撞器时,我每次都要在if条件下添加函数,我想要的是碰撞器对象自动调用这个函数,而不需要我在if语句中为碰撞器的每个实例调用它对象。请指导我如何这样做。

  • 减低闪烁    以Interlace(交错扫描)方式在电视机输出PSP™规格软件的影像时,设定是否要减低画面的闪烁。 关 不减低画面的闪烁。 开 减低画面的闪烁。

  • 我最近一直在练习Java的swing特性,在我的一个扩展JPanel类的类中,我重写了方法,以便它将我的BufferedImage绘制到JPanel上。我也有一个方法在上面移动。在这个问题之前,我有一个问题,显示了移动的过程,因为它重新油漆太快。因此,我创建了一个名为的布尔变量,当图像仍在移动过程中时,该变量被设置为false。但是,现在我看到屏幕把整个图像拿走并放回原处,导致它眨眼。下面是我的基

  • 我创建小应用程序来显示我的问题。您可以在https://github.com/anton111111/exampleglideblink中看到它。 当我调用RecyerView适配器上的notifyItemChanged时,我只需要更改文本(在我的示例中,它使用id r.id.progress EditText)和图像,而不需要更改。但它会眨眼。 我有能力不眨眼地更改文本吗?