JTable
,但需要很长时间。我想使用SwingWorker
来解决问题。我找到了这样的教程SwingWorker,但我仍然没有得到它。我在哪里必须使用SwingWorker
;它是在阅读课程或GUI类中,我从来没有尝试过,我也不知道有什么事情;我试图谷歌每个地方,buti需要非常简单的例子“最简单的”,换句话说从哪里开始
答案 0 :(得分:0)
这是一个最小的例子。这是一个在JLabel
中显示虚拟进度的程序:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class GUI extends JFrame {
private JLabel lblProgress = new JLabel("0%");
public GUI() {
setTitle("Swing worker example");
setSize(300, 200);
add(lblProgress);
new Process().execute();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GUI gui = new GUI();
gui.setVisible(true);
}
});
}
class Process extends SwingWorker<String, String> {
@Override
protected String doInBackground() throws InterruptedException {
int count = 10000;
for (int i = 0; i < count; i++) {
int progress = (int) ((double) i/count*100.0);
lblProgress.setText(progress + "%");
repaint();
Thread.sleep(10);
}
return null;
}
@Override
protected void done() {
lblProgress.setText("Finished!");
}
}
}