监听事件2
Posted 逆骨111
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了监听事件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());
}
}
以上是关于监听事件2的主要内容,如果未能解决你的问题,请参考以下文章