监控硬盘空间...如何实时显示信息?
我有一个带有标签文本的java应用程序,并显示实际剩余的硬盘空间。我创建了另一个类,一个“可运行”类,它每 X 秒读取一次 HDD 上剩余的空间。
现在我的问题是:如何在 JFrame 的“静态”标签中显示该信息?我认为应该起作用的最接近的事情是在“runneable”类中创建一个返回“double”数字的函数,并在 JFrame
中实例化该类,但看起来 JFrame 仅运行一次行并且代码中没有更新。
我怎样才能监控这个?我也想监控其他变量,但这只是开始。
代码:读取硬盘空间
package gwcontrol;
import java.io.File;
public class Analisis implements Runnable{
long delay;
File file = new File("C:\\");
float FreeSpace;
float TotalSpace;
public Analisis(long delay){
this.delay = delay;
//setDaemon(true);
}
public void run(){
try {
while (true) {
this.FreeSpace = (float) file.getFreeSpace() / 1024 / 1024 / 1024;
System.out.println("Espacio libre: " + this.FreeSpace);
Thread.sleep(this.delay);
}
} catch (InterruptedException e) {
//System.out.println("Error" + e);
}
}
public float getFreeSpace(){
return this.FreeSpace;
}
public float getTotalSpace(){
return this.TotalSpace;
}
}
代码:GUI(不是所有代码,只是需要的 IMO)
public class ControlGUI extends javax.swing.JFrame {
/** Creates new form ControlGUI */
public Analisis analisis = new Analisis(1000);
public ControlGUI() {
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch (Exception e){
System.err.println( e );
}
new Thread(analisis).start();
File file = new File("C:\\");
float FreeSpace = (float) file.getFreeSpace() / 1024 / 1024 / 1024;
float TotalSpace = (float) file.getTotalSpace() / 1024 / 1024 / 1024;
float UsedSpace = TotalSpace - FreeSpace;
setTitle("GWControl - A+V - v.1.0");
initComponents();
jProgressBar1.setMaximum((int)TotalSpace);
jProgressBar1.setMinimum(0);
jProgressBar1.setValue((int)UsedSpace);
lbl_espacio_libre.setText("HDD: " + analisis.getFreeSpace() + " Mb libres");
lbl_porcentaje_espacio.setText(UsedSpace*100/TotalSpace + "% del disco utilizado");
}
谢谢!
更新的代码:
通过阅读此处,我进行了以下更改:
在 ControlGUI.java 中:
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ControlGUI control = new ControlGUI();
control.setVisible(true);
}
});
}
和 在构造函数内部:
this.updateProgress(analisis.getFreeSpace());
[...]
private void updateProgress(final double numero) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
lbl_espacio_libre.setText("HDD: " + numero + " Mb libres");
}
});
}
我还无法让它工作=/
I have a java application with a label text, and shows the actual remaining hard disk space. I created another class, a "Runnable" one which reads every X seconds how much space left on HDD.
Now my problem: how can I show in the "statatic" label in the JFrame, that information? The closest thing I thought should work, was create a function in the "runneable" class returning the "double" number, and instance that class in the JFrame
, but it looks like that JFrame runs just once that line and there's no update in the code.
How can I monitor this? I want to monitor other variables too, but this is the beggining.
Code: To read HDD space
package gwcontrol;
import java.io.File;
public class Analisis implements Runnable{
long delay;
File file = new File("C:\\");
float FreeSpace;
float TotalSpace;
public Analisis(long delay){
this.delay = delay;
//setDaemon(true);
}
public void run(){
try {
while (true) {
this.FreeSpace = (float) file.getFreeSpace() / 1024 / 1024 / 1024;
System.out.println("Espacio libre: " + this.FreeSpace);
Thread.sleep(this.delay);
}
} catch (InterruptedException e) {
//System.out.println("Error" + e);
}
}
public float getFreeSpace(){
return this.FreeSpace;
}
public float getTotalSpace(){
return this.TotalSpace;
}
}
Code: The GUI (not all the code, just the needed IMO)
public class ControlGUI extends javax.swing.JFrame {
/** Creates new form ControlGUI */
public Analisis analisis = new Analisis(1000);
public ControlGUI() {
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch (Exception e){
System.err.println( e );
}
new Thread(analisis).start();
File file = new File("C:\\");
float FreeSpace = (float) file.getFreeSpace() / 1024 / 1024 / 1024;
float TotalSpace = (float) file.getTotalSpace() / 1024 / 1024 / 1024;
float UsedSpace = TotalSpace - FreeSpace;
setTitle("GWControl - A+V - v.1.0");
initComponents();
jProgressBar1.setMaximum((int)TotalSpace);
jProgressBar1.setMinimum(0);
jProgressBar1.setValue((int)UsedSpace);
lbl_espacio_libre.setText("HDD: " + analisis.getFreeSpace() + " Mb libres");
lbl_porcentaje_espacio.setText(UsedSpace*100/TotalSpace + "% del disco utilizado");
}
Thanks!!
Updated Code:
By reading HERE, I made these changes:
in ControlGUI.java:
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ControlGUI control = new ControlGUI();
control.setVisible(true);
}
});
}
and
Inside the constructor:
this.updateProgress(analisis.getFreeSpace());
[...]
private void updateProgress(final double numero) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
lbl_espacio_libre.setText("HDD: " + numero + " Mb libres");
}
});
}
I can't get it working yet =/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要创建一个在后台运行的单独线程(例如使用ScheduledExecutorService),定期收集所需的信息,然后更新相关的swing组件(使用SwingUtilities.invokeLater() )。
或者使用 javax.swing.Timer (正如 Kaj 在评论中指出的那样),这可能会为您简化一些操作。
you need to create a separate thread which runs in the background (e.g. using a
ScheduledExecutorService
), periodically gathers the information you need, and then updates the relevant swing components (usingSwingUtilities.invokeLater()
).or use a javax.swing.Timer (as Kaj pointed out in the comments), which will probably simplify some of this for you.
最后!!!感谢@jtahlborn 提供的库/函数
以及
Finally!!! thanks to @jtahlborn for the library / function
and