0

我有一个套接字连接的客户端和服务器。我尝试将 progressMonitorInputStream() 实现到服务器,数据传输完美,但进度监视器不可见。我尝试实现它以仅从一侧读取文件,进度监视器工作正常。下面是两个代码

服务器:

    public class Server implements ActionListener, PropertyChangeListener {

    DataInputStream dis;
    ProgressMonitor pm;
    ProgressMonitorInputStream pmis;
    JFrame frm;
    Server s;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {

                new Server().createAndShowUI();
            }
        });
    }

    private void createAndShowUI() {
        frm = new JFrame();
        JPanel panel = new JPanel();

        JButton st = new JButton("Start");

        st.setPreferredSize(new Dimension(80, 25));

        panel.add(st);

        panel.setSize(500, 50);

        frm.add(panel);
        frm.setLocationRelativeTo(null);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.pack();
        frm.show();

        st.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        frm.dispose();
        try {
            ServerSocket ss = new ServerSocket(5555);
            Socket soc = ss.accept();
            dis = new DataInputStream(soc.getInputStream());
            pmis = new ProgressMonitorInputStream(frm, "Progress", dis);
            pm = pmis.getProgressMonitor();
//            pm.setMillisToPopup(1);
            Task task = new Task();
            task.addPropertyChangeListener(s);
            task.execute();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress".equals(evt.getPropertyName())) {
            int progress = (Integer) evt.getNewValue();
            pm.setProgress(progress);

        }
    }

    class Task extends SwingWorker<Void, Void> {

        @Override
        protected Void doInBackground() throws Exception {
            int progress = 0;
            setProgress(0);
            int x;
            while ((x = pmis.read()) != -1) {
                System.out.print((char) x);
                setProgress(progress);
                progress++;                
            }
            pmis.close();
            return null;
        }

        @Override
        public void done() {
            pm.close();
        }        

    }
}

上面的代码是有问题的。工作代码如下:

一侧带有文件输入流的正确工作代码

    public class ProgressMonitorOwn implements ActionListener,
        PropertyChangeListener {

    JFrame frm;
    Task task;
    DataInputStream dis;
    ProgressMonitorInputStream pmis;
    JButton but;
    ProgressMonitor pm;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ProgressMonitorOwn().createAndShowUI();
            }
        });
    }

    private void createAndShowUI() {
        frm = new JFrame();
        JPanel panel = new JPanel();
        but = new JButton("Show");
        but.setPreferredSize(new Dimension(100, 25));

        panel.add(but);
        panel.setSize(200, 100);

        frm.add(panel);
        frm.setSize(200, 100);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setLocationRelativeTo(null);
        frm.show();
        but.addActionListener(this);
    }

    class Task extends SwingWorker<Void, Void> {

        @Override
        protected Void doInBackground() throws Exception {
            int x;
            int prog = 0;
            while ((x = pmis.read()) != -1) {
                System.out.print((char) x);
                setProgress(prog);
                Thread.sleep(100);
                prog++;
            }
            return null;

        }

        @Override
        public void done() {
            but.setEnabled(true);
            pm.close();
            System.exit(0);
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        but.setEnabled(false);
        try {
            FileInputStream fis = new FileInputStream("D:\\test\\test.txt");
            dis = new DataInputStream(fis);
            pmis = new ProgressMonitorInputStream(frm, "ProgressTest", dis);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress" .equals(evt.getPropertyName())) {
            int progress = (Integer) evt.getNewValue();
            pm = pmis.getProgressMonitor();
            pm.setProgress(progress);
        }
    }
}

我无法弄清楚为什么进度监视器显示在 FileInputStream 程序中而不是套接字程序中。有任何想法吗?

4

2 回答 2

1

您需要调用setMaximum()到预期ProgressMonitorProgressMonitorInputStream下载大小。否则它不知道“进步”是什么意思。

您不必setProgress()使用ProgressMonitorInputStream. 它会自动执行此操作。

你不需要DataInputStream.

您一次应该读取多个字节。读入byte[]缓冲区。

于 2014-05-03T07:23:42.233 回答
0

Call frm.setVisible(true);to make 可见。两个班级都有变化。

删除frm.show(),因为它是一种已弃用的方法。看这里Component#show()

示例代码:

private void createAndShowUI() {
   frm = new JFrame();
   ...
   // call in the end after adding all the components
   frm.setVisible(true);
}
于 2014-05-03T07:10:40.227 回答