1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| import java.awt.Button; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class TestActionEvent2 { public static void main(String args[]) { Frame f = new Frame("Test"); Button b1 = new Button("Start"); Button b2 = new Button("Stop"); Monitor2 bh = new Monitor2(); b1.addActionListener(bh); b2.addActionListener(bh); f.add(b1,"North"); f.add(b2,"Center"); f.pack(); f.setVisible(true); } }
class Monitor2 implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("a button has been pressed," + "the relative info is:\n " + e.getActionCommand()); } }
|