📄 example9_17.java.bak
字号:
import java.awt.*;
import java.awt.event.*;
public class Example9_17
{
public static void main(String args[])
{ Win win= new Win();
}
}
class Win extends Frame implements Runnable,ActionListener
{ Thread moveOrStop;
Button 开始,挂起,恢复,终止;
Label moveLabel;
boolean move=false,die=false;
Win()
{ moveOrStop=new Thread(this);
开始=new Button("开始");
挂起=new Button("挂起");
恢复=new Button("恢复");
终止=new Button("终止");
开始.addActionListener(this);
挂起.addActionListener(this);
恢复.addActionListener(this);
终止.addActionListener(this);
moveLabel=new Label("线程负责运动我");
moveLabel.setBackground(Color.cyan);
setLayout(new FlowLayout());
add(开始);add(挂起);add(恢复);add(终止);add(moveLabel);
setVisible(true);
setSize(200,300);
validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ setVisible(false);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==开始)
{
try{move=true;
moveOrStop.start();}
catch(Exception event){}
}
else if(e.getSource()==挂起)
{
move=false;
}
else if(e.getSource()==恢复)
{
move=true;
恢复线程();
}
else if(e.getSource()==终止)
{
die=true;
}
}
public void run()
{ while(true)
{
while(!move)
{
try{挂起线程();}
catch(InterruptedException e){}
}
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 e2){}
if(die==true)
return;
}
}
public synchronized void 挂起线程() throws InterruptedException
{
wait();
}
public synchronized void 恢复线程()
{
notifyAll();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -