package itat;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Example9_17 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Win win=new Win();
}
}
class Win extends Frame implements Runnable,ActionListener{
Thread moveOrStop;
Button bstart ,bguaqi,bhuifu,bstop;
Label moveLabel;
boolean move=false,die=false;
Win(){
moveOrStop=new Thread(this);
bstart=new Button("线程开始");
bguaqi=new Button("线程挂起");
bhuifu=new Button("线程恢复");
bstop=new Button("线程终止");
bstart.addActionListener(this);
bguaqi.addActionListener(this);
bhuifu.addActionListener(this);
bstop.addActionListener(this);
moveLabel=new Label("线程负责运动我");
moveLabel.setBackground(Color.cyan);
setLayout(new FlowLayout());
add(bstart);
add(bguaqi);
add(bhuifu);
add(bstop);
add(moveLabel);
setSize(200,300);
validate();
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void run() {
// TODO Auto-generated method stub
while(true){
while(!move){
guiqi();
}
int x=moveLabel.getBounds().x;
int y=moveLabel.getBounds().y;
y=y+2;
if(y>=200)y=10;
moveLabel.setLocation(x,y);
try {
moveOrStop.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(die==true){
return;
}
}
}
public synchronized void guiqi() {
// TODO Auto-generated method stub
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==bstart){
moveOrStop.start();
}else if(e.getSource()==bguaqi){
move=false;
}else if(e.getSource()==bhuifu){
move=true;
huifu();
}else if(e.getSource()==bstop){
die=true;
}
}
public synchronized void huifu() {
// TODO Auto-generated method stub
notifyAll();
}
}