import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Date;
public class Test extends Applet implements WindowListener, ActionListener, Runnable {
public static void main(String[] args) {
Frame F=new Frame("Event handling with threads and multirun prevention");
Test BD=new Test();
F.setSize(420, 350);
F.addWindowListener(BD);
F.add(BD);
BD.init();
F.setVisible(true);
}
Button BS;
Button BC;
Checkbox CB;
TextArea TA;
int cnt;
public void init() {
cnt=0;
setLayout(new BorderLayout());
Label l=new Label("Event handling with threads and multirun prevention",Label.CENTER);
l.setFont(new Font("Helvetica",Font.BOLD,16));
add("North", l);
TA=new TextArea("", 60, 10, TextArea.SCROLLBARS_VERTICAL_ONLY);
TA.setEditable(false);
TA.setFont(new Font("Courier",Font.BOLD,14));
add("Center", TA);
BS=new Button("Process");
BS.addActionListener(this);
BC=new Button("Clear");
BC.addActionListener(this);
CB=new Checkbox("only once", false);
Panel SU=new Panel();
SU.setLayout(new FlowLayout());
SU.add(BS);
SU.add(BC);
SU.add(CB);
add("South", SU);
}
private void DoSomething() {
cnt++;
int saved=cnt;
TA.append(saved+" GO...\n");
try {
Thread.currentThread().sleep(5000);
} catch (Exception err) {
}
TA.append(saved+" DONE!\n");
BS.setEnabled(true);
}
public void run() {
DoSomething();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==BS) {
if (CB.getState()) BS.setEnabled(false);
(new Thread(this)).start();
} else if (e.getSource()==BC) {
TA.setText("");
}
}
public void windowClosing(WindowEvent e) { System.exit(0); }
public void windowClosed(WindowEvent e) { }
public void windowOpened(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
}