import java.awt.*; import java.awt.event.*; public class ActionEventDemo implements ActionListener { public static void main(String[] args) { new ActionEventDemo(); } public ActionEventDemo() { Frame frame = new Frame("AWTDemo"); frame.addWindowListener(new AdapterDemo()); frame.setLayout(new FlowLayout()); Button button = new Button("demo"); button.setActionCommand("d1"); button.addActionListener(this); frame.add(button); frame.pack(); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd == "d1") { System.out.println("Yes"); } } } class AdapterDemo extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } }

沒有留言:
張貼留言