对于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)带有runnable接口的嵌套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
不受欢迎。
答案 0 :(得分: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();