0

私は何週間もの間、次の問題を修正しようとしてきました:(。私のJavaアプレット(MyApplet)には、選択したファイルの内容をサーバーからローカルPCにオープンソケットを介して送信することになっているbtnSendToPCと呼ばれるボタン用のメソッドがあります。この問題は、ローカル PC にダウンロードされたファイルの量をユーザーに表示する JProgressBar を実装しようとしたときに発生します. このサイトで多くの 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 を使用してプログレスバーを変更できます。

他のクラスからプログレスバーを変更するには、observer-observable を試してください。

于 2011-12-15T19:26:30.340 に答える