MyIF interface development example - b

By Nicholas Duchon - March 16, 2012, Musings

UML from jGrasp:

Display:

Code:

// File: MyIFb
// Author: Nicholas Duchon
// Date: Feb 19, 2006
// Part b of the interface demonstrations

import javax.swing.JApplet;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.awt.Color;

public class MyIFb extends JApplet {
   private JButton jb = new JButton ("Button: 0");
   private JButton je = new JButton ("Disable");
   private IncrementButton ib = new IncrementButton();
   private EnableButton    eb = new EnableButton();
   private DisableButton   db = new DisableButton();
   private int count = 0;
  
   public void init () {
      setLayout (new FlowLayout ());
      je.setBackground (Color.green);
      je.addActionListener (db);
      jb.addActionListener (ib);
      add (je);
      add (jb);
   } // end method init
  
   class EnableButton implements ActionListener {
      public void actionPerformed (ActionEvent e) {
         je.setText ("Disable");
         je.setBackground (Color.green);
         je.removeActionListener (eb);
         je.addActionListener (db);
         jb.addActionListener (ib);
      } // end actionPerformed - required by interface
   } // end inner class EnableButton
  
   class DisableButton implements ActionListener {
      public void actionPerformed (ActionEvent e) {
         je.setText ("Enable");
         je.setBackground (Color.red);
         je.removeActionListener (db);
         je.addActionListener (eb);
         jb.removeActionListener (ib);
      } // end actionPerformed - required by interface
   } // end inner class EnableButton
  
   class IncrementButton implements ActionListener {
      public void actionPerformed (ActionEvent e) {
         count++;
         jb.setText ("button: " + count);
      } // end actionPerformed - required by interface
   } // end inner class InrementButton

} // end class MyIFa