import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class ProgressBar implements ActionListener
{
JFrame frame=null;
JProgressBar progressbar;
JLabel label;
JButton b;
MyThread thread;
private int len = 20;
private int size = 100;
private int value = 0;
private boolean doing = true;
public ProgressBar()
{
frame=new JFrame("安装");
frame.setBounds(100, 100, 400, 130);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPanel=frame.getContentPane();
label=new JLabel("",JLabel.CENTER);
progressbar = new JProgressBar();
progressbar.setOrientation(JProgressBar.HORIZONTAL);
progressbar.setMinimum(0);
progressbar.setMaximum(100);
progressbar.setValue(0);
progressbar.setStringPainted(true);
progressbar.setPreferredSize(new Dimension(300,20));
progressbar.setBorderPainted(true);
progressbar.setBackground(Color.pink);
thread = new MyThread();
JPanel panel=new JPanel();
b=new JButton("安装");
b.setForeground(Color.blue);
b.addActionListener(this);
panel.add(b); contentPanel.add(panel,BorderLayout.NORTH);
contentPanel.add(progressbar,BorderLayout.CENTER);
contentPanel.add(label,BorderLayout.SOUTH);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
thread.start();
value += len/(float)size*100;
System.out.println("...."+value);
}
}
//线程类
public class MyThread extends Thread
{
public MyThread()
{
}
public void run()
{
while(doing)
{
progressbar.setValue(value);
}
}
}
public static void main(String[] args)
{
ProgressBar app=new ProgressBar();
}
}
代码如上,程序很简单,就是单击按钮,进度条显示相应进度,由于进度条的进度值是在单击按钮后实时更新的,所以要实现同步更新,但现在问题是不知道怎么写了,刚开始学习线程,所以不太明白,请大家帮帮忙,指导一下
import java.awt.event.*;
import javax.swing.*;public class ProgressBar implements ActionListener
{
JFrame frame=null;
JProgressBar progressbar;
JLabel label;
JButton b;
MyThread thread;
private int len = 20;
private int size = 100;
private int value = 0;
private boolean doing = true;
public ProgressBar()
{
frame=new JFrame("安装");
frame.setBounds(100, 100, 400, 130);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPanel=frame.getContentPane();
label=new JLabel("",JLabel.CENTER);
progressbar = new JProgressBar();
progressbar.setOrientation(JProgressBar.HORIZONTAL);
progressbar.setMinimum(0);
progressbar.setMaximum(100);
progressbar.setValue(0);
progressbar.setStringPainted(true);
progressbar.setPreferredSize(new Dimension(300,20));
progressbar.setBorderPainted(true);
progressbar.setBackground(Color.pink);
thread = new MyThread();
JPanel panel=new JPanel();
b=new JButton("安装");
b.setForeground(Color.blue);
b.addActionListener(this);
panel.add(b); contentPanel.add(panel,BorderLayout.NORTH);
contentPanel.add(progressbar,BorderLayout.CENTER);
contentPanel.add(label,BorderLayout.SOUTH);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
thread.start();
value += len/(float)size*100;
System.out.println("...."+value);
}
}
//线程类
public class MyThread extends Thread
{
public MyThread()
{
}
public void run()
{
while(doing)
{
progressbar.setValue(value);
}
}
}
public static void main(String[] args)
{
ProgressBar app=new ProgressBar();
}
}
代码如上,程序很简单,就是单击按钮,进度条显示相应进度,由于进度条的进度值是在单击按钮后实时更新的,所以要实现同步更新,但现在问题是不知道怎么写了,刚开始学习线程,所以不太明白,请大家帮帮忙,指导一下
public class MyThread extends Thread
{
JProgressBar pb;
int step;
public MyThread(JProgressBar pb, int step)
{
this.pb = pb;
this.step = step;
} public void run()
{
while(true)
{
int value = pb.getValue();
if (value == pb.getMaximum()) break;
value += step;
pb.setValue(value);
System.out.println("...."+value); try {
sleep(1000);
} catch (Exception e) {}
}
}
}thread = new MyThread(); -> thread = new MyThread(progressbar, len*100/size);public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
if (thread.getState() == Thread.State.NEW) {
thread.start();
}
//value += len/(float)size*100;
//System.out.println("...."+value);
}
}
for example
public class MyThread extends Thread
{
JProgressBar pb;
int step;
int id;
static int value = 0;
public MyThread(JProgressBar pb, int step, int id)
{
this.pb = pb;
this.step = step;
this.id = id;
} public void run()
{
while(true)
{
synchronized (pb) {
try {
if (id == 0) { //如果是负责增加value的线程
while (value != pb.getValue()) { //判断value是否已经更新过
pb.notifyAll();
pb.wait(); //更新过则唤醒其他线程,然后自己等待
}
value += step;
System.out.println("update value...."+value);
} else { //如果是负责刷新进度条的线程
while (value == pb.getValue()) { //判断是否已经刷新过
pb.notifyAll();
pb.wait(); //刷新过则唤醒其他线程,然后自己等待
}
pb.setValue(value);
System.out.println("refresh progressbar...."+value);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
pb.notifyAll(); //最后唤醒其他线程
} if (value == 100) {break;} //进度条达到最大,则退出循环
}
yield();
}
}
}定义两个线程
MyThread thread1,thread2;thread = new MyThread(); ->
thread1 = new MyThread(progressbar, len*100/size, 0);
thread2 = new MyThread(progressbar, len*100/size, 1);public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
if (thread1.getState() == Thread.State.NEW) {
thread1.start();
}
if (thread2.getState() == Thread.State.NEW) {
thread2.start();
}
//value += len/(float)size*100;
//System.out.println("...."+value);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class ProgressBar implements ActionListener
{
JFrame frame=null;
JProgressBar progressbar;
JLabel label;
JButton b;
MyThread thread;
private int len = 20;
private int size = 100;
private int value = 0;
private boolean doing = true;
public ProgressBar()
{
frame=new JFrame("安装");
frame.setBounds(100, 100, 400, 130);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPanel=frame.getContentPane();
label=new JLabel("",JLabel.CENTER);
progressbar = new JProgressBar();
progressbar.setOrientation(JProgressBar.HORIZONTAL);
progressbar.setMinimum(0);
progressbar.setMaximum(100);
progressbar.setValue(0);
progressbar.setStringPainted(true);
progressbar.setPreferredSize(new Dimension(300,20));
progressbar.setBorderPainted(true);
progressbar.setBackground(Color.pink);
thread = new MyThread();
JPanel panel=new JPanel();
b=new JButton("安装");
b.setForeground(Color.blue);
b.addActionListener(this);
panel.add(b);contentPanel.add(panel,BorderLayout.NORTH);
contentPanel.add(progressbar,BorderLayout.CENTER);
contentPanel.add(label,BorderLayout.SOUTH);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
thread.start();
value += len/(float)size*100;
System.out.println("...."+value);
}}
//线程类
public class MyThread extends Thread
{
public MyThread()
{
}public void run()
{
while(doing)
{
progressbar.setValue(value);
}
}
}
public static void main(String[] args)
{
ProgressBar app=new ProgressBar();
}
}