0

有一个用于 java 桌面应用程序的类托盘,我必须更新打开弹出窗口时使用的磁盘配额的值

1) 托盘类

    public class Tray implements Observer {

        static Logger log = Logger.getLogger(Tray.class);
        static WDServer wds;
        static StatusWindow sw;
        boolean connected;
        TrayIcon trayIcon;
        String volSize = "";

        public Tray() {

            log.info("installing system tray menu");

            if (SystemTray.isSupported()) {

                TrayUtils tu = new TrayUtils(1000);

                final SystemTray sysTray = SystemTray.getSystemTray();
                final PopupMenu popup = new PopupMenu();

                MenuItem quotaDisk = new MenuItem(tu.getLabelDisk());
                popup.add(quotaDisk);
                popup.addSeparator();
...

2) 具有可运行接口的嵌套 TrayUtils 类

public class TrayUtils extends Thread implements Runnable {

        long delay;
        long bytes;
        String labelDisk = "";

        public TrayUtils(long delay) {
            this.delay = delay;
        }

        @Override
        public void run() {
            while (true) {
                try {
                    setBytes(FileUtils.sizeOfDirectory(new File(System.getProperty("user.home") + "/.ubi/data")));
                    Thread.sleep(this.delay);
                } catch (InterruptedException e) {
                    log.error("error checking disk space used");
                }

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        setLabelDisk("Storage Used: " + FileUtils.byteCountToDisplaySize(getBytes()));
                    }
                });
            }

        }

<getters & setters here>
    }

为什么我在弹窗中看不到存储状态?实现这些功能的正确设计是什么?

我必须使用awt,所以JLabel这里不受欢迎。

4

1 回答 1

1

感谢 PM,javax.swing.Timer 完成了这项工作:

// Update space used every 1s
            ActionListener taskQuota = new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    quotaDisk.setLabel("Storage used: " + checkVolSize());
                }
            };
            new Timer(1000, taskQuota).start();
于 2013-11-13T00:18:18.870 回答