import java.awt.Dimension; import java.awt.event.*; import javax.swing.*; /* * Created on Sep 14, 2004 * */ /** * @author Bob Futrelle * */ public class ControlWindow { JFrame controlFrame; DisplayWindow dwin; JButton textButton; JButton drawButton; TextAction myTextAction; ControlWindow(String title){ controlFrame = new JFrame(title); controlFrame.setLocation(20,50); myTextAction = new TextAction("Text"); textButton = new JButton(myTextAction); drawButton = new JButton("DrawStuff"); JPanel contentPane = new JPanel(); contentPane.setPreferredSize(new Dimension(200,50)); contentPane.add(textButton); contentPane.add(drawButton); setActions(); controlFrame.setContentPane(contentPane); controlFrame.pack(); controlFrame.setVisible(true); } void setDisplay(DisplayWindow dwin){ this.dwin = dwin; } /** * A single AbstractAction derived class can be used * as the action that responds to more than one * component, e.g., both a button and a menu item. */ class TextAction extends AbstractAction { public TextAction(String s) { super(s); } public void actionPerformed(ActionEvent e) { dwin.setText("boo"); } }// class TextAction public void setActions(){ /** * This version creates an anonymous new class * which is in a compact form. Note the * apparently odd closing line '});' required * by this form. (Look for matching parens/braces!) * Note that ActionListener is an interface, * which cannot be instantiated. But in this * context it creates a new, anonymous class * which implements ActionListener which in addition * requires that actionPerformed() be implemented. */ drawButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dwin.paintIt = true; dwin.drawPanel.repaint(); /*when the draw button is pressed * the text action is disabled. * This propagates to the text button * which is then dimmed. */ myTextAction.setEnabled(false); } }); } }