package day15; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TwoButton { public static void main(String[] args) { Frame frame = new Frame("开始-停止"); Button button1 = new Button("start"); Button button2 = new Button("stop"); button2.setActionCommand("button2-stop"); //可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值 //可以多个按钮只写一个监听类 MyMonitor myMonitor = new MyMonitor(); button1.addActionListener(myMonitor); button2.addActionListener(myMonitor); frame.add(button1, BorderLayout.NORTH); frame.add(button2, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); } } class MyMonitor implements ActionListener { @Override public void actionPerformed(ActionEvent e) { System.out.println("按钮被点击了:" + e.getActionCommand()); } }
执行结果: