论坛首页 Java版

请教一个swing线程的问题

浏览 634 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2008-05-08 关键字: swing线程
下面这段代码是简单模拟我遇到的问题,
分两个线程
线程A显示一个进度条
线程B循环输出数字

点击“开始”以后,我要的效果是进度条在那边滚,同时循环输出,输出结束后再显示一个新框。
而现在的现象是等输出结束后,显示新框,这时候进度条才开始滚。
请教怎么解决这个问题


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

public class ProgressSample
{
	static class BarThread extends Thread
	{
		private static int DELAY = 500;
		JProgressBar progressBar;

		public BarThread(JProgressBar bar)
		{
			progressBar = bar;
		}

		public void run()
		{
			int minimum = progressBar.getMinimum();
			int maximum = progressBar.getMaximum();
			Runnable runner = new Runnable()
			{
				public void run()
				{
					int value = progressBar.getValue();
					progressBar.setValue(value + 1);
				}
			};
			for (int i = minimum; i < maximum; i++)
			{
				try
				{
					SwingUtilities.invokeAndWait(runner);
					// Our task for each step is to just sleep
					Thread.sleep(DELAY);
				} catch (InterruptedException ignoredException)
				{
				} catch (InvocationTargetException ignoredException)
				{
				}
			}
		}
	}

	public static void main(String args[])
	{
		// Initialize
		final JProgressBar aJProgressBar = new JProgressBar(0, 100);
		final JButton aJButton = new JButton("Start");
		ActionListener actionListener = new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				aJButton.setEnabled(false);
				Thread stepper = new BarThread(aJProgressBar);
				stepper.start();
				
				whileThread wt = new whileThread();
				wt.start();
				try
				{
					wt.join();
				} catch (InterruptedException e1)
				{
					// TODO 自动生成 catch 块
					e1.printStackTrace();
				}
				JFrame newFrame = new JFrame("&&&&&&&&&&&&&&&&&&&&&&");
				newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				newFrame.setLocation(500, 400);
				newFrame.setSize(300, 100);
				newFrame.show();
			}
		};
		aJButton.addActionListener(actionListener);
		JFrame theFrame = new JFrame("Progress Bars");
		theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container contentPane = theFrame.getContentPane();
		contentPane.add(aJProgressBar, BorderLayout.NORTH);
		contentPane.add(aJButton, BorderLayout.SOUTH);
		theFrame.setLocation(500, 200);
		theFrame.setSize(300, 100);
		theFrame.show();
	}
	
	
}
class whileThread extends Thread
{
	public void run()
	{
		int i =0;
		while(i < 1000000)
		{
			System.out.println(i++);
		}
	}
}
   
时间:2008-05-08
为什么要加这段呢
try
				{
					wt.join();
				} catch (InterruptedException e1)
				{
					// TODO 自动生成 catch 块
					e1.printStackTrace();
				}
   
0 请登录后投票
时间:2008-05-08
恩,问题就在这段,join之后主线程要等待子线程执行完成才能继续。
   
0 请登录后投票
时间:2008-05-08
try
{
wt.join();
} catch (InterruptedException e1)
{
// TODO 自动生成 catch 块
e1.printStackTrace();
}


加了上面之后。swing线程要等待直到wt线程结束。swing线程不运行,所以状态条就没办法显示了,这个时候界面是僵死的。
   
0 请登录后投票
时间:2008-05-08
LZ现在不能回帖,带LZ回个帖
lz说 写道
就是要等子线程结束后再继续,这个只是个演示程序,模拟实际流程
所以不知道该怎可么办
   
0 请登录后投票
时间:2008-05-08
代LZ发帖
Kruby 写道
如果不用join(),我用全局变量,判断子线程有没有结束,似乎界面也是僵死的
   
0 请登录后投票
时间:2008-05-08
没见过这么用进度条的。。。

把打开新窗口的代码放在字线程里面就好了
package swing.progress;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

public class ProgressSample
{
	static class BarThread extends Thread
	{
		private static int DELAY = 500;
		JProgressBar progressBar;

		public BarThread(JProgressBar bar)
		{
			progressBar = bar;
		}

		public void run()
		{
			int minimum = progressBar.getMinimum();
			int maximum = progressBar.getMaximum();
			Runnable runner = new Runnable()
			{
				public void run()
				{
					int value = progressBar.getValue();
					progressBar.setValue(value + 1);
				}
			};
			for (int i = minimum; i < maximum; i++)
			{
				try
				{
					SwingUtilities.invokeAndWait(runner);
					// Our task for each step is to just sleep
					Thread.sleep(DELAY);
				} catch (InterruptedException ignoredException)
				{
				} catch (InvocationTargetException ignoredException)
				{
				}
			}
		}
	}

	public static void main(String args[])
	{
		// Initialize
		final JProgressBar aJProgressBar = new JProgressBar(0, 100);
		final JButton aJButton = new JButton("Start");
		ActionListener actionListener = new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				aJButton.setEnabled(false);
				Thread stepper = new BarThread(aJProgressBar);
				stepper.start();
				
				whileThread wt = new whileThread();
				wt.start();
//				try
//				{
//					wt.join();
//				} catch (InterruptedException e1)
//				{
//					// TODO 自动生成 catch 块
//					e1.printStackTrace();
//				}
			}
		};
		aJButton.addActionListener(actionListener);
		JFrame theFrame = new JFrame("Progress Bars");
		theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container contentPane = theFrame.getContentPane();
		contentPane.add(aJProgressBar, BorderLayout.NORTH);
		contentPane.add(aJButton, BorderLayout.SOUTH);
		theFrame.setLocation(500, 200);
		theFrame.setSize(300, 100);
		theFrame.show();
	}
	
	
}
class whileThread extends Thread
{
	public void run()
	{
		int i =0;
		while(i < 1000000)
		{
			System.out.println(i++);
		}
		SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				JFrame newFrame = new JFrame("&&&&&&&&&&&&&&&&&&&&&&");
				newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				newFrame.setLocation(500, 400);
				newFrame.setSize(300, 100);
				newFrame.show();
			}
		});
	}
}
   
0 请登录后投票
时间:2008-05-08
想复杂了,想复杂了。
   
0 请登录后投票
时间:2008-05-12
goldapple 写道
没见过这么用进度条的。。。

把打开新窗口的代码放在字线程里面就好了


在下说过了,这只是对实际问题的一个模拟,打开新窗口的代码不能放在子线程里
   
0 请登录后投票
时间:2008-05-16
在子线程里打开是最直接简单的方法。如果不能在子线程里加代码可以考虑给子线程做个wrapper,把开窗口的代码放在wrapper里面。还有觉得你这个模拟看不懂,进度条的进度没有任何意义
   
0 请登录后投票
论坛首页 Java版

跳转论坛:
JavaEye推荐