按钮事件监听 //按钮事件监听 package com.zeng.GUI; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TextActionEvent { public static void main(String[] Args) { Frame frame = new Frame("事件监听"); frame.setSize(500, 500); frame.setVisible(true); Button button = new Button(); frame.add(button); button.addActionListener(new MyActionListner());//按钮事件监听 WindowClose(frame);//窗口关闭 } //窗口关闭方法 private static void WindowClose(Frame frame) { frame.addWindowListener(new WindowAdapter() {//匿名内部类 @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } //按钮事件监听类 class MyActionListner implements ActionListener { @Override public void actionPerformed(ActionEvent e) { System.out.println("我爱你"); } }