Package com.qilu.test;
Import java.awt.*;
Import java.awt.event.*;
Import java.applet.*;
Import Org.junit.Test;
public class Counter3 extends Applet implements Runnable {
private int count = 0;
Private Boolean runflag = true;
Private Thread selfthread = null;
Private button OnOff = New button ("Toggle"), start = New button ("Start");
Private TextField t = new TextField (10);
public void init () {
Add (t);
Start.addactionlistener (New Startl ());
Add (start);
Onoff.addactionlistener (New Onoffl ());
Add (ONOFF);
}
@Test
public void Run () {
while (true) {
try {
Selfthread.sleep (100);
} catch (Interruptedexception e) {
}
if (Runflag)
T.settext (integer.tostring (count++));
}
}
Class Startl implements ActionListener {
public void actionperformed (ActionEvent e) {
if (Selfthread = = null) {
Selfthread = new Thread (counter3.this);
Selfthread.start ();
}
}
}
Class Onoffl implements ActionListener {
public void actionperformed (ActionEvent e) {
Runflag =!runflag;
}
}
public static void Main (string[] args) {
Counter3 applet = new Counter3 ();
Frame aframe = new Frame ("Counter3");
Aframe.addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e) {
System.exit (0);
}
});
Aframe.add (applet, borderlayout.center);
Aframe.setsize (300, 200);
Applet.init ();
Applet.start ();
Aframe.setvisible (TRUE);
}
}
Code Writing time: 30%
Test time: 70%
-JUNIT4 test with main class merge thread