gpt4 book ai didi

java - 更新托盘中的标签

转载 作者:行者123 更新时间:2023-12-01 13:51:18 25 4
gpt4 key购买 nike

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

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) 具有可运行接口(interface)的嵌套 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

最佳答案

感谢 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();

关于java - 更新托盘中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19940560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com