Java接口:ActionListener的使用

1、例子,实现OKbutton 和Canclebutton 的点击输出;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class HandleEvent extends JFrame{


public  HandleEvent(){

    JButton okButton = new JButton("OK");
    JButton cancleButton = new JButton("cancle");

    JPanel pane = new JPanel();
    pane.add(okButton);
    pane.add(cancleButton);

    add(pane);

    //添加事件

    OKActionLisenerClass okAction = new OKActionLisenerClass();
    okButton.addActionListener(okAction);

    CancleActionListerClass cancleAction = new CancleActionListerClass();
    cancleButton.addActionListener(cancleAction);


 }

public static void main(String[] args) {

    HandleEvent frame = new HandleEvent();
    frame.setSize(400,300);
    frame.setTitle("action");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

}

class OKActionLisenerClass implements ActionListener{

public  void actionPerformed(ActionEvent e){
    System.out.println("OK action Button Pressed");
}
}


class  CancleActionListerClass implements ActionListener{

public void actionPerformed(ActionEvent e){
    System.out.println("cancle Button pressed");
}
}

你可能感兴趣的:(Java接口:ActionListener的使用)