import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** Displays a window containing a couple fields and buttons. */
public class SimpleFieldDemo {

  private static int val1 = 1;  // application data: two numbers
  private static int val2 = 2;  // these are displayed in buttons below

  public static void main(String[] args) {
    JFrame frame = new JFrame("A Window");
    frame.setSize(300, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField field1 = new JTextField(10);
    field1.setText("" + val1);

    JTextField field2 = new JTextField(10);
    field2.setText("" + val2);

    JButton button1 = new JButton("Make bigger");
    button1.addActionListener(new Button1Listener(field1));

    JButton button2 = new JButton("Make bigger");
    button2.addActionListener(new Button2Listener(field2));

    JPanel panel = new JPanel();
    panel.add(field1);
    panel.add(button1);
    panel.add(field2);
    panel.add(button2);

    frame.add(panel);
    frame.setVisible(true);
  }

  /**
   * Called when the user clicks on button1. Responds by making sure that val1
   * is bigger than val2.
   */
  private static class Button1Listener implements ActionListener {

    private final JTextField field;  // field where val2 is shown

    public Button1Listener(JTextField field) {
      this.field = field;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      if (val2 > val1) {
        val1 = val2 + 1;           // make 1 bigger
        field.setText("" + val1);  // update UI to show new value
      }
    }
  }

  /**
   * Called when the user clicks on button2. Responds by making sure that val2
   * is bigger than val1.
   */
  private static class Button2Listener implements ActionListener {

    private final JTextField field;  // field where val2 is shown

    public Button2Listener(JTextField field) {
      this.field = field;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      if (val1 > val2) {
        val2 = val1 + 1;           // make 2 bigger
        field.setText("" + val2);  // update UI to show new value
      }
    }
  }
}