2008-05-08
请教一个swing线程的问题
关键字: swing线程
下面这段代码是简单模拟我遇到的问题,
分两个线程
线程A显示一个进度条
线程B循环输出数字
点击“开始”以后,我要的效果是进度条在那边滚,同时循环输出,输出结束后再显示一个新框。
而现在的现象是等输出结束后,显示新框,这时候进度条才开始滚。
请教怎么解决这个问题
分两个线程
线程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++);
}
}
}
评论
goldapple
2008-05-16
在子线程里打开是最直接简单的方法。如果不能在子线程里加代码可以考虑给子线程做个wrapper,把开窗口的代码放在wrapper里面。还有觉得你这个模拟看不懂,进度条的进度没有任何意义
kruby
2008-05-12
goldapple 写道
没见过这么用进度条的。。。
把打开新窗口的代码放在字线程里面就好了
把打开新窗口的代码放在字线程里面就好了
在下说过了,这只是对实际问题的一个模拟,打开新窗口的代码不能放在子线程里
neora
2008-05-08
想复杂了,想复杂了。
goldapple
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();
}
});
}
}
icream
2008-05-08
代LZ发帖
Kruby 写道
如果不用join(),我用全局变量,判断子线程有没有结束,似乎界面也是僵死的
icream
2008-05-08
LZ现在不能回帖,带LZ回个帖
lz说 写道
就是要等子线程结束后再继续,这个只是个演示程序,模拟实际流程
所以不知道该怎可么办
所以不知道该怎可么办
cddcdd
2008-05-08
try
{
wt.join();
} catch (InterruptedException e1)
{
// TODO 自动生成 catch 块
e1.printStackTrace();
}
加了上面之后。swing线程要等待直到wt线程结束。swing线程不运行,所以状态条就没办法显示了,这个时候界面是僵死的。
{
wt.join();
} catch (InterruptedException e1)
{
// TODO 自动生成 catch 块
e1.printStackTrace();
}
加了上面之后。swing线程要等待直到wt线程结束。swing线程不运行,所以状态条就没办法显示了,这个时候界面是僵死的。
Godlikeme
2008-05-08
恩,问题就在这段,join之后主线程要等待子线程执行完成才能继续。
huangyy
2008-05-08
为什么要加这段呢
try
{
wt.join();
} catch (InterruptedException e1)
{
// TODO 自动生成 catch 块
e1.printStackTrace();
}发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 518 次
- 性别:

- 来自: 无锡

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
搞了2年j2se,现在跳槽都 ...
javase很精通,还有什么学不会的吗?我是搞javaee的,感觉javase很 ...
-- by moses3017 -
搞了2年j2se,现在跳槽都 ...
如果你J2SE基础好的话,的确很多公司愿意要你 基础比什么所谓的Spring之类 ...
-- by wl95421 -
搞了2年j2se,现在跳槽都 ...
这帖怎么又被挖出来了 LZ已经去南瑞了
-- by icream -
搞了2年j2se,现在跳槽都 ...
java,c++,语言真的有那么重要?我很茫然。。。
-- by fungway -
搞了2年j2se,现在跳槽都 ...
没想到永中薪水这么低。
-- by dongle2001






评论排行榜