📄 e9_5.java
字号:
import java.awt.event.*;
import java.awt.*;
import java.util.Date;
class E9_5 extends Frame implements Runnable,ActionListener
{ Thread thread=null; TextArea text=null;
Button b_start=new Button("Start"),b_stop=new Button("Stop");
E9_5()
{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)
{try {thread.start();}
catch(Exception e1)
{text.setText ( "在线程结束之前不赞成让线程再次调用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;
}
}
}
public static void main (String args[])
{Example tt=new Example ();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -