监听事件2
作者:互联网
package cn.rushangw.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActonEvent2 {
public static void main(String[] args) {
Frame frame = new Frame("开始-结束");
Button button1 = new Button("start");
Button button2 = new Button("stop");
button1.setActionCommand("button1-start");
button2.setActionCommand("button2-stop");
MyMonitor myMonitor = new MyMonitor();
button1.addActionListener(myMonitor);
button2.addActionListener(myMonitor);
frame.add(button1,BorderLayout.WEST);
frame.add(button2,BorderLayout.EAST);
frame.pack();
frame.setVisible(true);
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//获得按钮的信息
System.out.println("按钮被点击了:msg"+e.getActionCommand());
}
}
标签:java,frame,Button,button2,button1,事件,new,监听 来源: https://www.cnblogs.com/nigustudent/p/14791061.html