import java.awt.*; import java.awt.event.*; import java.applet.*; import java.lang.reflect.*; public class ChoiceApplet04 extends Applet { Choice theChoice = new Choice(); TextField theTextField = new TextField(20); int x,y,w,h; public void init() { theChoice.add("big"); theChoice.add("small"); theChoice.add("unknown"); add(theChoice); theChoice.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { System.out.println("ENTER----->theChoice ActionListener"); String mName = (String) e.getItem(); System.out.println("mName: " + mName); String pString = e.paramString(); // useful for Debugging System.out.println("pString: " + pString); theTextField.setText(mName); invoke(mName); System.out.println("EXIT------>theChoice ActionListener"); } }); add(theTextField); theTextField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("ENTER----->theTextField ActionListener"); String mName = e.getActionCommand(); System.out.println("mName: " + mName); if (mName.equals("big") || mName.equals("small")) theChoice.select(mName); else theChoice.select("unknown"); invoke(mName); System.out.println("EXIT------>theTextField ActionListener"); } }); } public void invoke(String mName) { try { Method m = ChoiceApplet04.class.getDeclaredMethod(mName, null); m.invoke(this, null); } catch (Exception ex) { System.err.println("no such method: "+mName); } } public void paint(Graphics g) { g.drawRect(x,y,w,h); } public void big() { System.out.println("big()"); x=10; y=50; w=100; h=100; repaint(); } public void small() { System.out.println("small()"); x=10; y=50; w=20; h=20; repaint(); } public static void main(String args[]) { Applet theApplet = new ChoiceApplet04(); appletFrame theFrame = new appletFrame(); theFrame.add("Center", theApplet); theApplet.init(); theFrame.setSize(500,300); theFrame.setVisible(true); theApplet.start(); } } class appletFrame extends Frame implements WindowListener { appletFrame() { addWindowListener(this); } public void windowClosed(WindowEvent event) { } public void windowDeiconified(WindowEvent event) { } public void windowIconified(WindowEvent event) { } public void windowActivated(WindowEvent event) { } public void windowDeactivated(WindowEvent event) { } public void windowOpened(WindowEvent event) { } public void windowClosing(WindowEvent event) { System.out.println("ENTER-----appletFrame.windowClosing"); this.setVisible(false); dispose(); System.out.println("EXIT-----appletFrame.windowClosing"); System.exit(0); } }