📄 suspendresum.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class Suspender extends Thread
{
private int count=0;
private TextField t=new TextField(30);
public Suspender(Container c)
{
c.add(t);
}
public void run()
{
while(true)
{
t.setText("count="+(count++));
try
{
sleep(100);
}catch(InterruptedException e){}
}
}
}
public class SuspendResum extends Applet
{
private Button
start=new Button("Start"),
suspend=new Button("Suspend"),
resume=new Button("Ressume");
private boolean started=false;
private boolean suspended=false; //暂停标志
private Suspender s1=new Suspender(this);
public void init()
{
start.addActionListener(new StartL());
add(start);
suspend.addActionListener(new SuspendL());
add(suspend);
resume.addActionListener(new ResumeL());
add(resume);
}
class StartL implements ActionListener
{
//添加Start按钮事件
public void actionPerformed(ActionEvent e)
{
if(!started)
{
//线程启动后将标志位started置为true,使"Start"按钮
//被多次单击时也只会创建一个进程
started=true;
s1.start();
}
}
}
class SuspendL implements ActionListener
{
//添加Suspend按钮事件
public void actionPerformed(ActionEvent e)
{
//如果没有被暂停则调用suspend()方法暂停进程
if(!suspended)
{
suspended=true;
s1.suspend(); //调用suspend()方法暂停进程
}
}
}
class ResumeL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//如果线程已被暂停则调用resume()方法恢复执行
if(suspended)
{
suspended=false;
s1.resume(); //调用resume()方法恢复执行
}
}
}
public static void main(String[] arg)
{
SuspendResum applet=new SuspendResum();
Frame aFrame=new Frame("Suspender");
aFrame.addWindowFocusListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
aFrame.add(applet,BorderLayout.CENTER);
aFrame.setSize(250,150);
applet.init();
applet.start();
aFrame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -