0
我正在開發一個下載管理器。我無法更新標籤。我試圖用run()
方法下載更新標籤,但發生異常。我如何更新Label.Where我寫更新標籤和ProgressBar的代碼。如何通過下載更新javafx標籤
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.geometry.*;
import javafx.scene.effect.*;
import javafx.scene.paint.*;
import javafx.event.*;
import javafx.scene.image.*;
import java.util.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.io.*;
import java.net.*;
public class DownloadFile implements Runnable
{
Thread thread;
private static final int BUFFER_SIZE=1024;
public final int DOWNLOADING=0;
public final int PAUSED=1;
public final int COMPLETE=2;
public final int CANCELLED=3;
public final int ERROR=4;
private URL url;
private int size;
private int downloaded;
private int status;
static int check=0;
Stage dStage;
Label fileNameLabel;
Label fileNameValueLabel;
Label statusLabel;
Label statusValueLabel;
Label fileSizeLabel;
Label fileSizeValueLabel;
Label downloadedLabel;
Label downloadedValueLabel;
Label speedLabel;
Label speedValueLabel;
Label timeRemainingLabel;
Label timeRemainingValueLabel;
ProgressBar downloadingProgressBar;
public void startStage()
{
dStage=new Stage();
dStage.setResizable(false);
dStage.getIcons().add(new Image("images\\webdownloadmanager.png"));
dStage.setTitle("Friends Download Manager");
BorderPane root=new BorderPane();
root.setPadding(new Insets(10, 10, 10, 10));
Scene dScene=new Scene(root,520,250);
dStage.setScene(dScene);
Label titleLabel=new Label(" Download Status ");
titleLabel.setStyle("-fx-background-color:#FFFFFF;");
GridPane gpCenter=new GridPane();
gpCenter.setPadding(new Insets(10, 10, 10, 10));
gpCenter.setStyle("-fx-background-color:#FFFFFF;");
fileNameLabel =new Label("File Name");
fileNameValueLabel=new Label();
statusLabel =new Label("Status");
statusValueLabel =new Label();
fileSizeLabel =new Label("File size");
fileSizeValueLabel =new Label();
downloadedLabel =new Label("Downloaded");
downloadedValueLabel =new Label();
speedLabel =new Label("Speed");
speedValueLabel =new Label();
timeRemainingLabel =new Label("Time Remaining ");
timeRemainingValueLabel =new Label();
gpCenter.add(fileNameLabel,0,0);
gpCenter.add(fileNameValueLabel,2,0);
gpCenter.add(statusLabel ,0,3);
gpCenter.add(statusValueLabel ,2,3);
gpCenter.add(fileSizeLabel ,0,4);
gpCenter.add(fileSizeValueLabel ,2,4);
gpCenter.add(downloadedLabel ,0,5);
gpCenter.add(downloadedValueLabel ,2,5);
gpCenter.add(speedLabel ,0,6);
gpCenter.add(speedValueLabel ,2,6);
gpCenter.add(timeRemainingLabel ,0,7);
gpCenter.add(timeRemainingValueLabel ,2,7);
AnchorPane anchorPaneBottom=new AnchorPane();
VBox vb=new VBox();
HBox progressBarHB=new HBox();
progressBarHB.setPadding(new Insets(10, 0, 30, 0));
downloadingProgressBar=new ProgressBar();
downloadingProgressBar.prefWidthProperty().bind(root.widthProperty().subtract(20));
progressBarHB.getChildren().add(downloadingProgressBar);
vb.getChildren().addAll(progressBarHB);
HBox labelHB=new HBox();
Label message=new Label("Whould you like to perform ?");
labelHB.getChildren().add(message);
HBox buttonHB=new HBox();
buttonHB.setSpacing(20);
Button startPauseButton=new Button("Start");
startPauseButton.setPrefWidth(80);
Button cancelButton=new Button("Cancel");
cancelButton.setPrefWidth(80);
buttonHB.getChildren().addAll(startPauseButton,cancelButton);
anchorPaneBottom.getChildren().addAll(vb,labelHB,buttonHB);
AnchorPane.setTopAnchor(buttonHB, 40.0);
AnchorPane.setRightAnchor(buttonHB,30.0);
AnchorPane.setLeftAnchor(labelHB,40.0);
AnchorPane.setTopAnchor(labelHB,40.0);
AnchorPane.setTopAnchor(vb, 0.0);
root.setTop(titleLabel);
root.setCenter(gpCenter);
root.setBottom(anchorPaneBottom);
dStage.show();
dStage.setOnCloseRequest(new EventHandler<WindowEvent>()
{
public void handle(WindowEvent we)
{ /*
if(WebDownloadManager.check==0)
Platform.exit();
else
check--;
*/
}
});
}
public DownloadFile(URL url)
{
this.url=url;
size=-1;
downloaded=0;
status=DOWNLOADING;
startStage();
fileNameValueLabel.setText(getFileName(url).toString());
statusValueLabel.setText("Waiting..");
fileSizeValueLabel.setText(getSize()+"");
downloadedValueLabel.setText("0.00 MB ("+getProgress()+" %)");
speedValueLabel.setText("450.123 KB/sec");
timeRemainingValueLabel.setText("1 hr ");
download();
}
public String getUrl()
{
return url.toString();
}
public int getSize()
{
return size;
}
public float getProgress()
{
float tf=(downloaded/size)*100;
return tf;
}
public int getStatus()
{
return status;
}
public void pause()
{
status=PAUSED;
}
public void resume()
{
status=DOWNLOADING;
download();
}
public void cancel()
{
status=CANCELLED;
}
private void error()
{
status=ERROR;
}
private void download()
{
thread=new Thread(this);
thread.start();
}
private String getFileName(URL url)
{
String fileName=url.getFile();
String str=fileName.substring(fileName.lastIndexOf('/')+1).replace("%20"," ");
return str;
}
public void run()
{
RandomAccessFile file=null;
InputStream stream=null;
try
{
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestProperty("Range","bytes="+downloaded+"-");
conn.connect();
if(conn.getResponseCode()/100!=2)
{
error();
}
int contentLength=conn.getContentLength();
if(contentLength<1)
{
error();
}
if(size==-1)
{
size=contentLength;
}
file=new RandomAccessFile(getFileName(url),"rw");
file.seek(downloaded);
stream=conn.getInputStream();
while(status==DOWNLOADING)
{
statusValueLabel.setText("Running..");
byte buffer[];
if(size-downloaded>BUFFER_SIZE)
{
buffer=new byte[BUFFER_SIZE];
}
else
{
buffer=new byte[size-downloaded];
}
int read=stream.read(buffer);
if(read== -1)
{
break;
}
file.write(buffer,0,read);
downloaded+=read;
}
if(status==DOWNLOADING)
{
status=COMPLETE;
}
}
catch(Exception exp)
{
error();
System.out.println("Error :"+exp);
}
finally
{
if(file!=null)
{
try
{
file.close();
}
catch(Exception e)
{
}
}
if(stream!=null)
{
try
{
stream.close();
}
catch(Exception exp)
{
}
}
}
}
}
提示1:閱讀['Task'(https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html)並使用它來代替'Runnable'。 – ItachiUchiha
你的問題很難回答,因爲它是。你能1.簡化代碼。這裏有更多的代碼比你的問題更多。用一個標籤創建一個簡單的例子,以及如何管理下載,並擺脫所有額外的東西。 2.從異常中發佈堆棧跟蹤,並確定它來自哪條線。 –