首页上一页 1 下一页尾页 1 条记录 1/1页
请教:有关线程的加入
发表在Java图书答疑
2010-06-21
是否精华
是
否
版块置顶:
是
否
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class JoinTest extends JFrame {
/**
* @param args
*/
Thread threadA;
Thread threadB;
JProgressBar bar1=new JProgressBar();
JProgressBar bar2=new JProgressBar();
public JoinTest() {
// TODO Auto-generated constructor stub
super();
getContentPane().add(bar1,BorderLayout.NORTH);
getContentPane().add(bar2,BorderLayout.SOUTH);
bar1.setStringPainted(true);
bar2.setStringPainted(true);
threadA=new Thread(new Runnable() {
int count=0;
public void run() {
// TODO Auto-generated method stub
while(true){
bar1.setValue(++count);
try {
threadA.sleep(100);
threadB.join();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
});
threadA.start();
threadB=new Thread(new Runnable() {
int count=0;
public void run() {
// TODO Auto-generated method stub
while(true){
bar2.setValue(++count);
try {
threadB.sleep(100);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
if(count==100)
break;
}
}
});
threadB.start();
}
public static void init(JFrame frame,int width,int height){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
init(new JoinTest(),200,200);
}
}
这是书上的例18.4,
如果,我想:先bar1到50%,然后停止,让bar2加入,bar2到50%时,再停止,让bar1开始,直到100%,最后,bar2到100%。程序该如何改,我改了几次都不成功。
麻烦赐教,谢谢。
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
public class JoinTest extends JFrame {
/**
* @param args
*/
Thread threadA;
Thread threadB;
JProgressBar bar1=new JProgressBar();
JProgressBar bar2=new JProgressBar();
public JoinTest() {
// TODO Auto-generated constructor stub
super();
getContentPane().add(bar1,BorderLayout.NORTH);
getContentPane().add(bar2,BorderLayout.SOUTH);
bar1.setStringPainted(true);
bar2.setStringPainted(true);
threadA=new Thread(new Runnable() {
int count=0;
public void run() {
// TODO Auto-generated method stub
while(true){
bar1.setValue(++count);
try {
threadA.sleep(100);
threadB.join();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
});
threadA.start();
threadB=new Thread(new Runnable() {
int count=0;
public void run() {
// TODO Auto-generated method stub
while(true){
bar2.setValue(++count);
try {
threadB.sleep(100);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
if(count==100)
break;
}
}
});
threadB.start();
}
public static void init(JFrame frame,int width,int height){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
init(new JoinTest(),200,200);
}
}
这是书上的例18.4,
如果,我想:先bar1到50%,然后停止,让bar2加入,bar2到50%时,再停止,让bar1开始,直到100%,最后,bar2到100%。程序该如何改,我改了几次都不成功。
麻烦赐教,谢谢。