JProgressBar 无法自动更新是什么原因??

jackwin 2008-07-17 08:55:17
我在Main外定义了JButton JProgressBar 让Swing_mainWindows里其它函数能访问.

public class Swing_mainWindows {
private JButton button;
JProgressBar progressBar = new JProgressBar();
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {

=============================================================================
修改progressBar的百分比
public void setJProgressBar(int a){
progressBar.setValue(a);
}

下面是BUTTON的MouseClicked过程,每完成一个就增加10%,但程序运行时进度条是不会一格一格动的,只会由按了BUTTON开始到结束完成后就到100%了,我还特意用hread.sleep(1000);暂停一下主程序但依然是不动!

button = new JButton("开始数据转换");
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent arg0) {
try {
FunctionPro com = new FunctionPro();
Collection c2 = a.getselectcheckNew(19);
setJProgressBar(10);

hread.sleep(1000);

Collection c3 = a.getselectcheckNew(20);
setJProgressBar(progressBar.getValue()+10);

Collection c = com.linkCheckTime(c2,c3);
if(c.size() <=0)
{SetActionLable("没有新数据可转换");
setJProgressBar(100);
return;}
setJProgressBar(progressBar.getValue()+10);
// CHECKTIME USERID BADGENUMBER SENSORID
//取指纹记录集合 2008-06-24 08:06:31.000 1249 01006 19

//提取所有 单独 BADGENUMBER生成List
ArrayList st = com.FPBadgen(c);

// BPEID BPECode
//xuserver83221 01006
Collection c4 = b.getMainserverSSID(com.FPBadgenStr(st));
setJProgressBar(progressBar.getValue()+10);
Thread.sleep(1000);
// BPEID KH
//xuserver1174229 40561583
Collection c5 = b.getHRSendCheckInRs();
setJProgressBar(progressBar.getValue()+10);
Thread.sleep(1000);
//合成(i+"",KH,it.getBPEID(),"","","","","");
Collection c45 = com.CountBpHrc(c4,c5);

//查找丢失的KH到C45

Collection c46 = b.getNotFoundKHList(com.FPBadgenStr(com.getFoundHkList(c45)));

Collection c47 = com.HRDocatLinkKh(c45, c46);

Collection creatPass = com.HRDocatLink(c,c47);
setJProgressBar(progressBar.getValue()+10);
Thread.sleep(1000);
if(com.InsertSource(a,b,creatPass).endsWith(""))
SetActionLable("完成转换");
setJProgressBar(100);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
});
...全文
281 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackwin 2008-07-17
  • 打赏
  • 举报
回复
大概你说的我明白了,我再找段时间消化一下XX
hardtoregistration 2008-07-17
  • 打赏
  • 举报
回复

/*
* Created on July 10 2008
*
* Copyright by 布谷鸟
*/
package swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
*
* @author cuckoo
*
*/
public class RefreshTry {

public static void main(String args[]) {
new RefreshTry().createShell();
}

private void createShell() {
_shell = new Shell(_display, SWT.MIN);
_shell.setSize(300, 200);
_shell.setText("Refresh Contents");
createContents();
_shell.open();
while (!_shell.isDisposed()) {
if (!_display.readAndDispatch()) {
_display.sleep();
}
}
_display.dispose();
}

private void createContents() {
final Button button = new Button(_shell, SWT.PUSH);
button.setBounds(2, 2, 100, 20);
button.setText("Click me");
displayLabel = new Label(_shell, SWT.SHADOW_IN);
displayLabel.setText("Default ....");
displayLabel.setBounds(10, 50, 40, 20);

_methodLabel = new Label(_shell, SWT.SHADOW_ETCHED_OUT);
_methodLabel.setBackground(new Color(_display, 0, 0, 0));
_methodLabel.setForeground(new Color(_display, 140, 140, 140));
_methodLabel.setBounds(120, 50, 100, 15);

button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// TODO Auto-generated method stub
displayLabel.setBounds(10, 50, 40, 40);
displayLabel.setText("");
displayLabel
.setBackgroundImage(new Image(_display, "89_m.bmp"));
// sleep one minutes
processThread();
}
});
}

private void processThread() {
Thread thread = new Thread() {
public void run() {
// TODO Auto-generated method stub
_display.asyncExec(new Runnable() {
public void run() {
// TODO Auto-generated method stub
int i = 0;
while (i <= 30) {
if (i != 30) {
_methodLabel.setText("当前值为: " + i);
} else {
if (i == 30) {
_methodLabel
.setText("方法调用结束 ");
Image image = new Image(_display,
"89_m.bmp");
displayLabel.setBackgroundImage(image);
displayLabel.setSize(
image.getBounds().width, image
.getBounds().height);
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i ++ ;
}
}
});

}
};
thread.start();
}

private Shell _shell = null;
private Label _methodLabel = null;
private Label displayLabel = null;
private Display _display = Display.getDefault();
}



//这儿有段代码,不过是swt,他们理论上都是差不多的,希望对你有帮助

-------------------------------------------------------------
Quietly through .....
hardtoregistration 2008-07-17
  • 打赏
  • 举报
回复
进度条得配合线程使用,你的代码问题是,当你点击button的时候,显示进度条的那段代码都在button的监听事件里面,只有当button的监听事件执行完之后,才会回到主线程刷新页面(这句话描述可能有点问题),所以你看到的是一下就到100%了。
你应该把显示button监听里面的代码放到一个线程里面,监听时间里面启动线程就可以了

-------------------------------------------------------------
Quietly through .....
jackwin 2008-07-17
  • 打赏
  • 举报
回复
weilovewml 说了些什么话了???

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧