📄 example19_9.java
字号:
import java.awt.event.*;
import java.awt.*;import java.util.Date;
class Example19_9 extends Frame implements Runnable,ActionListener
{ Thread thread=null; TextArea text=null;
Button b_start=new Button("Start"),b_stop=new Button("Stop");
Example19_9()
{ thread=new Thread(this);
text=new TextArea();add(text,"Center");
Panel p=new Panel();p.add(b_start);p.add(b_stop);
b_start.addActionListener(this);
b_stop.addActionListener(this) ;
add(p,"North");setVisible(true);
setSize(500,500);pack();setSize(500,500);
setResizable(false); //让窗口的大小不能被调整。
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==b_start)
{
if(!(thread.isAlive()))
{ thread=new Thread(this);
}
try { thread.start();
}
catch(Exception e1)
{ text.setText("线程没有结束run方法之前,不要再调用start方法");
}
}
else if(e.getSource()==b_stop)
{ thread.interrupt();
}
}
public void run()
{ while(true)
{ text.append("\n"+new Date());
try{ thread.sleep(1000);
}
catch(InterruptedException ee)
{ text.setText("我被消灭");return;//结束run语句,消灭该线程。
}
}
}
public static void main(String args[])
{ Example19_9 tt=new Example19_9();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -