同时运行两个帧

时间:2014-09-03 11:29:51

标签: java

I just want to run two frames concurrently, one contains drag and drop feature which i am using to write file to ftp server and other one to display the progress bar to show how much file is written(uploaded you can say).
But the problem is my progress bar frame remains white i.e, GUI is not done on it.why???
please give me a solution.

这是我的第一个类,它是一个homePage框架,它调用另一个名为ProgressSample的类,它将数据写入输出流并同时更新进度条。

FTPConnect.java
SwingWorker<Void , Integer> worker = new SwingWorker<Void , Integer>(){


                @Override
                protected Void doInBackground() throws Exception {

                    ProgressSample.main(secondLocalFile , bytesIn , inputStream , outputStream);

                    return null;
                }

                protected void done(){
                    ProgressSample.frame.dispose();
                }

            };
            worker.execute();

这是第二堂课...... 当我把它的主(......)方法称为只有一个白色的框架时,它上面没有添加任何东西,PLZ PLZ PLZ尽快帮助我提出建议......

ProgressSample.java

package encdec;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
public class ProgressSample
{  
        static JFrame frame;
        JProgressBar progressBar;
        public static void main(File file , byte[] bytesIn, InputStream inputStream , OutputStream outputStream)throws IOException
        {  
            ProgressSample obj=new ProgressSample();
            obj.redfile(file , bytesIn , inputStream , outputStream);
        }  
        public ProgressSample()
        {
            frame = new JFrame();
            frame.setBounds(100, 100, 400, 200);
            frame.getContentPane().setLayout(null);
            progressBar = new JProgressBar();
            progressBar.setBounds(102, 40, 150, 16);
            frame.getContentPane().add(progressBar);
            progressBar.setStringPainted(true);
            frame.setVisible(true);
        }
        public void redfile(File f , byte[] bytesIn, InputStream inputStream , OutputStream outputStream)
        {
            try {


                long totalLength = f.length();

                double lengthPerPercent = 100.0 / totalLength;
                long readLength = 0;
                int read;
                System.out.println(totalLength);
                while ((read = inputStream.read(bytesIn)) != -1) {



                    readLength += read;
                    progressBar.setValue((int) Math.round(lengthPerPercent * readLength)); 

                    outputStream.write(bytesIn, 0, read);



                }
                progressBar.setValue(100);
               frame.dispose();
               JOptionPane.showMessageDialog(null, "Upload Successful");


            } catch (Exception e) {

            }
        }

    }

0 个答案:

没有答案