【问题标题】:App icon not showing up despite app running successfully尽管应用程序成功运行,但应用程序图标未显示
【发布时间】:2021-01-20 10:29:24
【问题描述】:

我有一个 Java 桌面应用程序,每次运行时它的图标都不显示。该应用程序可能会成功运行,但图标不会显示。 我的前辈暗示我这可能是我们使用的图书馆的问题。我在互联网上查找并找不到任何东西。 如果有人知道如何解决此问题,请联系。

我想添加新的库来测试它的功能但找不到。目前 dorkbox.SystemTray 和 dorkbox.Util 是使用中的两个库。

是否有任何不同的库可以尝试或任何方法。

以下是我的代码 我就是这样初始化SystemTray

    public static SystemTray systemTray;

这是显示SystemTray的代码

public static void showTray() throws Exception {
    try {
  
    if (systemTray == null) {
     
        System.exit(0);
    }
  
    System.out.println(SystemTray.DEBUG);
    SystemTray.DEBUG=true;
    CacheUtil.clear();
    SystemTray.FORCE_GTK2=true;
    systemTray.setImage("/usr/local/bin/Desklog-Client/nonet.png");
 
       

【问题讨论】:

  • 你在使用java-gnomedorkbox/SystemTray吗?
  • dorkbox/系统托盘?你的意思是图书馆是对的。是的,我正在使用它。

标签: java system-tray tray


【解决方案1】:

此处提供了完整的工作示例(也用于验证和测试):https://github.com/dorkbox/SystemTray/blob/master/test/dorkbox/TestTray.java

我还内嵌了该文件的源代码,以防它消失。

    /*
     * Copyright 2021 dorkbox, llc
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    package dorkbox;
    
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.net.URL;
    import java.util.Random;
    
    import dorkbox.os.OS;
    import dorkbox.systemTray.Checkbox;
    import dorkbox.systemTray.Menu;
    import dorkbox.systemTray.MenuItem;
    import dorkbox.systemTray.Separator;
    import dorkbox.systemTray.SystemTray;
    import dorkbox.util.CacheUtil;
    import dorkbox.util.Desktop;
    
    /**
     * Icons from 'SJJB Icons', public domain/CC0 icon set
     */
    public
    class TestTray {
    
        public static final URL BLUE_CAMPING = TestTray.class.getResource("accommodation_camping.glow.0092DA.32.png");
        public static final URL BLACK_FIRE = TestTray.class.getResource("amenity_firestation.p.000000.32.png");
    
        public static final URL BLACK_MAIL = TestTray.class.getResource("amenity_post_box.p.000000.32.png");
        public static final URL GREEN_MAIL = TestTray.class.getResource("amenity_post_box.p.39AC39.32.png");
    
        public static final URL BLACK_BUS = TestTray.class.getResource("transport_bus_station.p.000000.32.png");
        public static final URL LT_GRAY_BUS = TestTray.class.getResource("transport_bus_station.p.999999.32.png");
    
        public static final URL BLACK_TRAIN = TestTray.class.getResource("transport_train_station.p.000000.32.png");
        public static final URL GREEN_TRAIN = TestTray.class.getResource("transport_train_station.p.39AC39.32.png");
        public static final URL LT_GRAY_TRAIN = TestTray.class.getResource("transport_train_station.p.666666.32.png");
    
        // from issue 123
        public static final URL NOTIFY_IMAGE = TestTray.class.getResource("RemoteNotifications.png");
    
        public static
        void main(String[] args) {
            // make sure JNA jar is on the classpath!
            new TestTray();
        }
    
        private final SystemTray systemTray;
        private final ActionListener callbackGray;
    
        public
        TestTray() {
            SystemTray.DEBUG = true; // for test apps, we always want to run in debug mode
    //        SystemTray.FORCE_TRAY_TYPE = SystemTray.TrayType.Swing;
    
            // for test apps, make sure the cache is always reset. These are the ones used, and you should never do this in production.
            CacheUtil.clear("SysTrayExample");
    
            // SwingUtil.setLookAndFeel(null); // set Native L&F (this is the System L&F instead of CrossPlatform L&F)
            // SystemTray.SWING_UI = new CustomSwingUI();
    
            this.systemTray = SystemTray.get("SysTrayExample");
            if (systemTray == null) {
                throw new RuntimeException("Unable to load SystemTray!");
            }
    
            // SWT/JavaFX "shutdown hooks" have changed. Since it's no longer available with JPMS, it is no longer supported.
            // Developers must add the shutdown hooks themselves.
    
            systemTray.setTooltip("Mail Checker");
            systemTray.setImage(LT_GRAY_TRAIN);
            systemTray.setStatus("No Mail");
    
            callbackGray = e->{
                final MenuItem entry = (MenuItem) e.getSource();
                systemTray.setStatus(null)
                          .setImage(BLACK_TRAIN);
    
                entry.setCallback(null);
    //                systemTray.setStatus("Mail Empty");
                systemTray.getMenu().remove(entry);
                entry.remove();
                System.err.println("POW");
            };
    
    
            Menu mainMenu = systemTray.getMenu();
    
            MenuItem greenEntry = new MenuItem("Green Mail", e->{
                final MenuItem entry = (MenuItem) e.getSource();
                systemTray.setStatus("Some Mail!");
                systemTray.setImage(GREEN_TRAIN);
    
                entry.setCallback(callbackGray);
                entry.setImage(BLACK_MAIL);
                entry.setText("Delete Mail");
                entry.setTooltip(null); // remove the tooltip
    //                systemTray.remove(menuEntry);
            });
            greenEntry.setImage(GREEN_MAIL);
            // case does not matter
            greenEntry.setShortcut('G');
            greenEntry.setTooltip("This means you have green mail!");
            mainMenu.add(greenEntry);
    
    
            Checkbox checkbox = new Checkbox("Euro € Mail", e->System.err.println("Am i checked? " + ((Checkbox) e.getSource()).getChecked()));
            checkbox.setShortcut('€');
            mainMenu.add(checkbox);
    
            MenuItem removeTest = new MenuItem("This should not be here", e->{
                try {
                    Desktop.browseURL("https://git.dorkbox.com/dorkbox/SystemTray");
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            });
            mainMenu.add(removeTest);
            mainMenu.remove(removeTest);
    
            mainMenu.add(new Separator());
    
            mainMenu.add(new MenuItem("About", e->{
                try {
                    Desktop.browseURL("https://git.dorkbox.com/dorkbox/SystemTray");
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }));
    
    
            mainMenu.add(new MenuItem("Temp Directory", e->{
                try {
                    Desktop.browseDirectory(OS.TEMP_DIR.getAbsolutePath());
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }));
    
            mainMenu.add(new MenuItem("Notify", e->{
                final MenuItem entry = (MenuItem) e.getSource();
                systemTray.setStatus("Notification!");
                systemTray.setImage(NOTIFY_IMAGE);
    
                entry.setImage(NOTIFY_IMAGE);
                entry.setText("Did notify");
                System.err.println("NOTIFICATION!");
            }));
    
    
            Menu submenu = new Menu("Options", BLUE_CAMPING);
            submenu.setShortcut('t');
    
    
            MenuItem disableMenu = new MenuItem("Disable menu", BLACK_BUS, e->{
                MenuItem source = (MenuItem) e.getSource();
                source.getParent().setEnabled(false);
            });
            submenu.add(disableMenu);
    
    
            submenu.add(new MenuItem("Hide tray", LT_GRAY_BUS, e->systemTray.setEnabled(false)));
            submenu.add(new MenuItem("Remove menu", BLACK_FIRE, e->{
                MenuItem source = (MenuItem) e.getSource();
                source.getParent().remove();
            }));
    
            submenu.add(new MenuItem("Add new entry to tray",
                                     e->systemTray.getMenu().add(new MenuItem("Random " + new Random().nextInt(10)))));
            mainMenu.add(submenu);
    
            MenuItem entry = new MenuItem("Type: " + systemTray.getType().toString());
            entry.setEnabled(false);
            systemTray.getMenu().add(entry);
    
            systemTray.getMenu().add(new MenuItem("Quit", e->{
                systemTray.shutdown();
                //System.exit(0);  not necessary if all non-daemon threads have stopped.
            })).setShortcut('q'); // case does not matter
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2021-08-23
    • 2020-08-09
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多