返回信息流我的java程序跑的比较慢所以就想加上进度条,但每次运行的时候都是先弹出进度条的框,直到另一个线程运行完才开始进度条的效果(ps,我的进度条只是为了在等程序运行的时候不那么无聊,两个线程根本没有交集),然后现在好像就是在并行的时候,进度条的那个线程被阻塞了,直到另一个线程运行完才出结果
以下是我的进度条的代码
public class ProgressBarDemo2 extends JPanel implements Runnable{
//JPanel Panel=new JPanel();
private JProgressBar progressBar;
private JButton startButton;
private JTextArea taskOutput;
private Task task;
public static ArrayList<String> keyword=new ArrayList<String>();
private Runnable run=null;
public int progress;
int k=0;
int j=0;
class Task extends SwingWorker<Void, Void> {
//SwingWorker testWorker = new SwingWorker<Icon , Void>(){
/*
* Main task. Executed in background thread.
*/
@Override
public Void doInBackground() throws Exception{////////////////////////
Random random = new Random();
int progress = 0;
//System.out.println("22222222");
//Initialize progress property.
setProgress(0);
//Sleep for at least one second to simulate "startup".
try {
Thread.sleep(1000 + random.nextInt(2000));
} catch (InterruptedException ignore) {}
while (progress < 100) {
//Sleep for up to one second.
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException ignore) {}
//Make random progress.
//System.out.println("11111111111111");
progress += random.nextInt(10);
setProgress(Math.min(progress, 100));
}
return null;
}
/*
* Executed in event dispatch thread
*/
public void done() {/////////////////////////////
Toolkit.getDefaultToolkit().beep();
setEnabled(true);
taskOutput.append("Done!\n");
}
}
public ProgressBarDemo2() {
super(new BorderLayout());
//Create the demo's UI.
//startButton = new JButton("Start");
// startButton.setActionCommand("start");
// startButton.addActionListener(new startbutton());
addComponentListener(new button());
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
//System.out.println("11111111111111");
//Call setStringPainted now so that the progress bar height
//stays the same whether or not the string is shown.
progressBar.setStringPainted(true);
///test
run = new Runnable(){//实例化更新组件的线程
public void run() {
}
};
taskOutput = new JTextArea(8, 30);
taskOutput.setMargin(new Insets(5,5,5,5));
taskOutput.setEditable(false);
//System.out.println("22222222");
JPanel panel = new JPanel();
//panel.add(startButton);
panel.add(progressBar);
//System.out.println("333333");
add(panel, BorderLayout.PAGE_START);
add(new JScrollPane(taskOutput), BorderLayout.CENTER);
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}
/**
* Create the GUI and show it. As with all GUI code, this must run
* on the event-dispatching thread.
*/
public void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("ProgressBarDemo2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new ProgressBarDemo2();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
/**
* Invoked when the user presses the start button.
*/
class button implements ActionListener,ComponentListener{
public void actionPerformed(ActionEvent evt) {
progressBar.setIndeterminate(true);
setEnabled(false);
//Instances of javax.swing.SwingWorker are not reusuable, so
//we create new instances as needed.
task = new Task();
task.addPropertyChangeListener(new property());
task.execute();
}
@Override
public void componentResized(ComponentEvent e) {
// TODO Auto-generated method
progressBar.setIndeterminate(true);
setEnabled(false);
//Instances of javax.swing.SwingWorker are not reusuable, so
//we create new instances as needed.
task = new Task();
task.addPropertyChangeListener(new property());
task.execute();
}
@Override
public void componentMoved(ComponentEvent e) {
// TODO Auto-generated method stub
}
@Override
public void componentShown(ComponentEvent e) {
// TODO Auto-generated method stub
}
@Override
public void componentHidden(ComponentEvent e) {
// TODO Auto-generated method stub
}
}
class property implements PropertyChangeListener{
@Override
/**
* Invoked when task's progress property changes.
*/
public void propertyChange(PropertyChangeEvent evt) {
if ("progress" == evt.getPropertyName()) {
int progress = (Integer) evt.getNewValue();
progressBar.setIndeterminate(false);
progressBar.setValue(progress);
if(k<=1){//
taskOutput.append(String.format(
"connect to ontology base.\n", progress));
k++;
}else if(k>1&&k<=3){
taskOutput.append(String.format(
"input rules into reasoner.\n", progress));
k++;
}else if(k>3&&k<=5){
taskOutput.append(String.format(
"find all keywords in ontology base.\n", progress));
k++;
}else if(k>5&&k<=7){
taskOutput.append(String.format(
"find keywords equal to %s.\n", keyword.get(j)));
k++;
}else if(k>7&&k<=9){
taskOutput.append(String.format(
"find subject about %s and it's related words.\n", keyword.get(j)));
k++;
}else if(k>9&&k<=11){
taskOutput.append(String.format(
"find subject about %s and it's related words.\n", keyword.get(j)));
k++;
if(j<1){
j++;
}
}else if(k>11&&k<=14){
taskOutput.append(String.format(
"find keywords equal to %s.\n", keyword.get(j)));
k++;
System.out.println(j);
}else if (k>14){
taskOutput.append(String.format(
"find subject about %s from range of first resoning.\n", keyword.get(j)));
k++;
System.out.println(j);
}
}//if progress
}//event
}//class
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
ProgressBarDemo2 demo=new ProgressBarDemo2();
demo.createAndShowGUI();
}
});
}
@Override
public void run() {
// TODO Auto-generated method stub
ProgressBarDemo2 demo=new ProgressBarDemo2();
demo.createAndShowGUI();
}
}
然后线程调用
Thread demothread=new Thread(new ProgressBarDemo2());
demothread.start();
Thread threadsearch=new Thread(new inference.test());
threadsearch.start();
跪谢跪谢
这是一条镜像帖。来源:北邮人论坛 / java / #40385同步于 2015/5/2
Java机器人发帖
求问关于给java程序加进度条的问题
kate8528577
2015/5/2镜像同步0 回复
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。