Java 在构造函数加载时显示加载条
在我的java类中,我从一个jtable的构造函数中填充了大约10000个值。因此,加载需要时间,应用程序在加载数据之前会挂起。我想显示一个带有装载标志的JLabel以显示状态。但问题是加载类后也会显示加载标志。请帮我做这个。我的代码看起来像这样Java 在构造函数加载时显示加载条,java,swing,Java,Swing,在我的java类中,我从一个jtable的构造函数中填充了大约10000个值。因此,加载需要时间,应用程序在加载数据之前会挂起。我想显示一个带有装载标志的JLabel以显示状态。但问题是加载类后也会显示加载标志。请帮我做这个。我的代码看起来像这样 class myClass{ myClass(){ Loading.setVisible(true); // LOAD ~ 10000 values in the jtable } }
class myClass{
myClass(){
Loading.setVisible(true);
// LOAD ~ 10000 values in the jtable
}
}
有很多可能的方法可以做到这一点。这是其中之一
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
@SuppressWarnings("serial")
public class Test extends JPanel {
private JLabel progressLabel = new JLabel("0 %");
private HeavyProcessor heavyProcessor = new HeavyProcessor();
private static class HeavyProcessor extends Thread {
private volatile int currentStatus = 0;
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
currentStatus++;
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
}
}
}
public int getStatus() {
return currentStatus;
}
}
public Test() {
this.add(progressLabel);
this.heavyProcessor.start();
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
progressLabel.setText(String.valueOf(heavyProcessor.getStatus()) + " %");
}
});
}
}, 0, 1000);
}
public static void main(String[] args) {
JFrame window = new JFrame("Progressbar");
window.setSize(200, 200);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(new Test());
}
}
import java.util.Timer;
导入java.util.TimerTask;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
@抑制警告(“串行”)
公共类测试扩展了JPanel{
私有JLabel progressLabel=新JLabel(“0%”);
私有HeavyProcessor HeavyProcessor=新的HeavyProcessor();
私有静态类HeavyProcessor扩展线程{
私有易失性int currentStatus=0;
@凌驾
公开募捐{
对于(int i=0;我明白了。