我已经在这个论坛上访问了this以及其他几个这样的页面,并通过了Swing on Oracle docs。即使我逐行模拟所有代码并在代码中进行一些调整,他们的所有代码都不适用于我的ProgressBar。我不知道它为什么不起作用!我真的对自己感到怜悯,我无法让它发挥作用。
我知道EDT存在一些问题,但我自己也无法弄明白。我认为我的代码正在使用工作线程来更新进度条。
此外,一旦代码下载,然后再次重置字段和所选项目,则后续的下载调用也会被阻止。该文件已创建,但未下载。它们的大小仍为0.请解释原因?
我创建了一个SwingWorker实例,并从doInBackground()方法调用FileProgressBar的更新,然后调用done()。但是,它没有用。文件下载成功,但进度条不会移动一英寸!!!
public class Client{
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MakeGUI().setVisible(true);
}
});
}
}
class MakeGUI extends JFrame implements PropertyChangeListener{
private JTextField ClientNameTF;
private JLabel DisplayLabel;
private JButton DownloadButton;
private JButton ExitButton;
private JComboBox FileComboBox;
private JProgressBar FileProgressBar;
private JButton FileStatusButton;
private JLabel FinalLabel;
private JTextField StatusTextField;
private JLabel TitleLabel;
private JButton ResetButton;
private JLabel LoginLabel;
private ProgressWorker pw;
private static JFrame jf;
public static String IP_ADDRESS_TO_CONNECT;
public MakeGUI() {
FileComboBox = new javax.swing.JComboBox();
DisplayLabel = new javax.swing.JLabel();
FileStatusButton = new javax.swing.JButton();
StatusTextField = new javax.swing.JTextField();
DownloadButton = new javax.swing.JButton();
FileProgressBar = new javax.swing.JProgressBar();
ExitButton = new javax.swing.JButton();
FinalLabel = new javax.swing.JLabel();
TitleLabel = new javax.swing.JLabel();
LoginLabel = new javax.swing.JLabel();
ClientNameTF = new javax.swing.JTextField();
ResetButton = new javax.swing.JButton();
callPainting();
}
public void callPainting(){
setLocationRelativeTo(null);
setBounds(300, 0, 300, 300);
FileComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ALGORITHM- The Hacker.MKV", "ARUN_ASAD.docx", "Johnny Sins.jpg", "MGH.Java.The.Complete.Reference.9th.Edition.pdf", "rabbit4.11-src.tar.gz", "SAMUDRA.jpg", "Syngress - Seven Deadliest Web Application Attacks (2010) (ATTiCA).pdf", "Tu Hai Ki Nahi (Roy).mp3" }));
FinalLabel.setLabelFor(FinalLabel);
TitleLabel.setText("Distributed File Downloading System");
LoginLabel.setText("Your Login Name");
DisplayLabel.setText("Select the Item you wanna download");
FileStatusButton.setText("Check Status of the File");
FileStatusButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
FileStatusButtonActionPerformed(evt);
}
});
DownloadButton.setText("Start Download");
DownloadButton.setEnabled(false);
DownloadButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
DownloadButtonActionPerformed(evt);
}
});
FileProgressBar.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(187, 55, 55)));
FileProgressBar.setStringPainted(true);
FileProgressBar.setMinimum(0);
FileProgressBar.setMaximum(100);
FileProgressBar.setValue(0);
//FileProgressBar.setIndeterminate(false);
ResetButton.setText("Reset");
ResetButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
ResetButtonActionPerformed(evt);
}
});
ExitButton.setText("Exit Application");
ExitButton.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
ExitButtonActionPerformed(evt);
}
});
pack();
}
public void FileStatusButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
String clientName=ClientNameTF.getText();
int choiceOfFile = FileComboBox.getSelectedIndex();
new Server().requestFile(choiceOfFile,clientName);
IP_ADDRESS_TO_CONNECT=new Server().returnIPAddress();
StatusTextField.setText("Server Available = "+IP_ADDRESS_TO_CONNECT);
if(!(StatusTextField.getText().isEmpty()))
DownloadButton.setEnabled(true);
else
DownloadButton.setEnabled(false);
} catch (IOException ex) {
FinalLabel.setText(ex.getMessage());
}
}
public void DownloadButtonActionPerformed(java.awt.event.ActionEvent evt) {
DownloadButton.setEnabled(false);
boolean COMPLETION=false;
long fsize=new Server().returnFileSize();
String fileSize;
Long initialTime = 0L;
if(fsize <= 0) {
fileSize = "0";
}
final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" };
int digitGroups = (int) (Math.log10(fsize)/Math.log10(1024));
fileSize= new DecimalFormat("#,##0.#").format(fsize/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
String choiceOfFile = (String)FileComboBox.getSelectedItem();
int dialogButton=JOptionPane.YES_NO_OPTION;
if(new File(choiceOfFile).exists()){
int dialogResult=JOptionPane.showConfirmDialog(null,"Would you like to delete your already"
+ " existing file(Press Yes to delete OR No to quit)???","Confirm Action", dialogButton);
if(dialogResult==JOptionPane.YES_OPTION){
new File(choiceOfFile).delete();
}
else {
FinalLabel.setText("File Already Exists!!! If you wish, you can download some another file...");
System.exit(-1);
}
}
FinalLabel.setText("File Size to be Downloaded = "+fileSize);
ProgressWorker pw = new ProgressWorker(choiceOfFile,IP_ADDRESS_TO_CONNECT,fsize);
initialTime = System.nanoTime();
pw.addPropertyChangeListener(this);
/*else if (name.equals("state")) {
SwingWorker.StateValue state = (SwingWorker.StateValue)evt.getNewValue();
switch (state) {
case DONE :
{
DownloadButton.setEnabled(true);
break;
}
}
}*/
pw.execute();
try {
initialTime = pw.get();
} catch (InterruptedException | ExecutionException ex) {
ex.printStackTrace();
}
long finalTime=System.nanoTime();
float difference = (float)((finalTime-initialTime)/1000000000.0);
StatusTextField.setText("Time difference = "+difference);
}
public void ResetButtonActionPerformed(java.awt.event.ActionEvent evt) {
ClientNameTF.setText("");
StatusTextField.setText("");
FinalLabel.setText("");
FileComboBox.setSelectedIndex(0);
FileProgressBar.setValue(0);
}
public void ExitButtonActionPerformed(java.awt.event.ActionEvent evt) {
int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit this application?", "Confirm",
JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (response == JOptionPane.NO_OPTION) {
System.out.println("No button clicked");
} else if (response == JOptionPane.YES_OPTION) {
System.exit(0);
} else if (response == JOptionPane.CLOSED_OPTION) {
System.out.println("JOptionPane closed");
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if("progress" == evt.getPropertyName()){
int progress = (Integer)evt.getNewValue();
FileProgressBar.setValue(progress);
}
}
class ProgressWorker extends SwingWorker<Long,Integer>{
String cof,IP;
long fileSize;
Long initialTime;
File downloadingFile;
public ProgressWorker(String choiceOfFile,String IP_ADDRESS_TO_CONNECT,long fsize) {
this.cof = choiceOfFile;
this.IP = IP_ADDRESS_TO_CONNECT;
this.fileSize = fsize;
downloadingFile = new File("/home/you/Desktop/BASH/Client",cof);
}
@Override
public Long doInBackground() throws Exception {
int temp=1;
boolean flag=true;
int progressPercent = 0;
setProgress(progressPercent);
initialTime=System.nanoTime();
if(SwingUtilities.isEventDispatchThread()==true)
System.out.println("True EDT.");
else
System.out.println("Worker Threads...");
StartFileClient.startDownloading(cof,IP);
while(progressPercent<100 && downloadingFile.length() != fileSize){
//System.out.println("Length = "+downloadingFile.length());
progressPercent = (int)((downloadingFile.length()*100.0)/fileSize);
setProgress(progressPercent);
Thread.sleep(50);
}
return initialTime;
}
@Override
public void done(){
Toolkit.getDefaultToolkit().beep();
DownloadButton.setEnabled(true);
FinalLabel.setText("Downloading Done...");
}
}
}