privatestaticclassMyMonitorimplementsActionListener{//build the ActionLister for the north button and the south button ,named myActionListener@OverridepublicvoidactionPerformed(ActionEvent e){//输入 e. 查看源码.if(e.getActionCommand()=="north"){
System.out.println("north Button been clicked ,and MyMonitor class run successfully.");}elseif(e.getActionCommand()=="south"){
System.out.println("south Button been clicked ,and MyMonitor class run successfully.");}}}
package GUI.事件监听;import GUI.MyClass.MySystemExit;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;publicclassTest2两个按钮实现同一个监听 {publicstaticvoidmain(String[] args){
Frame frame =newFrame("Test2两个按钮实现同一个监听");
frame.setVisible(true);
Button north =newButton("north");
Button south =newButton("south");//自定义触发会显示的ActionCommand 默认 为Button("...");中的值.//add listener for the south and north...
north.addActionListener(newMyMonitor());
south.addActionListener(newMyMonitor());//2个按钮add the same ActionListener
frame.add(north, BorderLayout.NORTH);
frame.add(south, BorderLayout.SOUTH);//l,s,c
frame.setLocation(100,100);
frame.setSize(400,400);
frame.setBackground(newColor(99,255,240));//System.exit(0);newMySystemExit(frame);}privatestaticclassMyMonitorimplementsActionListener{//build the ActionLister for the north button and the south button ,named myActionListener@OverridepublicvoidactionPerformed(ActionEvent e){//输入 e. 查看源码.if(e.getActionCommand()=="north"){
System.out.println("north Button been clicked ,and MyMonitor class run successfully.");}elseif(e.getActionCommand()=="south"){
System.out.println("south Button been clicked ,and MyMonitor class run successfully.");}}}}
south Button been clicked ,and MyMonitor class run successfully.
north Button been clicked ,and MyMonitor class run successfully.
south Button been clicked ,and MyMonitor class run successfully.
north Button been clicked ,and MyMonitor class run successfully.