[英]How to receive bytes from a lib to update the JProgressBar?
I've tried to receive bytes from a lib to set my JProgressBar update for a long time but unfortunately I didn't get the result I wanted. 我试图从库中接收字节以设置JProgressBar更新已很长时间了,但是不幸的是我没有得到想要的结果。
The problem is that I don't know how to receive bytes from a lib and how to update the JProgressBar while receiving bytes all that to make a browser with java 问题是我不知道如何从库中接收字节以及如何在接收字节的同时更新JProgressBar以使用Java创建浏览器
This is the code I've tried: 这是我尝试过的代码:
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_ENTER){
String az = jTextField1.getText();
if(az.contains("1")){
String hh = WorkSpace.jTextField1.getText();
URLConnection conn = null;
InputStream in = null;
try {
URL url = new URL(hh);
conn = url.openConnection();
in = conn.getInputStream();
int length = conn.getContentLength();
int current = 0;
WorkSpace.jProgressBar1.setMaximum(length);
WorkSpace.jProgressBar1.setValue(0);
byte[] buffer = new byte[1024];
int numRead = 0;
while ((numRead = in.read(buffer)) != -1) {
current=0;
current += numRead;
WorkSpace.jProgressBar1.setValue(current);
}
} catch (Exception e) {
}
}
If there's someone who knows how to do this please let me know. 如果有人知道该怎么做,请告诉我。
Just wrap the input stream in a ProgressMonitorInputStream
. 只需将输入流包装在
ProgressMonitorInputStream
。 It will manage its own JProgressBar
for you. 它将为您管理自己的
JProgressBar
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.