0

几个星期以来,我一直在尝试解决以下问题:(。在我的 Java 小程序 (MyApplet) 中,我有一个方法,用于一个名为 btnSendToPC 的按钮,它应该通过打开的套接字将选定的文件内容从服务器发送到本地 PC。当我尝试实现一个 JProgressBar 时会出现问题,它将向用户显示该文件中有多少已下载到本地 PC。我在此站点上阅读了许多 JProgressBar 示例,但老实说我仍然无法弄清楚。如果你帮了我以下是我的代码的一个非常简短的版本,包括所有主要部分。

//My Network class that opens a socket and reads bytes from the socket
public class TcpIp {

    protected Socket s = null;
    public DataInputStream dis = null;

    public TcpIp(InetAddress ipa, int port) {

        try { 
            s = new Socket(ipa.getHostAddress(), port);
        } catch (IOException ex) {
            System.out.println("Error opening socket!");
            return;
        }

        try { //Create an input stream.
            dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));
        } catch (Exception ex) {
            System.out.println("Error creating input stream!");
        }

    }

    public synchronized byte[] readBytes() throws IOException {

        ByteArrayOutputStream getBytes = new ByteArrayOutputStream();

        int oneByte;

        while ((oneByte = dis.read()) != 124) {//Reads 1 byte from the InputStream and breaks on | character
            getBytes.write(oneByte);
        }

        return (getBytes.toByteArray());

    }
}

//My main Applet class that has the method for Send to PC button
public class MyApplet extends javax.swing.JApplet implements Runnable {

    public TcpIp gtp = null;
    private static String inGet;

    @Override
    public void run() {

        int i;

        byte[] in = new byte[10000024];

        try {
            Thread.sleep(1000);
            //pause gtp.readBytes so that server has enough time to receive name of the file to read,
            //to read its contents and send its contents back through socket
        } catch (InterruptedException ex) {
        }

        if ((gtp != null)) {

            try {
                in = gtp.readBytes();
            } catch (IOException ex) {
            }

            //Remove non-printing bytes.
            for (i = 0; i < in.length; i++) {
                if (in[i] < 0x20) {
                    in[i] = 0x20;
                }
            }

            inGet = new String(in);
        }
    }

    public void btnSendToPC() {

        //In here are commands that send name of the file to read. 
        //Server reads that file and sends its contents back through socket.

        try {

            FileOutputStream fout = new FileOutputStream(fn);
            BufferedWriter myOutput = new BufferedWriter(new OutputStreamWriter(fout));

            timer = new Thread(this);
            timer.start();

            try {
                timer.join();
            } catch (InterruptedException ex) {
            }

            myOutput.write(inGet)

            myOutput.close();
            fout.close();
        }

    }
}
4

3 回答 3

3

您的 TcpIp 类需要在自己的线程中运行。然后你打电话

SwingUtilities.invokeLater(new Runnable(){
  public void run(){
    progressBar.setValue(progressBar.getValue()+1);
  }
}

getBytes.write(oneByte);

但在执行此操作之前,您需要设置一个最大值。为此,您需要知道要读取多少字节。如果您不知道将收到多少字节,那么进度条就没有意义。

我还建议不要在收到的每个字节上调用进度条更新。

于 2011-12-15T19:23:56.060 回答
3

只需使用javax.swing.ProgressMonitorInputStream. 为你做所有的努力。

于 2011-12-15T22:55:40.227 回答
1

您可以使用 oberver 或 eventlistener 更改进度条。

尝试观察者可观察来更改其他类的进度条。

于 2011-12-15T19:26:30.340 回答