我有一个套接字连接的客户端和服务器。我尝试将 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 程序中而不是套接字程序中。有任何想法吗?