import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class sSwing4 {
	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 sSwing4

class addOne implements ActionListener {
	private JTextField fld;
	public addOne(JTextField fld) {
		this.fld = fld;
	} //constructor
	
	public void actionPerformed(ActionEvent e) {
		int value = Integer.parseInt(fld.getText());
		//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
		value++;
		fld.setText(""+value);
	} //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) {
			int value = Integer.parseInt(fld.getText());
			//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
			value+=10;
			fld.setText(""+value);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException ie) {
				System.out.println("an InterruptedException occurred\n"+ie.toString()+"\n"+ie.getMessage() );
			} //catch
		} //while
	} //actionPerformed()
} //class addTen