package com.zym.lesson7; 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 TestActionEvent2 { public static void main(String[] args) { Frame frame=new Frame("课堂练习,事件监听启动、停止"); //按钮按下,触发事件 Button btn_start=new Button("start"); Button btn_stop =new Button("stop"); btn_start.setActionCommand("start"); btn_stop.setActionCommand("stop"); frame.add(btn_start); frame.add(btn_stop); //它需要 一个监听addActionListener,所以建一个类去实现 MyActonListener2 myActonListener2=new MyActonListener2(); //两个按钮共同实现了这个事件 btn_start.addActionListener(myActonListener2); btn_stop.addActionListener(myActonListener2); //设置流式布局 默认是靠中 frame.setLayout(new FlowLayout()); frame.setSize(200,200); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { //窗体关闭时做的事 @Override public void windowClosing(WindowEvent e) { // super.windowClosed(e); System.exit(0); } } ); } } class MyActonListener2 implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { e.getActionCommand(); System.out.println("aaaaaa"+e.getActionCommand()); } }
标签:lesson7,java,frame,stop,start,启停,new,btn,监听 From: https://www.cnblogs.com/zym97816/p/17736865.html