?? 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 + -