当下载一个jar时,我希望它在目前为止下载的jlabel%上打印,但是当我开始下载时我实现的方式是从0%的课程开始,但即使在下载之后它也没有显示新的percantage
继承我的代码:
while(( length = inputstream.read(buffer)) > -1)
{
down += length;
bufferedoutputstream.write(buffer, 0 , length);
String text = clientDL.label1.getText();
int perc = getPerc();
text = "Currently downloading , " + perc + "% finished...";
}
并且继承了我的getPerc()方法:
private int getPerc() {
return (down / (sizeOfClient + 1)) * 100;
}
更新:
我让它在一个单独的线程上运行,但仍然是相同的
while(( length = inputstream.read(buffer)) > -1)
{
down += length;
bufferedoutputstream.write(buffer, 0 , length);
thread = new Thread(new Runnable() {
public void run() {
clientDL.label1.setText("Downloading Client : " + getPerc() + " % Done.");
}
});
}
答案 0 :(得分:1)
自动会很好,但是你需要做一些工作才能让你的数字增加。如前所述,您有两种选择:
如果您使用Thread,请将JLabel传递给构造函数,以便在线程进行时轻松修改它的文本。
如果您选择使用SwingWorker创建一个内部类来完成工作并实现doInBackground(),process()和done()。
以下是使用sleep(60)
而不是文件下载作为延迟的两种解决方案。通过调用下载方法替换此行。
SwingWorker 方法:
public class LabelWorker extends JFrame {
private class Task extends SwingWorker<Void, Integer> {
@Override
protected Void doInBackground() {
val = 0;
setProgress(0);
while (val < 1000) {
try {
Thread.sleep(60);
} catch (InterruptedException ex) {
}
publish(val);
setProgress((int) (val * 100. / 1000.));
val++;
}
return null;
}
@Override
protected void process(List<Integer> chunks) {
progressLbl.setText(chunks.get(0).toString());
}
@Override
public void done() {
startBtn.setEnabled(true);
setCursor(null);
val = 1000;
}
private int val;
}
public LabelWorker() {
setTitle("Worker");
setLayout(new FlowLayout());
startBtn = new JButton("Start");
startBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
actionStart();
}
});
stopBtn = new JButton("Stop");
stopBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
actionStop();
}
});
progressLbl = new JLabel("not running...");
add(startBtn);
add(stopBtn);
add(progressLbl);
pack();
}
private void actionStart() {
Task task;
startBtn.setEnabled(false);
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
task = new Task();
task.execute();
t = task;
}
private void actionStop() {
startBtn.setEnabled(true);
setCursor(null);
t.cancel(true);
}
public static void main(String[] args) {
new LabelWorker().setVisible(true);
}
private final JButton startBtn;
private final JButton stopBtn;
private final JLabel progressLbl;
private Task t;
}
Runnable 线程方法:
public class ProgressingLabels extends JFrame {
private class Loader implements Runnable {
private final JLabel progress;
public Loader(JLabel progress) {
this.progress = progress;
progress.setText("0");
}
@Override
public void run() {
int i = 0;
while (i < 1000) {
progress.setText(String.valueOf(++i));
try {
TimeUnit.MILLISECONDS.sleep(60);
} catch (InterruptedException ex) {
break;
}
}
}
}
public ProgressingLabels() {
startButton = new JButton("Start");
stopButton = new JButton("Stop");
progressLabel = new JLabel("0 ");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
startButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
if ((wt == null) || (!wt.isAlive())) {
Loader ld = new Loader(progressLabel);
wt = new Thread(ld);
wt.start();
} else {
JOptionPane.showMessageDialog(null, "Thread already running...");
}
}
});
add(startButton);
stopButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
if ((wt != null) && (wt.isAlive())) {
wt.interrupt();
}
JOptionPane.showMessageDialog(null, "Thread interrupted\n" + (progressLabel.getText()) + " rounds.");
}
});
add(stopButton);
add(progressLabel);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ProgressingLabels().setVisible(true);
}
});
}
private final JButton startButton;
private final JButton stopButton;
private JLabel progressLabel;
private Thread wt;
}
线程类也可以写在单独的文件中。我把它们放在内部类中只是为了使这个代码更紧凑。
对于文件下载,我认为,您应该使用JProgressBar而不是JLabel。
答案 1 :(得分:0)
label1.setText("Download percentage: " + perc)
答案 2 :(得分:0)
您必须使用其他线程。您正在编写数据并在同一个线程上更新UI,因此屏幕没有时间刷新。
查看SwingWorker类。
http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html