监控硬盘空间...如何实时显示信息?

发布于 2024-11-06 04:54:32 字数 2797 浏览 2 评论 0原文

我有一个带有标签文本的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉薄对峙 2024-11-13 04:54:32

您需要创建一个在后台运行的单独线程(例如使用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 (using SwingUtilities.invokeLater()).

or use a javax.swing.Timer (as Kaj pointed out in the comments), which will probably simplify some of this for you.

多孤肩上扛 2024-11-13 04:54:32

最后!!!感谢@jtahlborn 提供的库/函数

public class ControlGUI extends javax.swing.JFrame {

pruebas prueba;

public ControlGUI() {

    try{
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch (Exception e){
    System.err.println( e );
    }

    final 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);

    prueba = new pruebas(lbl_espacio_libre);
    prueba.start();

    lbl_porcentaje_espacio.setText(UsedSpace*100/TotalSpace + "% del disco utilizado");
}

以及

package gwcontrol;
import java.io.File;
import javax.swing.SwingUtilities;

public class pruebas extends Thread implements Runnable {

float FreeSpace;
File file;
javax.swing.JLabel label;

public pruebas(javax.swing.JLabel label){

    this.label = label;
    file = new File("C:\\"); 
}

 public void run() {
    while (true) {

        try {
            this.FreeSpace = (float) file.getFreeSpace() / 1024 / 1024 / 1024;
            System.out.println("Espacio libre: " + this.FreeSpace);
            Thread.sleep(1000);
        } catch (Exception e) {
            System.out.println("Problema: " + e);
        }

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
              label.setText("HDD: " + FreeSpace + " Mb libres");
            }
        });
    }
  }
}

Finally!!! thanks to @jtahlborn for the library / function

public class ControlGUI extends javax.swing.JFrame {

pruebas prueba;

public ControlGUI() {

    try{
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch (Exception e){
    System.err.println( e );
    }

    final 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);

    prueba = new pruebas(lbl_espacio_libre);
    prueba.start();

    lbl_porcentaje_espacio.setText(UsedSpace*100/TotalSpace + "% del disco utilizado");
}

and

package gwcontrol;
import java.io.File;
import javax.swing.SwingUtilities;

public class pruebas extends Thread implements Runnable {

float FreeSpace;
File file;
javax.swing.JLabel label;

public pruebas(javax.swing.JLabel label){

    this.label = label;
    file = new File("C:\\"); 
}

 public void run() {
    while (true) {

        try {
            this.FreeSpace = (float) file.getFreeSpace() / 1024 / 1024 / 1024;
            System.out.println("Espacio libre: " + this.FreeSpace);
            Thread.sleep(1000);
        } catch (Exception e) {
            System.out.println("Problema: " + e);
        }

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
              label.setText("HDD: " + FreeSpace + " Mb libres");
            }
        });
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文