如何实现进度条功能 我想实现一个循环功能,在循环的时候调用进度条,显示循环的进度,请问如何实现?我找到一些都是通过线程的间停来控制进度条的,说白了,我找到的都是和时间有关的进度条例子,我想要的是和循环的进度有关的例子,谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 现贴出通过线程的时间来显示的进度条方法,给需要的人,我求的是循环进度的方式显示的进度条import javax.swing.*;import java.awt.*;import com.borland.jbcl.layout.*;import java.awt.event.*;import java.lang.reflect.InvocationTargetException;public class jindu extends JFrame{ JPanel jPanel1 = new JPanel(); XYLayout xYLayout1 = new XYLayout(); JProgressBar jProgressBar1 = new JProgressBar(); JButton jButton1 = new JButton(); Thread myThread; int num=500; boolean shouldStop;//should thread to be stopped Object lock=new Object(); JTextArea output; public jindu() { try { jbInit(); this.setSize(400,300); this.show(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { jindu jindu1 = new jindu(); } private void jbInit() throws Exception { jPanel1.setLayout(xYLayout1); jButton1.setText("jButton1"); jButton1.addActionListener(new jindu_jButton1_actionAdapter(this)); this.addWindowListener(new jindu_this_windowAdapter(this)); this.getContentPane().add(jPanel1, BorderLayout.CENTER); jProgressBar1.setValue(0); jProgressBar1.setStringPainted(true); jPanel1.add(jProgressBar1, new XYConstraints(209, 222, -1, -1)); jPanel1.add(jButton1, new XYConstraints(209, 180, -1, -1)); } void jButton1_actionPerformed(ActionEvent e) { if(myThread==null) { myThread=new TaskThread(num);//create a task thread shouldStop=false; myThread.start();//start the task thread } else {//end the task synchronized (lock) { shouldStop = true; lock.notify(); //notify thread if stopped } } } class TaskThread extends Thread{private int max;public TaskThread(int max){this.max=max;}public void run(){int min=0;jProgressBar1.setValue(min);jProgressBar1.setMinimum(min);jProgressBar1.setMaximum(max);Runnable runner=new Runnable(){public void run(){int value=jProgressBar1.getValue();value++;//increase counter valuejProgressBar1.setValue(value);//change current value of progress bar}};for(int i=min;i<max;i++){try{//block until AWT events hava been processedSwingUtilities.invokeAndWait(runner);}catch(InvocationTargetException e){break;}catch(InterruptedException e){//Ignore Exception}synchronized(lock){if(shouldStop)//check if Stop is pressedbreak;try{lock.wait(100);}catch(InterruptedException e){//Ignore Exception}}}jButton1.setEnabled(true);//enable buttonmyThread=null;}} void this_windowClosing(WindowEvent e) { System.exit(0); }}class jindu_jButton1_actionAdapter implements java.awt.event.ActionListener { jindu adaptee; jindu_jButton1_actionAdapter(jindu adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); }}class jindu_this_windowAdapter extends java.awt.event.WindowAdapter { jindu adaptee; jindu_this_windowAdapter(jindu adaptee) { this.adaptee = adaptee; } public void windowClosing(WindowEvent e) { adaptee.this_windowClosing(e); }} 伤心了,每次我发的帖子都没人关注,晕倒ING 伤心了,每次我发的帖子都没人关注,晕倒ING ls的我没事灌水也比你没事往OBJECT数组里面插入RS强呀 哈哈 ~~~~~~~~~~~~~~ 构造里new与声明时new,有什么不同? 入门新手求助,二维数组查找 java 读写execl 简单的网络编程 【新手提问】请问如果有多个JPanel,怎么样把指定的JPanel移动顶层显示..要不后面的会被前面的栏住哦. “事件处理”这一章遇到的几个问题 大三学生请求高手指点:一个详细学习的时间计划表 我画出的图片为什么不能立即显示? 菜鸟怎么练习编代码? 请问高级程序员有没有考java语言的? 问一句代码... 请问各位高手,怎么用图形从数据库实时反应工程的进度,用什么类?~~急急
import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;public class jindu extends JFrame{
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JProgressBar jProgressBar1 = new JProgressBar();
JButton jButton1 = new JButton();
Thread myThread;
int num=500;
boolean shouldStop;//should thread to be stopped
Object lock=new Object();
JTextArea output; public jindu() {
try {
jbInit();
this.setSize(400,300);
this.show();
}
catch(Exception e) {
e.printStackTrace();
} }
public static void main(String[] args) {
jindu jindu1 = new jindu();
}
private void jbInit() throws Exception {
jPanel1.setLayout(xYLayout1);
jButton1.setText("jButton1");
jButton1.addActionListener(new jindu_jButton1_actionAdapter(this));
this.addWindowListener(new jindu_this_windowAdapter(this));
this.getContentPane().add(jPanel1, BorderLayout.CENTER); jProgressBar1.setValue(0);
jProgressBar1.setStringPainted(true); jPanel1.add(jProgressBar1, new XYConstraints(209, 222, -1, -1));
jPanel1.add(jButton1, new XYConstraints(209, 180, -1, -1));
} void jButton1_actionPerformed(ActionEvent e) { if(myThread==null)
{
myThread=new TaskThread(num);//create a task thread
shouldStop=false;
myThread.start();//start the task thread
}
else
{//end the task
synchronized (lock) {
shouldStop = true;
lock.notify(); //notify thread if stopped
}
} } class TaskThread extends Thread
{
private int max;public TaskThread(int max)
{
this.max=max;
}
public void run()
{
int min=0;
jProgressBar1.setValue(min);
jProgressBar1.setMinimum(min);
jProgressBar1.setMaximum(max);Runnable runner=new Runnable()
{
public void run()
{
int value=jProgressBar1.getValue();
value++;//increase counter value
jProgressBar1.setValue(value);//change current value of progress bar
}
};for(int i=min;i<max;i++)
{
try
{//block until AWT events hava been processed
SwingUtilities.invokeAndWait(runner);
}
catch(InvocationTargetException e)
{
break;
}
catch(InterruptedException e)
{
//Ignore Exception
}synchronized(lock)
{
if(shouldStop)//check if Stop is pressed
break;
try
{
lock.wait(100);
}
catch(InterruptedException e)
{
//Ignore Exception
}
}
}jButton1.setEnabled(true);//enable button
myThread=null;
}
} void this_windowClosing(WindowEvent e) {
System.exit(0);
}
}class jindu_jButton1_actionAdapter implements java.awt.event.ActionListener {
jindu adaptee; jindu_jButton1_actionAdapter(jindu adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}class jindu_this_windowAdapter extends java.awt.event.WindowAdapter {
jindu adaptee; jindu_this_windowAdapter(jindu adaptee) {
this.adaptee = adaptee;
}
public void windowClosing(WindowEvent e) {
adaptee.this_windowClosing(e);
}
}