import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class sSwing5 {
	public static void main(String argv[]) {
		JFrame frm = new JFrame("simple Swing");
		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container container = frm.getContentPane();
		container.setLayout(new GridLayout(0, 1));
		JButton b1 = new JButton("add one now!");
		container.add(b1);
		JButton b2 = new JButton("add ten every second");
		container.add(b2);
		JTextField fld = new JTextField("0", 1);
		fld.setEditable(false);
		container.add(fld);
			
		b1.addActionListener( new addOne(fld) );
	 	b2.addActionListener( new addTenPerSecond(fld) );
	 			
		frm.setLocation(300, 50);
		frm.setSize(150, 200);
		frm.setVisible(true);
	} //main()
} //class sSwing5

class addOne implements ActionListener {
	private JTextField fld;
	public addOne(JTextField fld) {
		this.fld = fld;
	} //constructor
	
	public void actionPerformed(ActionEvent e) {
		//simulating some complex operation here 
		try {
			Thread.sleep(500);
		} catch (InterruptedException ie) {
			System.out.println("an InterruptedException occurred\n"+ie.toString()+"\n"+ie.getMessage() );
		} //catch
		SwingUtilities.invokeLater( new AddEvent(fld, 1) );
	} //actionPerformed()
} //class addOne

class addTenPerSecond implements ActionListener {
	private JTextField fld;
	public addTenPerSecond(JTextField fld) {
		this.fld = fld;
	} //constructor
	public void actionPerformed(ActionEvent e) {
		new Thread(new addTen(fld) ).start();
	} //actionPerformed()
} //class addOne

class addTen implements Runnable {
	private JTextField fld;
	public addTen(JTextField fld) {
		this.fld = fld;
	} //constructor
	
	public void run() {
		while (true) {
			//simulating some complex operation here 
			try {
				Thread.sleep(500);
			} catch (InterruptedException ie) {
				System.out.println("an InterruptedException occurred\n"+ie.toString()+"\n"+ie.getMessage() );
			} //catch
			SwingUtilities.invokeLater( new AddEvent(fld, 10) );
			try {
				Thread.sleep(1000);
			} catch (InterruptedException ie) {
				System.out.println("an InterruptedException occurred\n"+ie.toString()+"\n"+ie.getMessage() );
			} //catch
		} //while
	} //actionPerformed()
} //class addTen

class AddEvent implements Runnable {
	private JTextField fld;
	private int increment;
	
	public AddEvent(JTextField fld, int inc) {
		this.fld = fld;
		increment = inc;
	} //constructor
	
	public void run() {
		fld.setText( ""+(Integer.parseInt(fld.getText())+increment) );
	} //run()
} //class AddEvent